Home > Java > javaTutorial > body text

Detailed explanation of Java environment configuration, compilation and operation (pictures and text)

黄舟
Release: 2017-03-30 10:36:19
Original
1421 people have browsed it

This article mainly introduces the relevant information of Java environment configuration and compilation and operation in detail. It has certain reference value. Interested friends can refer to it

1.Opening

After understanding the previous Java journey, I believe Beginners have a relatively deep understanding of Java, but things cannot always stay at the theoretical level. Only through more implementation can we gain true knowledge. I think that when learning something, you must follow theory-practice-theory. Such a routine. First understand the basic concepts of things. After understanding the basic concepts, you must find ways to apply them in every aspect of your life. This will deepen your memory of the theory and discover your own blind spots. In practice, we will gradually have our own views and ideas on it, and deepen the theory to a certain extent. Finally, we will return to theory and summarize our own things, so that things will truly belong to you. So, today we are going to start moving. Configure the Java environment manually, and use the prepared Java environment to compile and run the program. It should be noted that in order to give you a deeper understanding of how Java works and how to compile and run it, this chapter will not cover any details. When using IDE, of course you use Notepad for coding. IDE can improve daily development efficiency, but Notepad can increase your chances of applying!

2. JDK download

To carry out Java development, in addition to JRE (Java Runtime Environment), we also need corresponding development tools, so we need to download the JDK. You can download the JDK from the Oracle official website: www.oracle.com/, select Downloads - Java.

for

Developers, as follows:

##Then select download JDK, the latest version currently available is 8u121 Detailed explanation of Java environment configuration, compilation and operation (pictures and text)

#.

##If you want to get the previous version, you can scroll to the bottom of the page and select Java Archive to get the historical Java version.

Detailed explanation of Java environment configuration, compilation and operation (pictures and text)

##We. Here we take downloading the latest 8u121 as an example for subsequent instructions.

Detailed explanation of Java environment configuration, compilation and operation (pictures and text)3. 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 basically just default to the next step. If necessary, you can modify the corresponding directory. ## will pop up halfway. A dialog box means the directory where the JRE is installed. Select the directory and then "Next".

##After the installation is successful, if you want to see

API##. #Documents and the like, you can click on its "Follow-up Tutorial". Detailed explanation of Java environment configuration, compilation and operation (pictures and text)

At this point, the JDK has been successfully installed. Detailed explanation of Java environment configuration, compilation and operation (pictures and text)

4. Environment configuration

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

Detailed explanation of Java environment configuration, compilation and operation (pictures and text)

Some students may encounter the following situation:

Why is this happening? Careful students may find that the current directory is different. As I said at the beginning, we need to execute the java command in the bin directory of the directory where you installed the JDK for it to take effect. The reason is simple, because there are various commands in the bin directory, and java is one of them. Many of the Java commands we use, such as javac, javaw, etc., are in its bin directory. So if you want to study in depth in the future, you can first learn about the tools in the bin directory. There are many useful tools in it. That's something to talk about later.

Detailed explanation of Java environment configuration, compilation and operation (pictures and text)

To answer the topic just now, it is very inconvenient if you have to run the java command in the bin directory. Not only is it inconvenient, many other programs cannot use the java command because they don’t know how to use it. Where to find the java command. At this time, we need to configure the environment variables on the system so that the java command can be executed in any directory.

Select"Start"-right-click "Computer"-Properties-Advanced System Settings-Environment Variables to open Environment variable setting window.

Detailed explanation of Java environment configuration, compilation and operation (pictures and text)

Mainly set the following three variables:

1.New : JAVA_HOME, value is the JDK path, here is: C:\Program Files\Java\jdk1.8.0_121

2. Edit: PATH, add ";%JAVA_HOME%" at the end of the original variable value \bin;%JAVA_HOME%\jre\bin". Here’s a little explanation. The %JAVA_HOME% in the value means the value of the previous newly added variable JAVA_HOME. It can be seen that the first new variable JAVA_HOME is to facilitate the configuration of the PATH variable without writing too lengthy. The meaning of the PATH variable itself is that Windows will search for the commands to be executed one by one in the directories listed in the PATH variable value, so as long as PATH is configured, the java command does not need to be executed in the bin directory of the JDK directory. . As for the two directories above, it is because the commands under bin are all commands used by java, which can also be said to be some tools.

