tags:

views:

53

answers:

5

In the tutorial I found out that jar files can be created in the following way:

jar cf jar-file input-file(s)

However, it was not clear what are the input-file(s). Is that .java files or .class files? From the examples on the same page I can assume that should be .class files.

But now it is not clear which .class files should I put there. After the compilation of .java files I have a lot of .class files. One of the reason of that is that I have a lot files like that: GameWindow$2$10class, GameWindow$2$7.class and so on. Should I include all of them into the command line for the creation of the .jar file?

To run my application I use java Game. So, my be I need to use only Game.class file when I create a .jar file? On the other hand other class files corresponds to classes used by the application.

My software use external libraries (during compilation I specify .jar files of these libraries). Will .jar file of my application be able to run on the computer which does not contain the .jar file of used library?

+1  A: 

The Jar-file could contain any file you want, but to hold a Java-program you need at least the .class-files, so you have to include them. The Game-class you are talking about may be dependant on the other classes. You can check that: delete all .class-files and recompile only Game.java (javac Game.java). All other classes that are compiled are a dependency. All of these have to be included in the Jar-file, that your program can be run. The class-files generated, that have not a corresponding .java-file (i.e. your GameWindow$2$7.class) are inner anonymous classes, in your example a inner class of the class GameWindow. If other libraries are needed, these must be present on other computers, that your program can be run. But you can include the content of the other jars into your jar, so that all that is needed is bundled into one file.

Mnementh
+1  A: 

The .jar file must contain all your classes that are needed during runtime. That includes the GameWindow$2$10 classes, they are the anonymous inner classes that you wrote in your GameWindow class.

Other .jar files are not included in your .jar file, but you can reference them using the Class-path attribute in your manifest.

Joachim Sauer
+4  A: 

However, it was not clear what are the input-file(s). Is that .java files or .class files? From the examples on the same page I can assume that should be .class files.

Yes, you need to include the class files.

I have a lot files like that: GameWindow$2$10class, GameWindow$2$7.class and so on. Should I include all of them into the command line for the creation of the .jar file?

Yes, these are from inner classes; you need them as well.

To run my application I use java Game. So, my be I need to use only Game.class file when I create a .jar file?

No, class Game will use other classes, which in turn use others. You need them all.

Will .jar file of my application be able to run on the computer which does not contain the .jar file of used library?

No.

That said, creating a JAR manually is a good learning experience, but not something you'd really do in practice.

You should probably look into automating you JAR building. Check out e.g. Ant: http://ant.apache.org/

sleske
+ for "good learning experience". Make sure you automate it **after** you know what you're doing. Don't try to do it now!
Joachim Sauer
+1  A: 

regarding your external dependencies, this will NOT work unless you compile in the dependencies as .class files, or use something like FatJar http://fjep.sourceforge.net/ to add them into one big jar.

oedo
A: 

You can use a wild card to add the classes in the current directory into the JAR-file.

jar cf mynewjar.jar *.class

When you compile your source file into byte code, all classes inside that source will be generated as separate .class files, so unless your game.java has more than the Game class, the game class would be sufficient.

Shyam