People really develop Java applications at work using Eclipse,myeclipse, IntelliJ, etc.
But it's still worth 10 minutes to learn how to compile and execute Java programs using the original command-line format
Use the original command-line method to execute Hello World
Step 1: First look at the running effect
Running the first Java program under the console, you can see that the string is entered
Hello World
Step 2: Prepare the project directory
You usually create a project directory in E:
In this case, 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 to Helloworld.java
In the file, typing the following code
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 a class
Class HelloWorld says the name of this class is HelloWorld
public static void Main (string[] args) This is the main method, the entry of all code
If you have learned C language, the equivalent of _main program entry
System.out.println ("Hello World"); Represents the output of a string in the console (black screen) "Hello World"
public class helloworld{public
static void Main (string[] args) {
System.out.println ("Hello World");
}
Step 4: Compile
The. java file is a Java source file, but cannot be run directly, and must be compiled into a. class file before it can be executed.
Java uses the Javac command to compile
Grammar:
Javac Filename.java
Note: must keep up. java suffix name
As shown in the figure, execute the following command:
Run Win+r, enter cmd into console interface
E: Switch letter to E disk
CD E:\PROJECT\HOW2JAVA\SRC switch directory to source file directory
Javac Helloworld.java Compile command javac filename case must be consistent
If you get a blank line, you're successful, and you get a class file: Helloworld.class
Step 5: Run
The Java Run command is
Java classname
As shown in the figure, execute the following command:
Run Win+r, enter cmd into console interface
E: Switch letter to E disk
CD E:\PROJECT\HOW2JAVA\SRC switch directory to source file directory
Java HelloWorld
Note that case needs to be consistent and there is no suffix name. class
You will see the string "Hello World" when you run successfully
Step 6: About classes
Java all the code is running in the class
public class HelloWorld
Public means this is a class that can be publicly accessed
class represents this one
HelloWorld represents the name of the class, capitalizing the first letter of each word
public class helloworld{
}
Step 7: Main method
public static void Main (string[] args)
You write a lot of code, there's always the first line of code to execute, and that's the main method.
Args represents the run 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");
Will output string in console
PRINTLN represents printing data to the console, and wrapping lines
System.out.println ("Hello World");