Creating and running a Jar file

2019-09-14 11:45发布

问题:

I am a beginner in java programming. I have created a package called Comp Project which contains two classes....

  1. ATM: It has various functions to carry out actions of atm screen.
  2. Main: This class calls all the functions from ATM and executes them.

Now the program is running good and I have to create a jar file. I created a Jar file with the main class selected as Main. But when I try to run it....it doesn't work. The problem is that i don't understand what creating a manifest is...I looked onto other posts but i did not understand anything.

Can anyone tell me the steps to follow for creating a proper Jar file and Running it?? I need both as I don't know where i am going wrong.

Update: I am using BlueJ.

回答1:

Create a manifest.txt file.

Main-Class: fully.qualified.class.name of your main class

Uses Main-Class as the entry point of this Jar file, when you double click on this Jar file, the “Example.class” main() method will be launched.

And then on command line

jar -cvfm Example.jar manifest.txt your-package-structure/*.class

Hope this helps.



回答2:

You can use jar cf yourCompAppJar.jar Main.class to create your jar file, if you have JDK 6 you can use the cfe to specify the entry point.


If you are using Eclipse :

  • Right-click the project > click Export > select "Runnable JAR file" under the Java folder > In the launch configuration select the class name > choose where to save your jar > finish.