Java Beginner: How Java compiles and runs programs at the command line

Source: Internet
Author: User

We said in the paper that we first install Java, then set up the Java environment, and then compile and run a simple Java program that shows a "Hello baby" in command-line mode. So, why? What's going on here?

First, download and install Java without asking why, right? The so-called paddle, without Java but want to use, this is impossible.

Second, why do we have to set up the Java Runtime environment? Because, so far, I just want to compile and run the Java program at the command line, so you need to have command-line mode to recognize the Java command. The command line can be automatically recognized commands, we must manually add the path to the command. Perhaps you will say, also not Ah, such as CD, dir ah these simple command-line commands, we have not manually added Ah, yes, that is the system automatically added. Now you have to use your own commands, and you will have to add them yourself.

Perhaps, you would ask, what do you mean by the command line so far? Can we not do it later? Yes, if you want to compile and run Java programs, you do have to install Java, but you do not have to set the Java environment, Java operating environment is not so much a Java operating environment, rather than to be able to run Java under the command line and have to make the settings. If you want to compile and run Java programs, after installing Java, you can actually use other compiler tools to write running Java programs, such as the famous eclipse, which we will talk about later.

Well, I think you should understand why we're installing Java and why we're setting up the environment. But it's not really clear yet, so let's explain how to compile a Java program at the command line:

See Javac Test.java First, what is this? This is the compiled file named Test.java, which is the Java program source file we wrote. Javac is an order that means "compiling Java programs". Test.java, this is the object that the command works on. That is, compile a Java source file named Test.java. What happens when you write a program code that doesn't have a syntax error (but maybe there's a logic error) that will compile and compile? Nothing seems to have happened if only from the command-line display. But in fact, if you go back to the location of the source file to look at, you will find that there is a source file with the same name, but the suffix named class file, in our case, is test.class, what is this thing? This is the result of your compilation, and when you compile it, Java will automatically generate a file with the same name as the source file, but with the suffix named class. With this file, it is possible for us to run the Java program. How does it work?

And then we look at Java Test, what is this? This is the run Java program, Java is the command to run the Java program, test is the program name. Why not Test.java? Because Java does not run Test.java, Java is actually running the Test.class file, but does not write the suffix name. If you do not believe, when you compile through, and generated the class file, you can completely delete the source files, and then use the format of the Java file name to run the program, completely can. In our example, after compiling and generating the Test.class file, delete the D-disk Test.java, and then, under the command line, the D-packing directory, into Java Test, can be run.

So what is a class file? It is the Java can directly read and execute directly the file, you have to use Notepad to open the word is also OK, inside is garbled, why? Because that is to read to Java, not to you read, you do not understand the normal, and your notepad opened, should not be its true.

However, we generally do not delete the source files, why not delete it? What did you delete it for? You're not looking at your code, are you? If you don't look at your teacher, why don't you divide it? You don't look at your clients. And what if you want to change the program? Can you change the class file directly? No. So... According to my beginner's humble opinion, you still keep the source file.

OK, so now let's summarize how Java runs the program under the command line, first, if you install Java and set up the Java Runtime Environment, then you have a Java source program. In this way, the command is used in the following format: Command name command Action object. Use Javac to compile the file, and then use Java to run the program.

Java Beginner: How Java compiles and runs programs at the command line

Related Article

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.