38

I have .class and .java files in JAR archive. Is there any way to extract only .java files from it? I've tried this command but it doesn't work:

jar xf jar-file.jar *.java
user3521479
  • 565
  • 1
  • 5
  • 12

3 Answers3

49

From the source:

To extract only certain files from a jar file, supply their filenames:

C:\Java> jar xf myFile.jar foo bar

Using wildcards is a shell thing, and you should not expect it to work when extracting from a JAR file (which, as you've realized, is the case).

What you can do, is supply a list of the files you want to extract, via the @ modifier:

jar xf my.jar @myfiles.lst

where myfiles.lst would be:

com/my/MyClass.java
com/my/MySecondClass.java

This file could easily be created automatically with creative use of jar tvf, awk and your desired extraction pattern. I'll leave that as an exercise and/or another SO question :-)

Or you could use unzip - a JAR file is basically a zip-file.

Cheers,

Community
  • 1
  • 1
Anders R. Bystrup
  • 15,729
  • 10
  • 59
  • 55
41

You can use the unzip command which accepts wildcards in its arguments (which is not the case of the jar command). Something like this should do the trick ( disclaimer : not tested)

unzip youFile.jar "*.java"
Pierre Rust
  • 2,474
  • 18
  • 15
-6

There shouldn't be any *.java files in a jar, only class files. You can check the content of the jar first by running:

jar tvf jar-file.jar
Adrian Ng
  • 4,204
  • 1
  • 11
  • 3