The Java virtual machine

A virtual machine is an abstract computer that simulates various computer functions on a real computer. Java virtual machines have their own complete hardware architecture, such as processors, stacks, registers, etc., as well as corresponding instruction systems. The Java Virtual machine masks information specific to a particular operating system platform, allowing Java programs to run unmodified on multiple platforms by generating object code (bytecode) that runs on the Java Virtual machine.

In fact, I did not understand this mechanism when I first started Java, but only understood some features of JVM when I learned the transition from Java SE to Java EE.

When I just started learning Java, I learned it from Java SE

1.2.1 Three Technical Architectures of the Java Language J2EE(Java 2 Platform Enterprise Edition) Enterprise Edition provides a set of solutions for developing Enterprise applications. The technologies contained in the system, such as Servlet Jsp, are mainly aimed at Web application development. The Java 2 Platform Standard Edition (J2SE) is a solution for developing common desktop and business applications. This technical architecture is the foundation of the other two and can complete the development of some desktop applications. Take the Java version of Minesweeper. J2ME(Java 2 Platform Micro Edition) is a solution for developing consumer electronic products and embedded devices. The technology system is mainly applied to small electronic consumer products, such as applications in mobile phones. After Java5.0, the name is changed to JAVAEE JAVASE JAVAME

Java language features: Cross-platform

What is cross-platform? Applications written in the Java language can run on different system platforms.

How does it work? Simply install a Java Virtual Machine (JVM) on the operating system that needs to run Java applications. The JVM is responsible for running Java programs on the system.

Features of the Java language: Cross-platform



Because of the JVM, the same Java program can run on three different operating systems. This enables Java programs to be cross-platform. Also known as Java has good portability.

What is the JRE and JDK? The Java Runtime Environment (JRE) includes the Java Virtual Machine (JVM Java Virtual Machine) and the core class libraries required by Java programs. If you want to run a developed Java program, You only need to install JRE on your computer. JDK(Java Development Kit) The Java Development Kit (JDK) is provided for Java developers. It contains Java Development tools, including JRE. So if you install the JDK, you don’t need to install the JRE separately. Development tools: compilation tools (javac.exe), packaging tools (jar.exe) and other simple: Java programs developed using JDK, handed to JRE to run.

Install the JDK

Foolproof installation, next step.

Suggestion: The installation path does not contain Chinese characters or special characters such as Spaces.

If you are prompted to install JRE, do not install it.

Configure the environment variable path(1)

An error message is displayed when you type javac in the DOS command line.

Error Principle: If the currently running program does not exist in the current directory, The Windows operating system looks for the program in the directory specified by an existing environment variable named Path. If not, the above error message appears.

So enter into

Run javac in JDK installation directory \bin. The JavAC parameter prompt is displayed

Information.



Configure the environment variable path(2)

Every time a Java tool executes, it has to go into the bin directory, which is very cumbersome.

Is there a tool that can execute Java in any directory?

According to the principle that The Windows system is looking for executable programs, you can define the path of Java tools into the path environment variable, so that the system can help us to find the program to run. Configure the environment variable path(3)

My computer – Properties – Advanced – Environment variables

Edit the path environment variable by starting the value of the variable with the directory where the Java tools are located followed by “;” Separate it from other values.

Open the DOS command line and type javac in any directory.

If javAC parameter information is displayed, the configuration is successful.

This configuration, once and for all.

Configure the environment variable path(4)

Temporary configuration: Run the set command in the DOS command. • Set: Used to view information about all environment variables of the local host. • Set variable name: View the value of a specific environment variable. • Set variable name = : Clears the value of an environment variable. • Set variable name = Concrete value: Defines a concrete value for the specified variable.

Want to add a new value to an existing environment variable? • First, get the value of the original environment variable through the % variable name % operator. • Set path= new value • Set path= new value; %path%

Note: This configuration only works in the current DOS window. The window is closed and the configuration disappears.

Configuration tips

You can use the following techniques to avoid misoperating the value of the path variable by constantly changing the value of the path variable due to JDK directory or directory name changes. • Create a new environment variable JAVA_HOME to record the JDK directory. • Obtain the value of JAVA_HOME dynamically by %% in path. • JAVA_HOME = F: \ jdk1.6.0 _01, path = % JAVA_HOME % \ bin; % PATH % • %path% : Dynamically obtains the value of the PATH environment variable. • %JAVA_HOME% : Dynamically retrieves the value of the environment variable named JAVA_HOME.

Java application development Experience – Hello World

  1. Write Java code to a file with a. Java extension. 2. Run the javac command to compile the Java file. 3. Run the generated class file using Java commands. For details, please refer to Hello World Flowchart.doc

Java program development Experience – writing

Step 1: Write • Select the simplest editor notepad. • Save the file as demo.java. This file is the Java code file, called the source file.

Java program development experience – compile

Step 2: Compile • With the Java source file, compile it into a file that the JVM can recognize. • Use the Javac compilation tool to compile the Demo. Java document in the source directory. • If the program has no errors, there is no warning, but a demo. class file appears in the current directory. This file is called a bytecode file, which is also an executable Java program. Java program development Experience – Run

Step 3: Run • Have an executable Java program (demo.class bytecode file) • Run the utility java.exe to execute the bytecode file. • A suggestion appears that a method named main is missing.



Public static void main(String[] args){} public static void main(String[] args){} Execution in progress. • Found that no compilation failed, but it had no effect because the JVM was not told what to do for us, i.e., there was no specific statement to execute. • To interact with the JVM, add a sentence • system.out.println (” Hello Java “) to the main method; In recompile, just run.

Configure the classpath (1)

Since class files (bytecode files) are Java runtime files, is it possible to execute a class file in any directory?

Following the principles of the PATH environment variable, you can define an environment variable called classpath in which the directory of the class file to be run is defined.

Example: set the classpath = c: \

The clasSPath variable value is the directory for Java class files

The path variable is the directory for Windows program files

Configure the classpath (2)

The order in which the JVM looks for class files: • If the CLASSPath environment variable is not configured, the JVM only looks for class files to run in the current directory. • If the CLASSPath environment is configured, the JVM first looks for the class file to run in the directory of the value of the classpath environment variable. • If a semicolon is added to the end of the value, then the JVM does not find the specified class file in the classpath directory and will look it up again in the current directory. If there is no semicolon at the end of the value, then the JVM does not find the specified class file in the classpath directory, does not look for it in the current directory, and does not run it even if it exists in the current directory. • Suggestion: When configuring the CLASspath environment variable, do not end the value with a hyphen (+). Use “if you need to access the current directory. Said.

Create problems and solutions



The source file name does not exist or is incorrectly written, or the current path is incorrect.



The class file name is incorrectly written, or the class file is not in the current path, or the classpath is no longer in the specified path.

Create problems and solutions



Compile failed, note the number of lines in error, and type the error at the specified location in the source code.

The Java Development Kit (JDK) includes the JRE (Java Runtime Environment), and the JRE contains the JVM, so when installing the JDK on Windows, This means that the JVM will also be installed on Windows.

Java is cross-platform because of the JVM mechanism, which is the underlying tool. The JVM varies from platform to platform (primarily operating system), so it serves as the underlying tool for portability on other operating systems.

Because of the JVM, the same Java program can run on three different operating systems (Windows Linux MacOS). This enables Java programs to be cross-platform. Also known as Java has good portability.