3. Added: CLASSPATH, whose value is ".;%JAVA_HOME%\lib;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME %\lib\tools.jar". The function of CLASSPATH is to let the Java program know where to find the Java source file or bytecode file (CLASS file) when compiling it (when using javac). Among them, dt.jar is the class library about the running environment, and tools.jar is the tool class library, which is needed during compilation and operation. It is worth noting here that there is a "." at the beginning of the entire string of values, which must not be missed. This represents the current directory.

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

Detailed explanation of Java environment configuration, compilation and operation (pictures and text)

5. First introduction to Java compilation

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

The process of Java compiler compiling Java code is as follows:

Detailed explanation of Java environment configuration, compilation and operation (pictures and text)

The JVM execution engine completes the execution of Java bytecode:

Detailed explanation of Java environment configuration, compilation and operation (pictures and text)

As you can see from the above two figures, the Java running program is divided into two steps. The first step is to compile the source code into bytecode, and the second step is to compile the bytecode into target code. This is different from C and C++ being directly compiled into machine-related target code. Through the intermediate link of bytecode, you can take the successfully compiled package and publish it to any machine with a JVM environment, and then the JVM will compile the final target code related to the machine, thus achieving "Write Once, Run Anywhere" without being tied to a specific running platform. So what we usually call compiling code is the process of compiling Java source code into JVM bytecode.

6. java and javac

As we have said in the previous section, there are a large number of Java tools that can be used in the %JAVA_HOME%\bin directory. We will also Be familiar with it gradually. So as those of us who are new to Java, which one should we be familiar with first? It is undoubtedly java and javac.

Today’s society is too fast-paced, and many people are more or less eager for quick success. In terms of convenience in development, this is mainly reflected in the fact that I don’t understand how the bottom layer of the program works. I start IDE development as soon as I come up. After encountering various problems, I don’t know how to deal with them because I don’t understand the underlying principles. Personally, I think that using an IDE can greatly improve our production efficiency, but you must also understand the underlying principles, otherwise you will not know how the IDE compiles, packages and runs your program, and you will not be able to solve problems when you encounter them. . A situation we often see is that some people are confused when they see that there is no IDE on the server, and they don't even know how to run the jar package program.

Back to the main story, let’s start with java and javac to learn how to compile and run a java program on the command line. First, we understand how to use these two commands respectively.

java command:

Function: used to execute classes or execute jar files.

Enter java in the cmd console and press Enter. We can get the format of running the java command as follows:

Detailed explanation of Java environment configuration, compilation and operation (pictures and text)

The above picture is not complete, the options (-options ) There are others later, you can run the java command yourself to see. We temporarily ignore the content of the option (-option) and focus on the required items "class" and "jar file". You can see that the java command can directly run classes and jar files. For example, if there is a HelloWorld class in the HelloWorld.class file, the command to run is: java HelloWorld. What should be noted here is that the java is not followed by the class file (HelloWorld.class), but the corresponding class (HelloWorld). If there is a jar file of HelloWorld.jar, the command to run is: java -jar HelloWorld.jar.

javac command:

Function: Used to compile .java files.

Enter javac in the cmd console and press Enter. We can get the format of javac command running as follows:

Detailed explanation of Java environment configuration, compilation and operation (pictures and text)

We temporarily ignore the content of the option (-option) , you can see that the simplest format of javac is followed by a source code file. For example, if there is a HelloWorld.java file that needs to be compiled, the command to run is: javac HelloWorld.java, which will generate a HelloWorld.class bytecode file in the current directory.

7. The first Java program

After understanding the java and javac commands, we can start the first Java program. Of course, we use Notes Originally started our first Java program. Microsoft's own Notepad has relatively few functions and the user experience is not very good. I personally prefer Notepad++. You can choose your favorite notepad according to your own habits.

Part 1:

Create the HelloWorld class and save it in the HelloWorld.java file.

Detailed explanation of Java environment configuration, compilation and operation (pictures and text)

In the cmd console, run the javac HelloWorld.java command to compile it, and the HelloWorld.class file will be automatically generated.

Detailed explanation of Java environment configuration, compilation and operation (pictures and text)

Then run the java HelloWorld command, Hello World will be displayed. It must be noted here that instead of running java HelloWorld.class, java is followed by the class to be run, which is HelloWorld.

Detailed explanation of Java environment configuration, compilation and operation (pictures and text)

This completes the entire process from code writing, to compilation, to running. Isn’t it very simple? In fact, all complicated things start from simplicity. As long as the foundation is laid, it will be very simple to learn the upper level things.

Part 2:

