This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Well, first of all the recommended URL syntax for files is more like this:
Backslashes MAY work, but they may also cause problems, since in Java, the primary use for a backslash is as an escape character.
"files" within JARs and other archives (such as ZIP) are not really files. The JAR itself is a file, but most OS filesystems don't support the ability to open up and access an archives contents directly.
There are some Java support functions that permit an extended filename syntax that allow access (typically read-only access) to members of a JAR, but the standard java.io file functions can't do it.
Your sample code appears to be attempting to open an input stream on a classpath resource. If the jar is part of the app's classpath, that's OK, but the "filename" would be relative to the classpath. You can't use an absolute filesystem path here, and I doubt that Windows-style resource paths (using backslashes) will work either.
An IDE is no substitute for an Intelligent Developer.
Joined: Mar 11, 2011
Thanks for the answer Tim.
I didnt put in the backslashes manually. They were added through the url...
I verified the following in my ant classpath
If I hover the mouse over then the Eclipse pop up lists the jar as a part of the path element.
For this point, I assume that the jar is in my classpath, right?
Thus, in the code I tried to access my file as relative path to the jar like
I doubt that the base directory is what you think it is. After creating the File object, print out its full path using file.getAbsolutePath(). Also be aware that the base directory from within Eclipse might well be very different from the base directory outside of eclipse.
Peter Johnson wrote:I doubt that the base directory is what you think it is. After creating the File object, print out its full path using file.getAbsolutePath(). Also be aware that the base directory from within Eclipse might well be very different from the base directory outside of eclipse.
Seems that you're right since file.getAbsolutePath() points to some place I did not expect.
But how can I get a text file from within a jar. I tried it with previously example (getResource()) but failed as well.
getResource() is the best way to do this, but you have to realize that the string you pass to getResource() must identify the location of the text file relative to the base of the JAR file. That is, if the text file is in the base of the JAR, then you use "xxx.txt". If the text file is in the org directory within the JAR, then use "org/xx.txt".
Joined: Mar 11, 2011
just solved it with the following piece of code:
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com