1. Write source code
Write the source file: CardLayoutDemo.java and save it, for example: I:/myApp/CardLayoutDemo.java. The program structure is as follows:
package test;import java.awt.*;import javax.swing.*;//Import more packages...class NotePadFrame extends JFrame {//Design of the main interface...}//Other related code...public class CardLayoutDemo { public static void main(String[] args) { new NotePadFrame(); }}2. Open the terminal
Open the system menu: "Start" -> "Run..." -> Enter "cmd" to open the terminal.
Run the following command to go to the directory where the source file is located:
cd myApp
3. Compile source files
Enter the following command to compile:
javac CardLayoutDemo.java
After successful execution, a series of .class files will be generated in the current directory.
4. Create a new package folder
Enter the following command to create a new package folder:
mkdir test
You can also create this folder in Explorer.
This folder must be exactly the same as the package name defined in the source file (package test;).
5. Put the class file in a new folder
Move all compiled .class files to the newly created test folder in Explorer.
6. Test whether the program can run normally
Enter the following command in the I:/myApp path:
java test.CardLayoutDemo
If the program can be run normally, you can proceed to the next step. Otherwise, the correctness of the above steps should be checked.
7. Generate executable jar packages
Enter the following command in the I:/myApp path:
jar -cef test.CardLayoutDemo CardLayoutDemo.jar test
The above commands and parameters are as follows:
The jar command is a dedicated packaging tool that comes with Java;
c means to generate a new jar package;
e represents an executable class, that is, the class where the main method is located. When writing, add the package name, in this case, the subsequent test.CardLayoutDemo;
f represents the name of the generated jar package, in this case CardLayoutDemo.jar. This package name can be named at will, without any regulations;
This parameter at the end of the test indicates that all files in the test directory are packaged into a new jar package.
8. Test whether the jar package can run normally
Enter the following command in the I:/myApp path:
java -jar CardLayoutDemo.jar
If the program can be run normally, it means that the jar package is successfully created.
If you have associated "Java(TM) Platform SE binary" to the .jar file type (it will be correctly associated after normal installation of jdk), you can also execute such jar packages by double-clicking, which feels the same as the exe file.
The above article uses the command line to compile Java and generate executable jar packages is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.