0

I am trying to read an xml configuration in the below class.It is Working when Running It .But When i Export it into a Runnable Jar.The file is not being read ..??

 public KeyTranslator() {
                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                String sPath = "res/config.xml";



     //InputStream is = (InputStream) Thread.currentThread().getContextClassLoader().getResourceAsStream(sPath);
    //InputStream is = this.getClass().getResourceAsStream((sPath);

                try {
                        DocumentBuilder builder = factory.newDocumentBuilder();
                        if (AppFrame.jar == null) {
                                this.myDoc = builder.parse( new File((AppFrame.basePath+sPath).replace('\\', '/')) );//ensure seperator is '/' as linux is picky
                        } else {
                                this.myDoc = builder.parse(AppFrame.jar.getInputStream(AppFrame.jar.getJarEntry(sPath)));
                        }
            }

I googled & found the getResourceAsStream method.But it seems to throw FileNotFoundException.& i Don't know how to add InputSream in my Code ???

So Help me in the Right Direction :)

Thanks for your Help ...

Note The Method i Tried has been Commented

AruLNadhaN
  • 2,808
  • 5
  • 24
  • 42

3 Answers3

0

But it seems to throw FileNotFoundException

The most likely reason is that the pathname to the file is incorrect.

You seem to be using "res/config.xml" as your pathname. That is a relative pathname, and relative pathnames are resolved relative the current directory; e.g. the directory where you were when you tried to launch the JAR.

(Note that it doesn't actually matter if you use "/" or "\" as the path separator. Java's built-in classes for opening files can cope with either form, irrespective of the runtime platform ...)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Because the current directory is different. Do you know what "current directory" actually means? – Stephen C Jan 18 '14 at 04:42
  • See the Image.Edit the Code & post .I can't get you..Pls – AruLNadhaN Jan 18 '14 at 05:10
  • You didn't answer my question. If you are going to get this to work this way, you NEED to understand relative paths and the current directory. The other alternative is to access the file via the classpath and `Class.getResource()` or `Class.getResourceAsStream()`. Read the javadocs for more information. – Stephen C Jan 18 '14 at 08:12
0

Read it from classpath - makes it very flexible in terms of configuring path. Here is how: How to really read text file from classpath in Java.

BTW to avoid replacing backslashes and slashes to run on Win and Unix use File.separator. For instance: "res" + File.separator + "config.xml". You can also use new File("res", "config.xml").

Community
  • 1
  • 1
yǝsʞǝla
  • 16,272
  • 2
  • 44
  • 65
0

Atlast Found how to use getResourceAsStream.The Below line does the Magic

this.myDoc = builder.parse((getClass().getResourceAsStream("/xml/config.xml")));

Thankz for all the Answers .....

AruLNadhaN
  • 2,808
  • 5
  • 24
  • 42