0

I have class files and a text file wrapped up in a jar. This problem has been solved on the internet before, but when I try it I get a null pointer exception, or File f.exists() returns false. It should be noted that my code is not in a package. It should be noted that when help.txt is dropped in the same folder as the jar, then it works.

`MyClass z = new MyClass();
String helpPath = z.getClass().getClassLoader().getResource("help.txt").toString();
File f = new File(helpPath);
if (f.exists()){
     Desktop d = Desktop.getDesktop();
     d.open(f);`

It should also be noted that I have code written to open powershell and then java my class file, with no specified classpath.

` Runtime.getRuntime().exec(new String[]{"cmd","/c","start","powershell","-noexit","/c","java -jar \"" + filename + "\""});`
Simon
  • 51
  • 5
  • A resource inside a jar is not a file. It's not on the file system.It's a zip entry inside the jar. So you can't use file IO to read it. If you want some external program to open a file, then it needs to exist, as a file, on the file system. What are you trying to achieve? – JB Nizet Jan 11 '20 at 13:38
  • I want the user to type 'help' into the program, and for the program to find help.txt in the jar and print it all out. I wouldn't call it an external program, because it lives in the same jar. Right? – Simon Jan 11 '20 at 13:42
  • print it on the standard output stream? Because that's not what `d.open(f)` does. If you ant to preint it, then read what is contains by using getResourceAsStream(), and then print what you read to System.out. – JB Nizet Jan 11 '20 at 13:45
  • Yes, d.open() was my first idea, and I changed plans. Sorry, that was unclear. – Simon Jan 11 '20 at 13:48

0 Answers0