Home >Backend Development >Python Tutorial >Java Official Notes: Writing and Running Java Programs
You may be eager to install Java, write a Java program and run it. But before that, there are some concepts that need to be understood in advance, because Java is a little different from C, C and Python.
We write English codes in text files. Computers cannot understand these English codes, so we need to do some conversion to a format that computers can recognize and run. This conversion is completed by the "compiler". Some languages do not have compilers, but Java does.
The file converted by the compiler is usually called a binary file, or executable file. The file content is changed from English to bytecode. Only computers can understand bytecode, not humans, so we don't need to care. We just need to ensure that the English code we write can be compiled correctly. Let the computer do the execution.
Java code files end with .java, and Java executable files end with .class.
All code in the Java file must be placed in "Java Class":
public class MyFirstClass { }
The file name must be called MyFirstClass.java, the file name It must be consistent with the class name. Why is Java designed to be so strict? Convenient, it allows you to know what the class name is when you see the file name, without having to guess.
How to compile class files? Download Java.
Downloading Java means downloading JDK, Java Development Kit, which contains a Java compiler to compile MyFirstClass.java into MyFirstClass.class.
The so-called JRE, Java Runtime Environment, is part of the JDK and can only be used to run Java programs and cannot be used to compile.
Download address: https://jdk.java.net/
After installation, you need to set the environment variables JAVA_HOME and PATH according to the operating system (Windows, Linux, macOS).
Verify that the installation is successful:
java -version
Use the javac command to compile:
javac MyFirstClass.java
If there is a problem with the code, an error will appear. If there is no problem, the MyFirstClass.class file will be generated.
Add code:
public class MyFirstClass { public static void main(String... args) { System.out.println("Hello, World!"); } }
Recompile, ensure that the class file is generated, use the java command to run:
java MyFirstClass
Run successfully, control The station will print a sentence: Hello, World!
Question 1: javac HelloWorldApp, error:
Class names, 'HelloWorldApp', are only accepted if annotation processing is explicitly requested
javac HelloWorldApp.java The compilation command "javac" should be followed followed by "file name".
Question 2: java HelloWorldApp.class, error:
Could not find or load main class HelloWorldApp.class
java HelloWorldApp The running command "java" should be followed by "class name".
You can command Help to see the difference:
javac Usage: javac <options> <source files>
java Usage: java [-options] class [args...] (to execute a class) orjava [-options] -jar jarfile [args...] (to execute a jar file)
When developing complex applications, javac and java commands are not used directly, but IDE, Integrated Development Environment, integrated development environment, such as Eclipse, NetBeans and IntelliJ IDEA.
Reference: Getting Started with Java https://dev.java/learn/getting-started
The above is the detailed content of Java Official Notes: Writing and Running Java Programs. For more information, please follow other related articles on the PHP Chinese website!