Java path-Environment configuration and compilation run

Source: Internet
Author: User

This article outlines

First, the outset

Second, JDK Download

Third, JDK Installation

Iv. Environment Configuration

v. Initial knowledge Java Compilation

Six, Java and Javac

Seven, the first Java Programs

Viii. Summary

Ix. references

First, the outset

by the previous after understanding the Java road, I believe that beginners have a deeper understanding of Java. But things can not always stay on the theoretical level, but also more to achieve, to see the truth. I think, learn one thing, to follow the theory-practice-theory such a routine. First understand the basic concept of things, understand the basic concepts, to find ways to use the life of the drip, so that can deepen the theory of memory and found their own blind spots. In the repeated practice, we will gradually have their own views, ideas, the theory has a certain deepening. Finally return to the theory, summed up their own things, so that things really belong to you. So, today we're going to start working with the Java Environment and compile the program with a well-equipped Java environment. It is important to note that in order to give you a deeper understanding of how Java works, how to compile and run, this chapter will not involve any use of the IDE, encoding of course, with Notepad. IDE to improve the daily development efficiency, but notepad can increase the candidates for the admission opportunity yo!

Second, JDK Download

As already mentioned in "Java Road--java", Java development requires the development tools in addition to the JRE (Java Runtime Environment), so we need to download the JDK. Download JDK to Oracle website: https://www.oracle.com/, select Downloads--java for developers as follows:

Then choose Download JDK, the latest version available is 8u121.

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

Here we take the latest 8u121 as an example for follow-up instructions.

Third, JDK Installation

After the corresponding version has been downloaded according to the system environment, it can be installed. The whole installation process is nothing special, the basic can be done by default next. If necessary, you can modify the corresponding directory.

A dialog box pops up, meaning the directory installed by the JRE, selecting the directory and then "next".

After successful installation, if you want to see the API documentation and so on, you can click on its "follow-up tutorial."

At this point, the JDK has been successfully installed.

Iv. Environment Configuration

After successful installation, how to use it? We can open the command console and go to the bin directory where you installed the JDK directory, execute the Java command, and if you have a lot of hints, prove that you are installed correctly.

Some students may encounter the following situation:

Why is this so? The attentive classmate may find that the current directory is different. I said from the beginning that we need to execute the Java command in the bin directory where you installed the JDK directory to take effect. The reason is simple, because there are various commands in the bin directory, and Java is one of the commands. We use a lot of Java commands, such as Javac, JAVAW, etc., are in its bin directory, so we can learn in depth, the bin directory of the tools to learn, there are a lot of useful tools, this is something.

To answer just the topic, if you must be in the bin directory to run Java commands is very inconvenient, not only inconvenient, many other programs to use the Java command is not used, because they do not know where to find Java commands. At this point, we need to configure environment variables for the system so that Java commands can be executed in any directory.

You can open the Settings window for the environment variable by choosing Start-Right-click Computer-Properties-Advanced system settings-environment variables.

The following three variables are mainly set:

    • 1. Added: Java_home, the value is JDK path, here is: C:\Program files\java\jdk1.8.0_121
    • 2. Edit: PATH, add ";%java_home%\bin;%java_home%\jre\bin" at the end of the value of the original variable. Here's a little bit of explanation. The value inside the%java_home% meaning is the value of the last new variable java_home, visible the first new variable java_home is to facilitate the configuration of the path variable does not need to write too lengthy. The path variable itself means that Windows finds the command to execute in the directory listed in the PATH variable value, so as long as the Path,java command is configured, it does not have to be executed in the bin directory of the JDK directory. And as to why the above two directories, because the bin is under the command of Java, also can 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 ". Classpath's role is to let Java programs compile (with Javac) know where to find Java source files, or bytecode files (class files). Among them, Dt.jar is about the runtime environment of the class library, Tools.jar is a tool library, compile and run the time needed to use. It is worth noting here that the beginning of the whole string of values has a "." and must not be missed, which represents the meaning of the current directory.

When we finish configuring these three points, we can then run the java-version command in the user's directory and see that CMD will automatically find the Java program and run it.

v. Initial knowledge Java Compilation

Before we develop our first Java program, we'll start with a rough look at the entire process of compiling and executing Java code. We often see files with these two suffixes in the Java project:. Java and. class. Both of these files represent Java source code and bytecode files, and the bytecode file is the key to Java's implementation of "Write Once,run Anywhere". We can take a look at the following two pictures [1].

The process by which the Java compiler compiles Java code is as follows:

The JVM execution engine completes the execution of the Java bytecode:

From the above two diagram can be seen, Java run the program in two steps, the first step is to compile the source code into bytecode, the second step is to compile the bytecode into the object. This is not the same as C, C + + directly compiled to machine-related target code. Through the middle of the bytecode, you can take a successful package to publish to any JVM environment on the machine, and then by the JVM to achieve machine-related final target code compilation, so as to achieve "Write once,run Anywhere", without the need to be tied to the specific running platform. So what we usually call compiling the code is the process of compiling the Java source code into the JVM bytecode.

Vi.java and Javac

As we have said in the previous section, there are a large number of JAVA tools available in the%java_home%\bin directory, and we will have to become familiar with it later. Which is the first thing we should be familiar with when we touch Java? Java and Javac are undoubtedly the same.

