How can I create a .jar file?

前端 未结 6 1107
谎友^
谎友^ 2021-01-15 15:33

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 wha

相关标签:
6条回答
  • 2021-01-15 16:17

    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.

    0 讨论(0)
  • 2021-01-15 16:19

    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.

    0 讨论(0)
  • 2021-01-15 16:26

    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.

    0 讨论(0)
  • 2021-01-15 16:33

    You can create a .jar file, zipping the folder with the compiled classes and renaming the file.

    For instance, if your class files are located in the "game" folder, zip it to game.zip, and rename it to game.jar

    This is really simple and it works!

    0 讨论(0)
  • 2021-01-15 16:37

    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/

    0 讨论(0)
  • 2021-01-15 16:37

    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.

    0 讨论(0)
提交回复
热议问题