I am getting an NPE at the point of getting path of a File (an sh file in assets folder). I have tried to read about NPE i detail from the following thread, but this actually could not solve my problem.
What is a NullPointerException, and how do I fix it?
Following is my code snippet:
File absPathofBash;
url = ClassLoader.class.getResource("assets/forbackingup.sh");
absPathofBash = new File(url.getPath());
Later I'm using it in a ProcessBuilder, as
ProcessBuilder pb = new ProcessBuilder(url.getPath(), param2, param3)
I've also tried getting the absolute path directly, like
absPathofBash = new File("assets/forbackingup.sh").getAbsolutePath();
Using the latter way, I am able to process it, but if I create a jar then the file cannot be found. (although the Jar contains the file within the respective folder assets)
I would be thankful if anyone can help me on that.
Fileto access embedded resources which are inside theJarfile(s), they aren't "files" in the sense that the file system sees them, they a bunch of compressed bytes inside a zip file (essentially). In your case, if you want to execute the file, you will need to extract the file from the Jar file (you can useClass#getResourceAsStream) and then execute it