This is the 10th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021
This blog will try to teach you how to package ordinary Java projects into JARS with IDEA and run them.
-
If your IDEA is Chinese, you can see “Ordinary Java project package Jar package and run (Chinese IDEA)”.
-
If your IDEA is in English, you can see the “Ordinary Java project Jar package and run (English IDEA)”.
-
If you want to localize your IDEA, you can check PyCharm to download the installation and localization part of your IDEA. The localization methods and packages of PyCharm and IDEA are universal.
Package normal Java projects into JARS and run them (IDEA)
Take the Bank project as an example.
First import the project into IDEA. If an error is reported after import, the normal phenomenon can be solved.
Click “File” → “Project Structure”.
Click [Artifact] → [+] → [JAR] → [From module with dependencies].
Click “Folder icon” → “OK” next to “Main Class”. If your project is a multi-module project, you can set modules to select which modules to package.
Select the main class that contains the main method and click OK.
Select extract to target JAR and click OK.
Here, we can change the name of the generated JAR package and the output location; Default can be; Click “Apply” → “OK”.
Click [Build] → [Build Artifact].
Click Build.
We wait for the build progress bar below to complete.
After the build is complete, you can find our Jar package in the project window.
Normal Java projects packaged as Jar packages and running (IDEA)
Take the Bank project as an example.
First import the project into IDEA. If an error is reported after import, the normal phenomenon can be solved.
Click [File] → [Project Structure…] .
Click on 【Artifacts】→【+】→【JAR】→【From modules with dependencies… .
Click “Folder icon” → “OK” after “Main Class”. If your project is a multi-module project, you can select modules to package by setting Module.
Select the main class that contains the main method and click OK.
Select extract to the target JAR and click OK.
Here, we can change the name of the generated JAR package and the output location; Default can be; Click [APPLY] → [OK].
Click Build → Build Artifacts… .
Click Build.
We wait for the Build progress bar below to complete.
After the build is complete, you can find our generated Jar package in the Project window.
Run the Jar package
Take the packaged bank. jar package as an example.
-
Go to our bank.jar package directory.
-
Enter CMD in the path bar and press Enter
-
Enter the command Java -jar bank. jar and press Enter to view the result.