0

I found this post How to copy files out of the currently running jar but because I am new to stackoverflow I can't comment there. The above post is mostly what I need but I receive an error while running it. I am also new to java and this error I believe is something simple but I can't figure it out. Here is my java code:

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class Test2 {

public static void main(String[] args) {

    InputStream dllStream = Test2.class.getClassLoader().getResourceAsStream("/resources/jacob-1.18-x86.dll");
    System.out.println(dllStream);

        try {
            FileOutputStream fos = new FileOutputStream("somelib.dll");
            byte[] buf = new byte[2048];
            int r=0;
            while(-1 != (r = dllStream.read(buf))) {
                fos.write(buf, 0, r);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

}

}

Here is a capture of my Eclipse resource folder + code:

Here is the error I'm receiving :

null

Exception in thread "main" java.lang.NullPointerException at Test2.main(Test2.java:23)

I know that I am receiving this error because something is null. My dllStream is null but I don't know why.

Please someone help this noob ^^^

Community
  • 1
  • 1
  • 1
    Do you have the correct path to the file to be copied? The API doc says: Returns: A InputStream object or null if no resource with this name is found – NormR Dec 07 '16 at 16:01
  • Hi @NormR. I think the path is right. You can check this capture [link](https://i.stack.imgur.com/tBh2L.png) to see where my file is. – Mihai Lucian Dec 07 '16 at 17:31
  • I used getClass() instead of getClassLoader() and it worked. – Mihai Lucian Dec 07 '16 at 18:52

0 Answers0