1

I am trying to create a JAR file via cmd line, but am having trouble with compiling it relative to a JAR file created using Eclipse.

To use the command line version, I do the following:

jar cf "myjar.jar" A.java B.java C.java

In the directory that I run that command from, only the three files exist.

When I build it from Eclipse, I right click on the three selected files. Then do: Export -> JAR file -> Finish (compress contents of JAR).

The size difference is very noticeable, 19 KB (cmd) versus 43 KB (Eclipse). When I try to use the command line created JAR, my application fails. My guess is that the command line created JAR does not include all necessary dependencies (outside of the three classes).

Any help on what I am doing wrong would be greatly appreciated!

jpints14
  • 1,281
  • 6
  • 15
  • 24
  • A jar is a zip file. You can examine the contents with winzip, etc to see what the difference is. – Sanj Apr 22 '15 at 14:16
  • *When I try to use the command line created JAR, my application fails.* -> Any error message when creating the archive or starting the program? Please provide more information. – Turing85 Apr 22 '15 at 14:16
  • What dependencies do you have? What is on your classpath? What error message do you see when you run the jar from the command line? What is inside each jar (view it with the jar tf command or with a zip file viewer like 7zip)? – Kevin Workman Apr 22 '15 at 14:17
  • You should also set the main class when running from cmd: java -cp myjar.jar com.example.yourMain – Dagriel Apr 22 '15 at 14:22

2 Answers2

2

The tool JAR of JDK is only a ZIP compressor then when you try:

jar cf "myjar.jar" A.java B.java C.java

You really are creating a zip with 3 java source code, you aren't compiling the code.

You must do:

javac *.java
jar cf "myjar.jar" *.class

When you generate it with Eclipse the IDE is assuming that you want compile and package the generated class files in a JAR. Additionally there is a MANIFEST.MF generated that can differ from command line and Eclipse

EDIT: If you want to execute an Application with multiple JARs check this: Setting multiple jars in java classpath

Community
  • 1
  • 1
Ernesto Campohermoso
  • 6,844
  • 1
  • 34
  • 50
  • This seems to be the correct answer. I'm using the Apache Commons Lang API, and I have the JAR file. Is there a way to include that dependency in the javac command? Sorry, fairly new to Java. – jpints14 Apr 22 '15 at 14:32
  • You can't include a JAR inside other JAR, you must deliver your app with both jars and configure the classpath. Check this: http://stackoverflow.com/questions/219585/setting-multiple-jars-in-java-classpath – Ernesto Campohermoso Apr 22 '15 at 14:37
  • The classpath can be defined for Compilation time and Execution time. You need the for both. That is the right way. – Ernesto Campohermoso Apr 22 '15 at 14:45
  • The 3 classes won't compile without the Apache jar. The way I'm currently running the app is as follows: java -cp "C:\myjar.jar;C:\someother.jar" com.EntryPoint. I can't use myjar.jar if the code is not compiled, but I can't compile it without the Apache jar. Am I supposed to run the app like this: java -cp "C:\apache.jar;C:\myjar.jar;C:\someother.jar" com.EntryPoint without compiling my three classes first? – jpints14 Apr 22 '15 at 14:45
1

Did you compile your .java files?

When building a .jar file you have to include .class files which are compiled java files.

See the Documentation for information on building a jar file: https://docs.oracle.com/javase/tutorial/deployment/jar/build.html

Eclipse will compile the sourcecode in the background and include the compiled version in your jar-file. Eclipse also offers a option to include the source which makes a difference in file size.

PhilippS
  • 1,662
  • 3
  • 12
  • 19
  • As I mentioned in a previous comment to Ernesto, an Apache jar is required to build the other 3 classes. Do I just jar the .java files, and when I call "java -cp" I include the Apache jar? – jpints14 Apr 22 '15 at 18:39