My bulk JAVA: Maven version Hello World

Posted by kotun on Fri, 04 Feb 2022 13:37:11 +0100

preface

There is a lot of writing ahead, and there is no CTRL+Z

Less nonsense, or put this "Hello World!" Finish it.

Java's "Hello World!" As follows:

class HelloWorldApp {
    public static void main(String[] args) {
        System.out.println("Hello World!"); // Display the string.
    }
}

This code is copied from the official tutorial of Java. You can go to the official website of Oracle, which has a more detailed description:

https://docs.oracle.com/javase/tutorial/getStarted/cupojava/index.htmlhttps://docs.oracle.com/javase/tutorial/getStarted/cupojava/index.html

Download IDE

Most "Hello World!" Tutorials start with Notepad, but for Java development, I think we can start directly from ide. Of course, IDE is not the official NetBeans of Oracle. It does not mean that NetBeans are not good, but there are few users. It is not recommended to use Eclipse or IntelliJ IDEA Community Edition. This article uses the Spring toolsuite, the Spring customized version of Ecipse.

 Spring | Toolshttps://spring.io/tools

IDE comes with JDK, which can concentrate on learning grammar. Novices don't have to worry about the configuration of annoying environment.

Write code

1. Start the program and select the workspace.

2. Create a project, menu File - > New - > Project

3. In the project wizard, the first step is to select Maven project, the second step is to check simple project, and the third step is to fill in Group ID and articlefact ID casually (helloworldapp is filled in here), and then click finish.

4. The default file code of the project is the system code, generally GBK, and the default JRE is 1.5. These two items are changed. The coding is changed to UTF-8, which is more general. JRE is changed to 1.8, which is in line with the mainstream development version.

Double click to open pom.com in the project on the left XML, add these lines between < project > < / Project >:

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<maven.compiler.source>1.8</maven.compiler.source>
		<maven.compiler.target>1.8</maven.compiler.target>

	</properties>

The complete contents are as follows:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>helloworldapp</groupId>
  <artifactId>helloworldapp</artifactId>
  <version>0.0.1-SNAPSHOT</version>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<maven.compiler.source>1.8</maven.compiler.source>
		<maven.compiler.target>1.8</maven.compiler.target>
	</properties>
</project>

CTRL+S to save, and then right-click on the project node,

Select the root node of the left project tree, press ALT+5, and click OK in the pop-up window. You can see that the JRE version has changed to 1.8.

5. Write code.

Select the menu file - > New - > class, fill in the class Name (Name): HelloWorldApp in the pop-up window, and check public static void main(String []args)

Add this line to the generated code:

System.out.println("Hello World!");

The complete code is as follows:

package helloworldapp;

public class HelloWorldApp {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("Hello World!");
	}

}

So far, "Hello World!" The code of is completed.

function

Press CTRL+F11, select Java Application in the pop-up window, and click OK.

Then a line Hello World! Appears at the bottom of the IDE!, This is the output of our program.

A little explanation

1. class

Class # is a basic code unit of Java, including program entry, which should be written in class. The definition of a class is that the class keyword is followed by the class name, and the interior of the class is wrapped with a pair of {}.

2. main method

The main method is a fixed form. Just remember to write it like this, and in actual projects, you won't directly contact the main method. Even if there is, you may only need to write a little code, such as Spring boot:

@RestController
@EnableAutoConfiguration
public class MyApplication {

    @RequestMapping("/")
    String home() {
        return "Hello World!";
    }

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }

}

3,System.out.println(args)

Console output is also a fixed form, which means printing and wrapping.

4. How do Java programs run?

summary

"Hello World!" in this article, It's not just a starter program, but also creating a maven based project template. Maven is a comprehensive tool integrating class library management and program construction.

Topics: Java Eclipse