aspose file tools*
The moose likes Ant, Maven and Other Build Tools and the fly likes Ant jar classpath Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Ant, Maven and Other Build Tools
Bookmark "Ant jar classpath" Watch "Ant jar classpath" New topic
Author

Ant jar classpath

yusuf Kaplan
Ranch Hand

Joined: Mar 11, 2011
Posts: 59
Hi all,

got the following output after executing an ant script:
" java.io.FileNotFoundException: file:\C:\...\ The filename, directory name, or volume label syntax is incorrect"

I try to access a file within a jar. The path to the file in the error description is definitely valid and exists.
The code looks like this


Any ideas?
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16305
    
  21

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.


Customer surveys are for companies who didn't pay proper attention to begin with.
yusuf Kaplan
Ranch Hand

Joined: Mar 11, 2011
Posts: 59
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

But ant still fails with
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5845
    
    7

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.


JBoss In Action
yusuf Kaplan
Ranch Hand

Joined: Mar 11, 2011
Posts: 59
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.
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5845
    
    7

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".
yusuf Kaplan
Ranch Hand

Joined: Mar 11, 2011
Posts: 59
Hi there,

just solved it with the following piece of code:

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Ant jar classpath