Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Null pointer exception when attempting to load file from inside jar file

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Null pointer exception when attempting to load file from inside jar file

    I have an XML file I want to parse and the parser I am using takes java.io.File. The XML file itself needs to be within the jar file and I have it in a folder called resources. The class I'm calling it from is in a package called test so I'm using ../ to go up a package first being going into the resources folder/package where the file is located. The below code successfully works when I debug it within Netbeans however when I deploy this as a jar file the input stream ends up being null. I extracted the content of the jar file and it does indeed have the XML file correctly located in resources folder so it is there but the below code doesnt find it I guess. Any ideas?


     
    package test;
     
    private void myMethod() {
     
                InputStream inputStream = MyClass.class.getResourceAsStream("../resources/file.xml");
     
                File file = File.createTempFile("tmp", "tmp");
     
                try (OutputStream outputStream = new FileOutputStream(file)) {
                    int read = 0;
                    byte[] bytes = new byte[1024];
                    while ((read = inputStream.read(bytes)) != -1) {
                        outputStream.write(bytes, 0, read);
                    }
                }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,113
    Thanks
    52
    Thanked 2,369 Times in 2,323 Posts

    Default Re: Null pointer exception when attempting to load file from inside jar file

    the parser I am using takes java.io.File
    Is that the only interface to the parser?

    when I deploy this as a jar file the input stream ends up being null
    Sounds like you need to find the correct path to use in the getResourceAsStream method call. Try a bunch of different paths to see if you can find the file.

    the XML file correctly located
    What is the full path to the file?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Null pointer exception when attempting to load file from inside jar file

    The parser is part of Java which is javax.xml.parsers.DocumentBuilder. It can take an input stream directly looking at it but either way when ran that input stream is null.

    I tried a few paths and it cannot find it. When I extract the jar I have two folders one called test with the class in and the other called resources with the XML file in.

    --- Update ---

    getClass().getResourceAsStream("/resources/file.xml") was the magic

Similar Threads

  1. JEditorPane doesn't load html file when jar executable run
    By dancaer69 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: November 12th, 2013, 09:46 AM
  2. image does not load to .jar file after been compile
    By azuma kazuma in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 26th, 2013, 08:34 AM
  3. run jar file inside a jar file?
    By ufrubnuckle in forum Java Theory & Questions
    Replies: 1
    Last Post: February 21st, 2013, 05:58 AM
  4. Run a jar file inside a batch file with Windows 7 Task schduler
    By kingnachi in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 15th, 2012, 09:20 AM
  5. Replies: 1
    Last Post: January 13th, 2012, 08:02 AM