Preparation materials:
1.java file:
Helloworld.java
Package Com.ray; Public class helloworld{ publicstaticvoid main (string[] args) { System.out.println ("Hello!jar");} }
2. Specify the directory: Create a directory to hold the. class file and the. jar file. This article specifies that the directory is E:\myclass.
I. Command-line Form
1. Compile the Java file into the specified directory:
The syntax format is: javac-d Specify directory java files
Compile to e:\myclass:javac-d E:\myclass Helloworld.java
Character encoding can be specified if there is a garbled exception: Javac-encoding UTF-8-D e:\myclass Helloworld.java
2. Specify Main-class:
After the compilation is complete, the COM folder is generated under the MyClass directory. At this point, in the MyClass file directory, build a file a (such as 1.txt).
File content: Main-class: (space) Full class name (carriage return)
1.txt file content is: Main-class:com.ray.helloworld return
3.cmd into the specified directory (E:\myclass), packaged with the jar command
Syntax format: JAR-CVFM (jar name) file The top-level directory of a package
C M F
Parameter C: Specifies that a new archive file is created.
Parameter V: generates verbose output in standard output, which is optional.
Parameter f: Specifies the package name after packaging.
Parameter m: Specifies a custom MANIFEST.MF manifest file, otherwise the JDK automatically generates a default manifest that does not contain main-class.
This example: JAR-CVFM my.jar 1.txt com
With the above three steps, the My.jar is created under the E:\myclass directory. Of course, these three steps can also be omitted as appropriate.
Another: Attach the command to run the jar package in cmd: Java-jar jar package name, such as Java-jar My.jar
Two. In Eclipse
1. Packaged as a generic jar package
(1) To package Items right-click on the export-> java-> JAR File->next
(2) in the Jar File Specification window, set the file name and location of the package, click on both sides next
(3) in the Jar Manifest Specification window, set the configuration of the MANIFEST.MF manifest file,
If only packaged into a simple jar package, do not make any changes, take the default can
If packaged as an executable jar package, you can use an existing manifest file or select the main class directly
(4) Click the Finish button to complete the package.
2. Packaged as a runnable jar package
(1) To package the item right-click on the->export->java->Runnable JAR File->next
(2) in the Runnable JAR File Specification window, select Launch configuration and export destination
(3) Click the Finish button to complete the package.
Java to package the program into a jar package