简体   繁体   中英

How exactly do I compile my Java into a JAR file?

So I'm trying to compile my game for my Object-Oriented Programming class into a jar file so it can be ran with java -jar javacoffeeadventure.jar .

My folder structure for a folder with java files removed looks like this:

audiomanager/
commandinterpreter/
gamemanager/
io/
logic/
META-INF/
player/
resources/
rooms/
main.class

Everything is packaged under javacoffeeadventure . For example, the main file is javacoffeeadventure.main . The META-INF folder contains one MANIFEST.MF file that I tried to edit and make the jar invoke main.class 's main() method:

Manifest-Version: 1.0
Created-By: 1.8.0_60 (Oracle Corporation)
Main-Class: javacoffeeadventure.main

I know I use jar to compile into a jar file, but how would I use that command to create a jar file that is able to begin with javacoffeeadventure.main ? Is my manifest wrong?

as a slight by-the-way, jar puns are funny to me if you guys have any. :)

In my experience, the following has worked, but if you utilize it, you may need to adapt your directory structure.

First, your folder structure needs to be of the form 'containing_folder.com.example.package', where your classes are in the 'package' folder. You need to then place your manifest.mf file in the uppermost directory ('folder'). The manifest file should be as follows:

 Manifest-Version: 1.0
 Main-Class: com/example/package/javacoffeeadventure

including a carriage return after the second line.

From the initial folder compile with the following command:

jar cmvf manifest.mf javacoffeeadventure.jar com/example/package/*.class

making sure that beforehand you've compiled the classes in your package (use *.java)

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM