[JavaSE] How does a Java program run?

》》》My blog homepage
》》》My gitee link
Follow me and make progress together on the road to learning Java! ! !

Everything



Preface

In order for a Java program to run, the JDK must be installed on the computer first. You can click on the link to view the installation stepsJDK installation

Java program running steps

There are 3 steps required to run a Java program on your computer:

1. Write Java programs

We can use tools for editing text documents (such as Notepad, Notepad++) to write Java programs. Since this file records the source code of Java, change the suffix of the file to.java
Everything

Everything
Open the HelloWorld.java file in Notepad or Notepad++ and write a program to output Hello World!.
Everything
Everything

2. Compile Java files using javac command

The purpose of compiling Java files is to:Translate source code files (.java files) that the JVM (Java Virtual Machine) cannot understand into bytecode files (.class files) that the JVM can understand. The specific details of JVM will be mentioned when running the class file.
To compile the file, you need to use javac.exe in the JDK bin file
The bin file stores various tool commands, the more important one is javac.exe and java.exe
javac.exe is the one provided by JDKcompileTool, used to compile .java files
java.exe is one provided by JDKrunTool used to run .class files. How to run it will be mentioned in Running class files.

Everything

Everything

Everything
How to use javac.exe file to compile .java file? Execute in cmd.
Steps:

  1. Click above the .java file that needs to be compiled,
    Everything
    Everything
    Enter cmd and press Enter
    Everything
    At this time, the .java file is directly displayed in cmd The path where it is located, and then enter the command in cmd to operate the .java file
    Everything
    But the javac.exe file path is on the C drive. How to use the javac.exe file directly in cmd to compile the .java file? ? At this point the role of configuring environment variables appears. You can click on the link to view the steps for configuring environment variablesJDK installation

The meaning of configuring environment variables

Although when installing the JDK, we have been helped to configure the environment, including the four tools java.exe, javac.exe, javaw.exe and jshell.exe, but if you want to use other tools in the JDK bin file, you still have to Configure environment variables manually.
Everything

The purpose of configuring environment variables:Facilitates the operating system to find and execute specific executable files or commands.
When you enter a command on the command line, the operating system will find and execute the corresponding executable file based on the configured environment variables. If the environment variables are set correctly, you can execute the command from any path without specifying the full file path.

For example, if I want to compile the HelloWorld.java file that has been written above, the javac.exe file needs to be used for compilation. Enter javac directly in the path of the HelloWorld.java file, followed by the file name and file name that need to be compiled in the current file path. .java suffix
Everything
Press Enter to execute. After compilation is completed, the HelloWorld.class file will appear above the HelloWorld.java file.
Everything
The content of the .class file is bytecode, which is the intermediate form between source code and binary machine code. At this time, cantExecuted on specific operating systems and hardware platforms.
Insert image description here

3. Use the java command to run the Class file

The operation of C language is different from that of Java.

C language files become binary machine files after compilation. The operating system and hardware platform can directly run the compiled C language files;

After Java is compiled, it is a class bytecode file. Bytecode is an intermediate form between source code and binary machine code. The operating system and hardware platform cannot directly run the compiled class file. In this case, you need to passimplementin JDKjava.exe command startIn Java Runtime Environment JRE (Java Runtime Environment)The Java Virtual Machine (JVM) passes a bytecode file as input. The JVM will load, interpret and execute the bytecode, converting it into machine instructions for a specific operating system., and finally achieve the effect of running class files.

When running the class file in cmd, do not write the .class suffix after it.
Everything
Everything

The relationship between JDK, JRE and JVM

JDK: Java Development Kit is a Java development tool kit, including JRE and development tools. The development tools include javac, java, jdb...
JRE: Java Runtime Environment is a Java runtime environment. Contains:

  1. JVM: Java Virtual Machine is a Java virtual machine, where Java programs run.
  2. Core class library: This is the standard class library of JavaSE. It is something that has been written in Java and can be used directly.
  3. Running tools: used to execute Java programs and manage Java applications.
    Everything

The principle of Java cross-platform operation

Different JDKs are installed for different operating systems. After the Java files are compiled with javac, the JVM of the corresponding operating system is used to interpret and run the class files. Finally, the same Java source code can be run under different operating systems. It is in line with Java’s slogan: Write once, Run anywhere
Everything

What is the purpose of integrated development environment (IDE)?

IDE (Integrated Development Environment, integrated development environment)
Compiling, running, debugging and other operations on cmd are troublesome after all.
The integrated development environment is a development tool that integrates multiple functions such as writing code, compiling, running, and debugging, etc., which improves the development efficiency of programmers. Programmers only need to focus on writing code and running it. The result will be fine.
For example, if I have written a program that outputs Hello world!, I just need to click Run, and the integrated development environment will automatically compile and run it for me and get the running results.
Everything
Everything
Finished.


Guess you like

Origin blog.csdn.net/weixin_73276255/article/details/131885621