GeeCON Prague 2014*
The moose likes Java in General and the fly likes Accessing contents of a jar file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Accessing contents of a jar file" Watch "Accessing contents of a jar file" New topic
Author

Accessing contents of a jar file

Paul Keohan
Ranch Hand

Joined: Mar 15, 2000
Posts: 411
How do you get to files within a jar file, from class files within the same jar file? For example, I want to copy a txt file into a directory on the C drive. The txt file is in a jar file. So is the java class that runs the copying mechanism using Runtime.exec().
Thanks for any help.
Paul
Graeme Brown
Ranch Hand

Joined: Oct 13, 2000
Posts: 193
There are two instance methods of java.lang.Class that will help:
URL getResource(String filename)
and
InputStream getResourceAsStream(String filename)
of course the JAR file needs to be in your CLASSPATH
Paul Keohan
Ranch Hand

Joined: Mar 15, 2000
Posts: 411
Thanks for the response but where do I go from here? What I'm attempting to do is use the URL reference and slot it's result into a copy command. So far this has not worked because it's got the wrong syntax. My url is
Graeme Brown
Ranch Hand

Joined: Oct 13, 2000
Posts: 193
Trying to use Runtime.exec() will not work because the OS can only access the jar file not what is contained within it.
You will have to read the contents of the file from the InputStream returned by getResourceAsStream(), then write the contents out to a new file using a FileWriter.
If you need more help with doing this, just post.
Paul Keohan
Ranch Hand

Joined: Mar 15, 2000
Posts: 411
Yes, I need more help. Some of these files are jar files themselves and I want to be able to copy them directly into any directory I want. I can't extract the data and rebuild it for these type of files.
Is there a way of using the File class possible?
Thanks for any help.
Paul
Graeme Brown
Ranch Hand

Joined: Oct 13, 2000
Posts: 193
Here is a simple example of how to read a nested jar file from inside another jar and copy it to the current directory. For my test nested.jar contains MS Word documents, I put nested.jar into main.jar and added main.jar to the CLASSPATH

[ February 07, 2002: Message edited by: Graeme Brown ]
Paul Keohan
Ranch Hand

Joined: Mar 15, 2000
Posts: 411
This kinda worked - at least for txt files. However, I noticed that the copied in version is always twice the size of the original version. Also, it dodn't quite work for jar files. After I did copied a jar file, it was not accessible from winzip so it's like it didn't format properly. It was twice the size of the original too.
Seeing as it's possible to run all dos commands from a java file using the Process, there must be some way to just copy a file in it's entirety without having to rebuild it byte by byte.
Graeme Brown
Ranch Hand

Joined: Oct 13, 2000
Posts: 193
Originally posted by Paul Keohan:

Seeing as it's possible to run all dos commands from a java file using the Process, there must be some way to just copy a file in it's entirety without having to rebuild it byte by byte.

If you can do something from the command line, then you can do the same thing from Runtime.exec()
I don't know enough about the command line use of jar or winzip to help.
With regards to the doubling of the file size and not being able to access the extracted file. I can't explain that. The code worked perfectly on my machine. What version of the JDK are you using? I tested on Sun JDK 1.3.1 and IBM JDK 1.2.2. with Winzip 8.0
 
GeeCON Prague 2014
 
subject: Accessing contents of a jar file