My problem is getting some CSS files from a foo.jar file. How can I get these CSS files from foo.jar? I found some ways, firstly by unzipping this .jar and copying files and then by deleting the unzipped folder. Is that a good way?
-
What is it you are trying to achieve? Do you want to use the CSS at runtime? Do you want to make updates to them? Do you want steal them for you own world domination purposes? – MadProgrammer May 19 '14 at 04:56
-
you can have a look at this http://stackoverflow.com/questions/1429172/how-do-i-list-the-files-inside-a-jar-file – user3470953 May 19 '14 at 04:56
-
Are you trying to get resources out of your application's *own* jar file, or some *other* jar file that isn't on the application's classpath? – Wyzard May 19 '14 at 05:02
-
I must generate .html file in another destination. So, I need to copy .css files. – javaKid May 19 '14 at 17:52
4 Answers
My JAR file location is C:\foo.jar
public static void main(String[] args) throws IOException {
{
URL url = getClass().getClassLoader().getResource("css/demo.css");
URL url2 = getClass().getClassLoader().getResource("");
System.out.println("url = " + url);
System.out.println("url_2 = " + url2);
}
}
Output:
url = jar:file:/C:/foo.jar!/css/demo.css
url_2 = file:/C:/
So, I don't need to extract foo.jar. I can copy files within JAR like this way. Thank you.

- 59
- 2
- 8
The following example will make you clear Let's extract some files from the TicTacToe JAR file we've been using in previous sections. Recall that the contents of TicTacToe.jar are:
META-INF/MANIFEST.MF
TicTacToe.class
TicTacToe.class
TicTacToe.java
audio/
audio/beep.au
audio/ding.au
audio/return.au
audio/yahoo1.au
audio/yahoo2.au
example1.html
images/
images/cross.gif
images/not.gif
Suppose you want to extract the TicTacToe class file and the cross.gif image file. To do so, you can use this command:
jar xf TicTacToe.jar TicTacToe.class images/cross.gif
This command does two things:
It places a copy of TicTacToe.class in the current directory. It creates the directory images, if it doesn't already exist, and places a copy of cross.gif within it.
The original TicTacToe JAR file remains unchanged.
As many files as desired can be extracted from the JAR file in the same way. When the command doesn't specify which files to extract, the Jar tool extracts all files in the archive. For example, you can extract all the files in the TicTacToe archive by using this command:
jar xf TicTacToe.jar

- 854
- 8
- 15
Put that jar into the class path of the project. Then you can access the resources you needed.

- 184
- 5
The basic command to use for extracting the contents of a JAR file is:
jar xf myjarfile.jar;
for more info please check this link out :
http://docs.oracle.com/javase/tutorial/deployment/jar/unpack.html
Hope this will help.

- 2,228
- 3
- 20
- 39