# Thread: java.io.File from Jar File

1. Member
Join Date
Feb 2011
Posts
31
Rep Power
0

## java.io.File from Jar File

Hey ppl i am struggling to get resources from a jar file.
when the my app is not complied to a jar file the following code works, however the moment i create the jar file it does not find the file, i have searched Google endlessly, but could not find the solution.

Java Code:
File chmfile=new File(this.getClass().getResource("text3D.chm").toURI());
I also tried the following, but same happens

Java Code:
File chmfile=new File(this.getClass().getResource("text3D.chm").getFile());
File chmfile=new File(this.getClass().getResource("text3D.chm").getPath());
in the end this is what i hope to achieve: 01 run the help file from the jar, 02 copy some resource files from the jar to the local hd

Please any help will be appreciated:)

2. Do you get an exception when you run your program?

3. Senior Member
Join Date
Jan 2009
Posts
671
Rep Power
5
Packaged resources can't be accessed as files, because they aren't files. Since you're trying to access it as a File, I presume you are wanting to read it in?

If so, use 'getResourceAsStream', and you can read it as you want. If you need it to be a File for some reason, you can read it in as a stream and write it back out as a temporary file. Alternately, if you want to modify it, you can use the java.util.jar package to directly read and write (not recommended if you're running from a jar to write to that same jar while running....) entries in a jar.

4. Member
Join Date
Feb 2011
Posts
31
Rep Power
0
Here is the Exception for one of the files i would like to copy to the local jre bin folder

java.io.FileNotFoundException: file:\C:\JDeveloper\mywork\text3D\text3D\deploy\t
ext3D_beta_1.jar!\text3d\jogl.dll (The filename, directory name, or volume label
syntax is incorrect)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at text3d.InstallJOGL.copyfile(InstallJOGL.java:29)
at text3d.InstallJOGL.run(InstallJOGL.java:16)
at text3d.main.<init>(main.java:12)
at text3d.main.main(main.java:36)

5. Member
Join Date
Feb 2011
Posts
31
Rep Power
0

Java Code:
    private void installjavadll(String filename) throws Exception{
InputStream in = this.getClass().getResourceAsStream(filename);
String path = System.getProperty("java.library.path").substring(0, System.getProperty("java.library.path").indexOf(";"))+ "\\" + filename;
File filejogl = new File(path);
OutputStream out = new FileOutputStream(filejogl,true);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
System.out.println("Copy Successful - " + path);
}

6. Your problem with that earlier path was that you had to change "file:" to "jar:"

It is possible to read files in from a jar file (or any zip file in that matter).
Java Code:
jar:\C:\path\to\file.jar!\file\inside\jar.txt
That's what Class.getResource() returns in URL if the file is in the local jar.

8. Senior Member
Join Date
Jan 2009
Posts
671
Rep Power
5
ra4king, yes, you can use the URL returned from getResource. What you can't do, is what the OP was trying to do, which is to create a java.io.File from that and then use the File oriented IO routines. You can only create java.io.File's from URI's that are "file:" URIs. Thats'' what the API states, and if you try to violate the API is doesn't work.

Java Code:
File file = new File(new URL("jar:\C:\path\to\file.jar!\file\inside\jar.txt").toURI());
;)

10. Senior Member
Join Date
Jan 2009
Posts
671
Rep Power
5
From the API:

Java Code:
File(URI uri)
Creates a new File instance by converting the given [B]file:[/B] URI into an abstract pathname
It must be a "file:" URI, not a "jar:" URI.

You can successfully create a File object from any String, but that object will not be of use in the File io routines if it is not a type the OS considers a file. Please try it and verify for yourself that it doesn't work, since you don't seem to believe it.

11. Which is why i did "new URL("...").toURI" ;)

12. Senior Member
Join Date
Jan 2009
Posts
671
Rep Power
5
ra4king, *it will not work*. Please try it.

Create a file junk.java with the following contents:

Java Code:
import java.io.*;
import java.net.*;

public class junk {

public static void main(String[] args) throws Exception {

URL url = new junk().getClass().getResource("junk.class");

System.out.println(url);

File file = new File(url.toURI());

System.out.println(file.exists());

}

}
javac junk.java
jar cf junk.jar junk.class
java -cp junk.jar junk

...here's what happens:

Java Code:
jar:file:/C:/cygwin/home/dad/junk.jar!/junk.class

Exception in thread "main" java.lang.IllegalArgumentException: URI is not hierarchical

at java.io.File.<init>(Unknown Source)

at junk.main(junk.java:12)
The reason for the error, is that the URI is not a "file:" URI. Now try running it as:

java junk

The result is then as expected, because "junk.class" is now a "file:" uri. Here are my results (which depend on your file structure):

Java Code:
file:/C:/cygwin/home/dad/junk.class

true

13. Huh, you're right I just did some testing and "toURI()" doesn't work with a jar file.
Quick question: since I've never used URI before, what does it mean? How does it differ from URL?

EDIT: Actually, with further experimenting, that exception happens because File can't handle the "!/junk.class" part, aka it is not part of the normal hierarchical file system. Not URI's fault :/
Last edited by ra4king; 04-22-2011 at 05:38 AM.

14. Senior Member
Join Date
Jan 2009
Posts
671
Rep Power
5
File can't handle "jar:", because "jar:" != "file:" and File only works with "file:" URI's. It's right there in the API.

15. 666th post! Hahaha. Well thanks you devil you!

EDIT: So if we need any file from a jar, we just avoid File and stick with URL :)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•