Now the social rhythm is too fast, a lot of people are more or less a bit quick. In the development of convenience, which is mainly reflected in the lack of understanding of the program's bottom is how to operate, a start IDE development, encountered a variety of problems, because do not understand the underlying principle, do not know how to deal with. Personally, the IDE to use, can greatly improve our production efficiency, but the underlying principle also must understand, otherwise do not know how the IDE says your program compiled packaging run, encountered problems will not be solved. A very common situation is that some people see the server without the IDE is ignorant, even how to run the JAR package program does not.

Returning to the story, let's start with Java and Javac and learn how to compile and run a Java program on the command line. First of all, we know how these two commands are used separately.

Java command:

Function: Used to execute a class or execute a jar file.

Entering Java enter in the CMD console, we can get the Java command to run in the following format:

No truncation is complete, and the option (-options) is followed by a Java command that you can run on your own. We temporarily ignore the contents of the option (-option), mainly the required fields "class" and "Jar file". You can see the Java command to run the class and jar files directly. For example, how to have a Helloworld.class file with a HelloWorld class, the command to run is: Java HelloWorld. It is important to note that Java is not followed by a class file (Helloworld.class), but the corresponding classes (HelloWorld). If there is a Helloworld.jar jar file, the command to run is: Java-jar Helloworld.jar.

Javac Command:

Function: Used to compile. java files.

Enter Javac carriage return in the CMD console and we can get the Javac command to run in the following format:

We temporarily ignore the contents of the option (-option), and you can see that the simplest format for Javac is to follow a source code file. For example, if you have a Helloworld.java file that needs to be compiled, the command that runs is: Javac Helloworld.java, you can generate a Helloworld.class bytecode file in the current directory.

Seven, the first Java Programs

Now that we've learned the Java and Javac commands, we're ready to start our first Java program, and of course we started our first Java program with a notepad. Microsoft comes with less Notepad function, the user experience is not very good, personal preferred to use notepad++, we can according to their own habits to choose favorite Notepad.

The first part:

Create the HelloWorld class, which is saved in the Helloworld.java file.

In the CMD console, when you run the Javac helloworld.java command to compile it, the Helloworld.class file is automatically generated.

Then run the Java HelloWorld command, and the Hello World will be displayed. It is important to note that not running Java Helloworld.class,java is followed by the class to be run, that is, HelloWorld.

This completes the whole process of writing from code, to compiling, to running, isn't it simple? In fact, all the complex things are from simple to start, as long as the foundation, learning the top of the thing is very simple.

Part II:

Some people will ask, if you want to refer to other jar packages or classes, then what? Don't worry, let's try. First build a user class (just casually build a class, we don't care too much), it has a way to say the content, there is a User.java file, the code is as follows:

Modify the HelloWorld class with the following code:

User.java placed in the Otherclass directory, not in the same directory Helloworld.java:

Then we try to compile with the method above, we find that the user class is not found, because Javac does not know where to look for the user class.

In these cases, we must look at the help tips to see what parameters Javac can provide. This is not only to solve the current problems, but also for the future to solve a variety of more problems, as long as you can do extrapolate, no problem can be rare to you.

We can see that as long as the-classpath parameter is added, you can specify the file location of the class to be found, so that we can specify the location of the user for Javac, so we can write:

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

At this point we can execute the Java HelloWorld to see if we can get the results, but unfortunately, we didn't find the user class:

A smart classmate might think that Java should also have a-classpath parameter. Congratulations, it's starting to start, so let's try it:

No, it's crashing!! Oh, don't worry, Java and javac a bit different, Java with-classpath parameter will be Java will only in the directory where Classpath find class, so if the HelloWorld is the current directory, to add the current directory, that is, add a ".", as follows:

This is a successful operation.

Part III:

At this time, some people are not happy, why every compile and run to execute such a big string of things, that much trouble, if the class up, it would not lose a command to lose half a day, there are some convenient method for later implementation use it? Of course, that's the way to package all classes into an executable jar package, and then run the jar package directly later. The command used here is the jar command, and we'll look at its format and content:

Here we mainly use the following four options:

-C Create a new archive file

-V generates verbose output in standard output

-f Specifies the archive file name

-E is a standalone application bundled into an executable jar file

Specify the application entry point

Here we use the prompt format, with the following command to package User.class and Helloworld.class into a Helloworld.jar package.

This generates a Helloworld.jar file. If you want to see what helloworld.jar inside, tell everyone a small trick, you can drag the jar package to WinRAR and other compression software, you can see the specific content.

You can see that in addition to packaging content, there is a Meta-inf folder, there is a MANIFEST.MF file, which is a manifest file, which has the relevant inventory information. We need to edit it, add Class-path: parameters, just like tell the Java command what Classpath is. Highlighted in the add-on content:

Finally, we can run its program by Java-jar the Hellowolrd.jar command. In the future, we can display "Hello world" anywhere with this jar bag, which is much easier than a bunch of options before.

Viii. Summary

This article from the JDK download, installation, environment configuration, program writing, compiling and execution, from beginning to end do a very detailed introduction, in order to let everyone have a deep understanding of the process. The environment is well-equipped, with the IDE, you may have little contact with this process in the future. But this process can not be lost, this is a foundation, as I said before, if you do not understand the foundation, when encountering problems, do not know where to solve. The upper things learn more, but do not understand the bottom is what is the matter, in the heart no end, people change a package, you re-learn, and do not extrapolate, it is less than good!

Ix. References:

1.JVM Learning Notes (ii)------The entire process of compiling and executing Java code (http://blog.csdn.net/cutesource/article/details/5904542)

Java path-Environment configuration and compilation run

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.