1

I need to read the file, that is inside the jar folder in this way:

 DataSource coverdata = new FileDataSource(new File("here is the path"));

How do I get the exact path? I am using a library, so the only way I can acces file is new File(), no inputstreams.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
artouiros
  • 3,947
  • 12
  • 41
  • 54
  • Um , seems to be a duplicate of this one: http://stackoverflow.com/questions/6639/how-should-i-load-files-into-my-java-application – home Jul 24 '11 at 08:04
  • @Arthur Shniv *"`DataSource`"* Can you vague that up for me? Is that a `javax.activation.DataSource`, a `javax.sql.DataSource`, or something else? *"I am using a library, so the only way I can acces file is new File(), no inputstreams."* Sounds like you need to get them to fix their API. Either that or use a different one. – Andrew Thompson Jul 24 '11 at 09:04

3 Answers3

3

Call ClassLoader.getResourceAsStream()::InputStream or ClassLoader.getResource()::URL.

See:

http://download.oracle.com/javase/1.5.0/docs/api/java/lang/ClassLoader.html#getResource(java.lang.String)

http://download.oracle.com/javase/1.5.0/docs/api/java/lang/ClassLoader.html#getResourceAsStream(java.lang.String)

The JAR file containing the requested file must be in the classpath.

home
  • 12,468
  • 5
  • 46
  • 54
1

You are on the wrong way. File is only something that is directly written on file system. File inside jar is not a file from java API point of view.

To read this file you have to parse JAR using either JarInputStream or ZipInputStream, get to the interesting entry and read from the input stream. It is not hard to do but you have to write several lines of code.

Other approach is using VFS package from jakarta.org. It provides you unified view on elements of different files systems, ZIP, TAR and other container formats.

AlexR
  • 114,158
  • 16
  • 130
  • 208
0

What about creating a new temporal file, with the InputStream (getResourceAsStream() ) methods that @home already talked about, and then reading it as a normal file?

Vicente Plata
  • 3,370
  • 1
  • 19
  • 26