Java path-Environment configuration and compilation and running, and java-based environment Compilation

Source: Internet
Author: User

Java path-Environment configuration and compilation and running, and java-based environment Compilation

Outline

I. Opening

Ii. Download JDK

Iii. JDK Installation

Iv. Environment Configuration

V. Java Compilation

6. Java and Javac

7. The first Java program

VIII. Summary

IX. References

I. Opening

After learning about the road to Java, I believe that beginners have a deep understanding of Java. However, things cannot always stay at the theoretical level, and they have to be implemented in order to see the truth. In my opinion, to learn something, we must follow a routine of theory-practice-theory. First, understand the basic concepts of things, and then try to use things to deepen the theoretical memory and discover your own blind spots. In repeated practices, we will gradually have our own views and ideas and deepen our theories. Finally, return to the theory and summarize your own things so that things really belong to you. So today, we will start to configure the Java environment and compile and run the program using the configured Java environment. It should be noted that, in order to allow everyone to have a deeper understanding of how Java works and how to compile and run it, this chapter does not involve the use of any IDE. encoding is of course the use of notepad. IDE improves the daily development efficiency, but the notebook can increase the admission opportunity for everyone!

Ii. Download JDK

As mentioned in the road to Java-the beginning of Java, in addition to the JRE (Java Runtime Environment), corresponding development tools are also required for Java development, so we need to download JDK. Download JDK can go to Oracle Official Website: https://www.oracle.com/, choose Downloads -- Java for Developers, as follows:

Next, Select download JDK. The latest version is 8u121.

If you want to get the previous version, you can pull the page to the bottom and select Java Archive to get the previous Java version.

Here we will download the latest 8u121 for subsequent instructions.

Iii. JDK Installation

After downloading the corresponding version according to the system environment, you can install it. There is nothing special about the entire installation process. You can simply click Next by default. If necessary, you can modify the corresponding directory.

A dialog box is displayed in the middle, indicating the directory installed by JRE. Select the Directory and click "Next.

After the installation is successful, if you want to see the API documentation, you can click its "subsequent tutorial ".

So far, JDK has been successfully installed.

Iv. Environment Configuration

How can I use it after the installation is successful? You can open the command console, go to the bin directory where you installed the JDK directory, and execute the java command. If there are a lot of prompts, it proves that you have installed it correctly.

Some may encounter the following situation:

Why? You may find that the current directory is different. As I said at the beginning, we need to execute the java command under the bin directory where you installed the JDK directory to make the command take effect. The reason is very simple, because the bin directory contains a variety of commands, java is one of them. Many java commands, such as javac and javaw, are used in the bin directory. Therefore, you can learn the tools in the bin directory in the future, there are many useful tools in it.

To answer this question, it is inconvenient to run java commands in the bin directory. It is not only inconvenient, but many other programs cannot use java commands, because they do not know where to find the java command. At this time, we need to configure environment variables for the system to facilitate the execution of java commands in any directory.

Select Start> Computer> Properties> advanced system Settings> environment variables to open the environment variable settings window.

Set the following three variables:

  • 1. Added JAVA_HOME. The value is the JDK path. Here, it is C: \ Program Files \ Java \ jdk1.8.0 _ 121.
  • 2. Edit: PATH, and add "; % JAVA_HOME % \ bin; % JAVA_HOME % \ jre \ bin" at the end of the original variable value ". Here is a brief description. % JAVA_HOME % in the value indicates the value of the newly added variable JAVA_HOME. It can be seen that the first newly added variable JAVA_HOME does not need to be written too long for convenient PATH variable configuration. The PATH variable itself means that windows will find the command to be executed in the directory listed in the PATH variable value, so if you configure the PATH, java commands do not need to be executed in the bin directory of the JDK directory. The reason for the above two directories is that the commands used by java in the bin are also some tools.
  • 3. Added: CLASSPATH with the value ".; % JAVA_HOME % \ lib \ dt. jar; % JAVA_HOME % \ lib \ tools. jar ". CLASSPATH is used to let java programs know where to find the Java source file or bytecode file (CLASS file) during compilation (when using javac ). Dt. jar is a class library about the runtime environment, and tools. jar is a tool library, which is required for compilation and running. It is worth noting that there is a "." at the beginning of the entire string value, which indicates the meaning of the current directory.

After these three points are configured, run the java-version command in the user's directory. Then we can see that cmd can automatically find and run the java program.

V. Java Compilation

Before developing our first Java program, let's take a rough look at the entire Java code compilation and execution process. We often see files with these two Suffixes in the Java project:. java and. class. These two types of files represent the source code and bytecode files of Java respectively, and the bytecode files are the key to java's implementation of "Write Once, Run Anywhere. Let's take a look at the following two figures [1].

The Java compiler compilation process is as follows:

The JVM execution engine executes Java bytecode:

From the above two figures, we can see that the Java program runs in two steps. The first step is to compile the source code into bytecode, and the second step is to compile the bytecode into the target code. This is different from directly compiling C and C ++ into machine-related target code. Through the bytecode link, you can release the compiled package to any machine with a JVM environment, and then the JVM will compile the final target Code related to the machine, to achieve "Write Once, Run Anywhere" without binding to a specific running platform. So we usually call code compilation, which is the process of compiling Java source code into JVM bytecode.

6. java and javac

As we have already said in the previous section, a large number of Java tools are available in the % JAVA_HOME % \ bin directory, and we will become familiar with it later. Which of the following should we be familiar with as we first came into contact with java? It is undoubtedly java and javac.

