Everyone will use eclipse, myeclipse, IntelliJ and so on when they are really developing java applications at work
But it is still worth spending 10 minutes learning how to compile and execute java programs using the most original command line format
Use the most original command line method to execute Hello World
Step 1: First look at the operation effect
Run the first Java program under the console and you can see that the string is entered
hello world
Step 2: Prepare the project directory
Usually, we will create a project directory in e: In this example, we use the e:/project/how2java directory as the project directory. All java code will be placed in this project directory and create a directory src for storing java source code.java files.
Step 3: Create the first java source file
Create the first java source file. Right-click on the desktop -> New-> Text file to get a file "New text document.txt"
Right-click the file -> Rename it to HelloWorld.java
Type the following code into this file
public class HelloWorld{ public static void main(String[] args){ System.out.println("hello world"); }} Because java is an object-oriented programming language, we are actually creating classes
class HelloWorld means that the name of this class is HelloWorld
public static void main(String[] args) This is the main method, the entry to all codes
If you have learned C language, it is equivalent to the _main program entry
System.out.println("hello world"); indicates that a string "hello world" is output on the console (black screen)
public class HelloWorld{ public static void main(String[] args){ System.out.println("hello world"); }} Step 4: Compile
The .java file is the source file of java, but it cannot be run directly. It must be compiled into a .class file before it can be executed.
java compile syntax using javac command:
javac filename.java
Note: Must keep up with the .java suffix name
Execute the following command as shown in the figure:
Run win+r, enter cmd to enter the console interface
e: Switch the drive letter to e drive
cd e:/project/how2java/src Switch directory to source file directory
javac HelloWorld.java compilation command javac file name must be consistent in case
If you get a blank line, it means that success is achieved and a class file is obtained: HelloWorld.class
Step 5: Run
The Java run command is
java classname
Execute the following command as shown in the figure:
Run win+r, enter cmd to enter the console interface
e: Switch the drive letter to e drive
cd e:/project/how2java/src Switch directory to source file directory
java HelloWorld
Note that the case needs to be consistent and there is no suffix name.class
After successful operation, you will see the string "hello world"
Step 6: About Class
All java code runs in the class
public class HelloWorld
public means this is a publicly accessible class
class means this class
HelloWorld represents the name of the class, with the first letter of each word capitalized.
public class HelloWorld{ } Step 7: Main method
public static void main(String[] args)
You can write a lot of code, there is always the first line of code to execute, this is the main method
args means the running parameter, which is not used in this example.
public static void main(String[] args){ System.out.println("hello world");} Step 8: Console output
System.out.println("hello world");
Strings will be output on the console
println means print data to the console and wrap the line
System.out.println("hello world");