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 5 of 5

Thread: Cannot read files while running program from JAR-file

  1. #1
    Member
    Join Date
    Apr 2013
    Posts
    43
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Cannot read files while running program from JAR-file

    Hello!

    I have a program that is a XML-parser, and it works fine when I'm running it from NetBeans. But when I create a JAR-file and run the very same program, it cannot find the xml file. Consider this small program that addresses my problem:

    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;
    import org.w3c.dom.Document;
    import org.xml.sax.SAXException;
     
    public class XMLTest {
     
            Document d;
     
            public XMLTest() throws ParserConfigurationException, FileNotFoundException, IOException, SAXException {
     
                String docString = "C:\\someXML.xml";
                DocumentBuilderFactory dbf;
                dbf = DocumentBuilderFactory.newInstance();
     
                dbf.setValidating(true);
                dbf.setIgnoringElementContentWhitespace(true);
     
                DocumentBuilder db = dbf.newDocumentBuilder();
     
                InputStream in = new FileInputStream(docString);
     
                d = db.parse(in);
            }
     
     
        public static void main(String[] args) throws ParserConfigurationException, FileNotFoundException, IOException, SAXException {
     
            XMLTest t = new XMLTest();
        }
    }

    Hank


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Cannot read files while running program from JAR-file

    But when I create a JAR-file and run the very same program, it cannot find the xml file.
    I presume the code throws an exception? Please post the full error message/stack trace.

  3. #3
    Member
    Join Date
    Apr 2013
    Posts
    43
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Cannot read files while running program from JAR-file


  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Cannot read files while running program from JAR-file

    Please copy and paste the error. The image you posted is unreadable.

  5. #5
    Member
    Join Date
    Apr 2013
    Posts
    43
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Cannot read files while running program from JAR-file

    Yes, seems like i forgot the DTD-file. You can close this thread.

    --- Update ---

    Thank you for your time!

Similar Threads

  1. Replies: 21
    Last Post: June 12th, 2013, 11:33 AM
  2. Running .JAR file issue.
    By JosPhantasmE in forum What's Wrong With My Code?
    Replies: 12
    Last Post: January 27th, 2013, 07:07 AM
  3. [SOLVED] files size for files inside the JAR file
    By Natherul in forum Java Theory & Questions
    Replies: 3
    Last Post: February 9th, 2012, 03:17 PM
  4. Running an Applet With JAR file
    By rameshiit19 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 20th, 2011, 07:10 AM
  5. [SOLVED] jar file Built(clean and build) perfectly, but not running as jar
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 18
    Last Post: July 11th, 2011, 11:41 AM