Preface
In this article, we will learn how to use IDEA to create, run and package our first Hello World!
Java program. After reading this tutorial, I hope you can have a simple understanding of how to use IDEA to create, run and package Java programs, familiarize yourself with some simple use skills of IDEA, and feel the convenience that IDEA brings to our developers. Next Let's take a look at the specific creation, operation and packaging process.
Create a Java project
First, let's take a look at how to use IDEA to create a Java project. The following is the specific creation process.
- Open the main interface of IDEA, and then click
New Project
.
- Then select the
Java
option on the left, and then set the project'sJDK
. Here, if you have installedJDK
, IDEA will usually detect it automatically. If it is not detected, then selectAdd JDK …
, and then find the path where youJDK
, And finally selectNext
at the bottom.
- Choose whether to create from a template. IDEA comes with a template by default. If you want to create based on a modified template, then check
Create project from template
. By default, we don’t check it, which is to create an empty project. Here, clickNext
enter the next step.
- Set the project name and project storage path. Here you can customize it according to your needs. Here we take
HelloWolrd
as an example. After setting the project name and project storage path, you canFinish
- At this time, our project is created, and the structure of the created project is shown in the figure below.
Write code
Although our project has been created, there is nothing in it. At this time, we need to write our code.
- First, we right-click the
src
folder in the project tool window, then selectNew
, and then selectJava Class
(you can also directly use the shortcut keyAlt + Insert
).
com.cunyu1943.helloworld.HelloWorld
in theName
field and press Enter. Then thecom.cunyu1943.hellowold
package andHelloWorld
class are created.
package com.cunyu1943.helloworld;
/**
* Created with IntelliJ IDEA.
*
* @author : zhangliang
* @version : 1.0
* @project : HelloWorld
* @package : com.cunyu1943.helloworld
* @className : HelloWorld
* @createTime : 2021/9/14 9:53
* @email : 747731461@qq.com
* @公众号 : 村雨遥
* @website : https://cunyu1943.github.io
* @description :
*/
public class HelloWorld {
}
- Then enter
main
and select the statement of themain()
method, and then enter the following code toHello World!
main()
package com.cunyu1943.helloworld;
/**
* Created with IntelliJ IDEA.
*
* @author : zhangliang
* @version : 1.0
* @project : HelloWorld
* @package : com.cunyu1943.helloworld
* @className : HelloWorld
* @createTime : 2021/9/14 9:53
* @email : 747731461@qq.com
* @公众号 : 村雨遥
* @website : https://cunyu1943.github.io
* @description : Hello World 类
*/
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Compile and run the project
After the code is written, we first compile the code, select the compile button in the toolbar (you can also use the shortcut key Ctrl + F9
), and the project will be compiled automatically.
If there are no errors in the compilation, then we can continue to run the project. Click main()
method, and the project will start (you can also use the shortcut key Ctrl + Shift + F10
.
Or click the green triangle arrow in the toolbar to run (or use the shortcut key Shift + F10
), but the whole project is running at this time, and the above method is to run only the main method in the current class.
Then you can see the output of our program in the console.
Package the project as a JAR application
File -> Project Structure
in turn, enter the project structure (or use the shortcut key Ctrl + Shift + Alt + S
), and then enter and select Project Settings -> Artifacts
.
+
upper left corner, then select JAR -> From modules with dependencies
, then enter Create JAR from Modules
, set Main Class
to HelloWolrd
, and finally click ok
.
After confirming and apply
, the project is packaged and configured at this time, and then select Build -> Build Artifacts
menu bar, then click HelloWorld:jar
and select Build
, and then wait for the packaging to be completed.
Then in the out -> artifacts
directory of our project, you can find HelloWorld.jar
, and the packaging of our project is now complete.
Run the JAR application
Now that we have packaged the project, we don't need to run our project in IDEA at this time. Find our packaged HelloWorld.jar
, and then run the following command in the console.
java -jar HelloWorld.jar
You can see that Hello Wolrd!
also printed out, indicating that our project was successfully packaged and it ran smoothly.
Summarize
That's it for today's content. This article mainly talks about how to use IDEA to create, compile, run, and package our Java projects. If you have more tips on using IDEA, please leave a message in the comment area.
It is not easy to create, if you think this article is helpful to you, then come to a wave of likes and attention.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。