• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Ant jar classpath

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
yusuf Kaplan
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
yusuf Kaplan
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,

just solved it with the following piece of code:

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic