java.lang.NullPointerException while getting a filepath in Java (executing through a jar file)

元气小坏坏 提交于 2019-12-22 11:29:07

问题


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.


回答1:


Once you have packaged your code as a jar, you can not load files that are inside the jar using file path, instead they are class resources and you have to use this to load:

this.getClass().getClassLoader().getResource("assets/forbackingup.sh");

This way you load assets/forbackingup.sh as an absolute path inside your jar. you also can use this.getClass().getResource() but this way the path must be relative to this class path inside jar.

getResource method gives you an URL, if you want to get directly an InputStream you can use getResourceAsStream

Hope it helps!




回答2:


Since the file itself is in the jar file, you could try using:

InputStream is = this.getClass().getClassLoader().getResourceAsStream(fileNameFromJar);



回答3:


In case of jar file , classloader will return URL different than that of when the target file is not embedded inside jar. Refer to answer on link which should help u : How to use ClassLoader.getResources() in jar file




回答4:


I got it done by creating a temp file. Though it's not difficult, yet I'm posting the code patch here:

InputStream stream = MyClass.class.getClassLoader().
            getResourceAsStream("assets/forbackingup.sh");

        File temp = File.createTempFile("forbackingup", ".sh");



      OutputStream outputStream =   
               new FileOutputStream(temp);

      int read = 0;

      byte[] bytes = new byte[1024];

      while ((read = stream.read(bytes)) != -1) {

            outputStream.write(bytes, 0, read);
            outputStream.close();

      }

Now, we have this temp file here which we can pipe to the ProcessBuilder like,

String _filePath=temp.getPath();
ProcessBuilder pb = new ProcessBuilder(url.getPath(), param2, param3)

Thank you everyone for your considerations.




回答5:


You can use Path class like :

Path path = Paths.get("data/test-write.txt");
if(!Files.exists(path)){
    // can handle null pointer exception
}


来源:https://stackoverflow.com/questions/33164102/java-lang-nullpointerexception-while-getting-a-filepath-in-java-executing-throu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!