Results 1 to 15 of 15
Thread: java.io.File from Jar File
- 04-20-2011, 11:16 PM #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.
I also tried the following, but same happensJava Code:File chmfile=new File(this.getClass().getResource("text3D.chm").toURI());
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 hdJava Code:File chmfile=new File(this.getClass().getResource("text3D.chm").getFile()); File chmfile=new File(this.getClass().getResource("text3D.chm").getPath());
Please any help will be appreciated:)
- 04-21-2011, 02:50 AM #2
- 04-21-2011, 05:26 AM #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.
- 04-21-2011, 09:35 AM #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)
- 04-21-2011, 10:20 AM #5
Member
- Join Date
- Feb 2011
- Posts
- 31
- Rep Power
- 0
Thanks toadaly..
i used your advice and found a solution
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); }
- 04-21-2011, 06:30 PM #6
Your problem with that earlier path was that you had to change "file:" to "jar:"
- 04-21-2011, 06:32 PM #7
@toadaly
It is possible to read files in from a jar file (or any zip file in that matter).
That's what Class.getResource() returns in URL if the file is in the local jar.Java Code:jar:\C:\path\to\file.jar!\file\inside\jar.txt
- 04-21-2011, 10:52 PM #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.
- 04-22-2011, 12:05 AM #9
Ah ok. How about:
;)Java Code:File file = new File(new URL("jar:\C:\path\to\file.jar!\file\inside\jar.txt").toURI());
- 04-22-2011, 03:56 AM #10
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
From the API:
It must be a "file:" URI, not a "jar:" URI.Java Code:File(URI uri) Creates a new File instance by converting the given [B]file:[/B] URI into an abstract pathname
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.
- 04-22-2011, 03:58 AM #11
- 04-22-2011, 04:14 AM #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:
javac junk.javaJava 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()); } }
jar cf junk.jar junk.class
java -cp junk.jar junk
...here's what happens:
The reason for the error, is that the URI is not a "file:" URI. Now try running it as: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)
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
- 04-22-2011, 04:32 AM #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 04:38 AM.
- 04-22-2011, 04:47 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.
I'm being evil about this, because this is my 666th post.
- 04-22-2011, 06:53 AM #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 :)
Similar Threads
-
how to split large xml file into small xml file in java
By enggvijaysingh@gmail.com in forum XMLReplies: 2Last Post: 02-07-2011, 09:34 AM -
how to read openproj(Projity) file i.e. ,POD file(Project Management file)
By mahendra.athneria in forum New To JavaReplies: 0Last Post: 02-11-2009, 09:53 AM -
To open an image file such as Jpeg file using JAva Program
By itmani2020 in forum Advanced JavaReplies: 10Last Post: 07-11-2008, 09:57 AM -
How to parse the CSV(Comma separation values)file and validate the file using java
By padmajap13 in forum Advanced JavaReplies: 7Last Post: 05-23-2008, 03:46 AM -
How to read a text file from a Java Archive File
By Java Tip in forum Java TipReplies: 0Last Post: 02-08-2008, 09:13 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks