头图

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.

  1. Open the main interface of IDEA, and then click New Project .

  1. Then select the Java option on the left, and then set the project's JDK . Here, if you have installed JDK , IDEA will usually detect it automatically. If it is not detected, then select Add JDK … , and then find the path where you JDK , And finally select Next at the bottom.

  1. 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, click Next enter the next step.

  1. 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 can Finish

  1. 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.

  1. First, we right-click the src folder in the project tool window, then select New , and then select Java Class (you can also directly use the shortcut key Alt + Insert ).

  1. com.cunyu1943.helloworld.HelloWorld in the Name field and press Enter. Then the com.cunyu1943.hellowold package and HelloWorld 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 {
} 
  1. Then enter main and select the statement of the main() method, and then enter the following code to Hello 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.


村雨遥
52 声望6 粉丝

我是 村雨遥,欢迎来到我的思否主页。