Some people may ask, if you want toreferenceother jar packages or classes ,then what should we do? Don't worry, let's try it. First, create a User class (just create a random class, don’t pay too much attention to it). It has a method to tell the content, which is stored in the User.java file. The code is as follows:

Detailed explanation of Java environment configuration, compilation and operation (pictures and text)

##Modify the HelloWorld class, the code is as follows:

Detailed explanation of Java environment configuration, compilation and operation (pictures and text)##User.java is placed in the otherclass directory, not in the same directory as HelloWorld.java:

Detailed explanation of Java environment configuration, compilation and operation (pictures and text)Then we try to compile using the above method, and we will find that the User class cannot be found. This is because javac does not know where to find the User class.

Detailed explanation of Java environment configuration, compilation and operation (pictures and text)When encountering these situations, we must look at the help prompts to see what parameters javac can provide. This is not only to solve the current problem, but also to solve more problems in the future. As long as you can draw inferences from one example, no problem will be difficult for you.

Detailed explanation of Java environment configuration, compilation and operation (pictures and text)We can see that as long as the -classpath parameter is added, the file location of the class to be found can be specified, so that we can specify the User location for javac, so We can write:

Detailed explanation of Java environment configuration, compilation and operation (pictures and text) will successfully compile HelloWorld.java and User.java, and generate HelloWorld.class and User.class in the corresponding directory.

At this time we can execute java HelloWorld to see if the results can be obtained, but unfortunately, it said that the User class was not found:

Detailed explanation of Java environment configuration, compilation and operation (pictures and text)

Smart students may think of it , java should also have a -classpath parameter. Congratulations, you are starting to get better. Let’s try it quickly:

Detailed explanation of Java environment configuration, compilation and operation (pictures and text)

It doesn’t work again, it’s collapsing! ! Haha, don't worry, java is a little different from javac. If java takes the -classpath parameter, java will only look for classes in the directory where the classpath is located. So if HelloWorld is actually in the current directory, you need to add the current directory, that is, add one. ".", as follows:

Detailed explanation of Java environment configuration, compilation and operation (pictures and text)

#This will run successfully.

Part Three:

At this time, some people are upset again, why do they have to execute such a long list of things every time they compile and run? How troublesome is that? If there are too many classes, wouldn’t it take a long time to input a command? Is there any more convenient method for future execution? Of course there is, this is to package all classes into an executable jar package, and then just run the jar package directly. The command used here is the jar command. Let’s take a look at its format and content first:

Detailed explanation of Java environment configuration, compilation and operation (pictures and text)

Here we mainly use the following four options:

-c Create a new archive

-v Generate verbose output in standard output

-f Specify the archive file name

-e Bundle to the executable jar Independent application of the file

Specify the application entry point

Here we use the following command to package User.class and HelloWorld.class into the HelloWorld.jar package according to the format of the prompt.

Detailed explanation of Java environment configuration, compilation and operation (pictures and text)

This generates a HelloWorld.jar file. If you want to see what is in HelloWorld.jar, let me tell you a little trick. You can drag the jar package into a compression software such as winrar and you can see the specific content.

Detailed explanation of Java environment configuration, compilation and operation (pictures and text)

You can see that in addition to the content to be packaged, there is also a META-INF folder, which contains a MANIFEST.MF file, which is a manifest file. There is relevant listing information inside. We need to edit it and add the Class-Path: parameter, just like telling the java command what the classpath is. The added content is highlighted in the picture below:

Detailed explanation of Java environment configuration, compilation and operation (pictures and text)

Finally, we can run its program through the java -jar HelloWolrd.jar command. In the future, we can display "Hello World" anywhere with this jar package, which is much more convenient than having to create a long list of options before.

Detailed explanation of Java environment configuration, compilation and operation (pictures and text)

8. Summary

This article covers the downloading and installation of jdk, the configuration of the environment, the writing, compilation and execution of the program. A very detailed introduction is given from beginning to end in order to give everyone a deep understanding of this process. Once the environment is set up and the IDE is used, people may rarely come into contact with this process in the future. But this process cannot be lost. It is a foundation. As I said before, if you don’t understand the basics, you won’t know how to solve problems when you encounter them. You have learned a lot about things at the upper level, but you don’t understand what is going on at the bottom level. You have no idea. If they change the packaging and you learn it all over again, but you can’t draw inferences from one example, it’s half the result with twice the result!

The above is the detailed content of Detailed explanation of Java environment configuration, compilation and operation (pictures and text). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!