The pace of today's society is too fast, and many people are more or less eager for quick success. It is easy to develop. This is mainly because I didn't understand how the program operates at the underlying layer. I started IDE development as soon as I got started. After various problems, I didn't know how to handle them because I didn't understand the underlying principles. I personally think that using IDE can greatly improve our production efficiency, but the underlying principle must be understood. Otherwise, I don't know how IDE talks about compiling, packaging, and running your program, the problem will not be solved. It is often seen that some people see that there is no IDE on the server and there is no way to run the jar package program.

Back to normal, let's start with java and javac and learn how to compile and run a java program in the command line. First, we understand how these two commands are used.

Java command:

Role: used to execute classes or execute jar files.

Enter java press enter in the cmd console. The format for running the java command is as follows:

There is no complete screenshot. The option (-options) is later. You can run the java command on your own. We temporarily ignore the content of the option (-option), mainly including the required "class" and "jar File ". You can see that java commands can directly run classes and jar files. For example, if a HelloWorld. class file contains a HelloWorld class, the command to run is java HelloWorld. Note that java is not followed by the class file (HelloWorld. class), but the corresponding class (HelloWorld ). If there is a jar file of HelloWorld. jar, run the following command: java-jar HelloWorld. jar.

Javac command:

Purpose: Compile the. java file.

Enter javac In the cmd console and press Enter. The format for running the javac command is as follows:

We temporarily ignore the option content. We can see that the simplest format of javac is a source code file. For example, if there is a HelloWorld. to compile a java file, run javac HelloWorld. java, you can generate a HelloWorld under the current directory. class bytecode file.

7. The first Java program

After learning about the java and javac commands, we can start the first Java program. Of course, we use NotePad to start our first Java program. Microsoft has few built-in Notepad functions, and the user experience is not very good. I personally prefer Notepad ++. You can choose your favorite Notepad based on your habits.

Part 1:

Create a HelloWorld class and save it to the HelloWorld. java file.

On the cmd console, run the javac HelloWorld. java command to compile the file. The HelloWorld. class file is automatically generated.

Then run the java HelloWorld command to display Hello World. Note that java HelloWorld. class is not run, and the class to be run is followed by HelloWorld.

This completes the entire process from code writing, compilation, and running, isn't it easy? In fact, all complicated things start with simplicity. As long as the foundation is established, it is easy to learn the upper layer.

Part 2:

Someone may ask, what if I want to reference other jar packages or classes? Don't worry. Let's try it. First, create a User class (just create a class at will, so don't worry too much). It has a method to express the content, which exists in the User. java file. The Code is as follows:

Modify the HelloWorld class with the following code:

The User. java is stored in the otherclass Directory, which is not in the same directory of HelloWorld. java:

Then we try to use the above method for compilation and will find the prompt that the User class cannot be found. This is because javac does not know where to find the User class.

In these cases, we must check the help prompt to see what parameters javac can provide. This is not only to solve the current problem, but also to solve more and more problems in the future, as long as you can do the opposite, there is no problem to get you.

We can see that the-classpath parameter can be added to specify the File Location of the class to be searched, so that we can specify the User location for javac, so we can write it:

HelloWorld. java and User. java are compiled successfully, and HelloWorld. class and User. class are generated in the corresponding directory.

At this time, we can execute java HelloWorld to check whether the result can be returned, but unfortunately, the User class is not found:

A clever person may think that java should also have a-classpath parameter. Congratulations! You're starting to get started. Let's try it:

No, crash !! Well, don't worry. java is a little different from javac. java uses the-classpath parameter to find classes only in the directory where classpath is located. So if HelloWorld is in the current directory, add the current directory, that is, add ". ", as follows:

This operation is successful.

Part 3:

At this time, some people are upset again. Why is it so troublesome to execute such a large string of things every time you compile and run the program? If there are multiple classes, wouldn't it be a long time to lose a command, is there any convenient way for future implementation? Of course, this is to package all classes into an executable jar package, and then run the jar package directly. The command used here is the jar command. Let's first look at its format and content:

Here we mainly use the following four options:

-C. Create a new archive file

-V generates detailed output in standard output

-F specifies the archive file name

-E is an independent application bound to an executable jar file.

Application entry point

Use the following command to package User. class and HelloWorld. class into the HelloWorld. jar package according to the prompted format.

This generates a HelloWorld. jar file. If you want to see what is in HelloWorld. jar, you can drag the jar package to the compression software such as winrar to see the specific content.

We can see that in addition to the content to be packaged, there is also a META-INF folder, there is a MANIFEST. MF file, this is a list file, which contains the relevant list information. We need to edit it and add the Class-Path: parameter, just like telling the java command what the classpath is. Highlighted in:

Finally, we can run the java-jar HelloWolrd. jar command. In the future, we will be able to display "Hello World" everywhere with this jar package, which is much more convenient than getting a bunch of options.

VIII. Summary

This article describes jdk download, installation, Environment configuration, program compilation, compilation, and execution in detail from start to end, in order to give you a deep understanding of this process. When the environment is ready and the IDE is used, you may seldom access this process in the future. But this process cannot be lost. This is a foundation. As I mentioned earlier, if you do not understand the basics, you do not know where to solve the problems. I learned a lot from the upper layer, but I don't know what the bottom layer is. I don't know what it is. If people change their packages, you will learn again, but you can't do anything else!

IX. References:

1. JVM learning notes (ii) ------ Java code compilation and execution throughout the process (http://blog.csdn.net/cutesource/article/details/5904542)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.