Compiling and running Java programs using the cmd command

Source: Internet
Author: User

For beginners, the benefit of compiling and running Java with the cmd Command (Unix and UNIX-like systems using termial) is to allow beginners to intuitively experience the step of compiling (Compile) and deepen their memory. The so-called compilation is to translate the text file Xxx.java into a Java Virtual machine can recognize the bytecode file Xxx.class, this part of the principle is not to do a drill, directly describes how to use the cmd command to execute the Java program. Unix/linux or Mac iOS and other Unix-like systems, methods are the same, some of the operations involved in CLASSPATH command and the Windows system is different, this is no longer mentioned.

Write a Hello World program
1  Public class HelloWorld {2      Public Static void Main (string[] args) {3         System.out.println ("Hello, world!" ); 4     }5 }

Because the Java text file name must be the same as the class name of public in the file, so I save the above code file named Helloworld.java, stored in the following directory:

1. Compiling

E:\myjava\javasrc>javac helloworld.java-d.

Command parsing:

Complete command: javac [options] [SourceFiles] [classes] [@argfiles]

    • Compiler for Javac:java languages, compiling Java source files using the command line, output bytecode files. class.
    • Sourcefiles:java the relative path of the source file.
    • Options: Here is the-D, directory shorthand, which represents the location of the output bytecode, "." Represents the current directory, "..." Represents the parent directory, ".. \ bin "represents the sibling directory bin in the current directory, and so on.

So Helloworld.java output bytes in the current directory after compilation is complete Helloworld.class file.

2. Running

E:\myjava\javasrc>java HelloWorld
Hello, world!.

The commands for Java runtime programs are:

java [options] classname [args]

    • Java: Execute program command. The execution order is the main () method that initiates the jre-> load class-> call class, and the main method must be public static void main (string[] args).
    • ClassName: The name of the class file to be executed, add the package name, in the middle with '. ' Separate, such as java.lang.String
    • Options: Some optional parameters [refer to Java documentation]

Successfully printed out the string: Hello, world!

Output class to the specified package

The package, in short, is a packet of Java bytecode files, the jar file is the package, so that the file structure, so that the hierarchy of the project more distinct, easy to maintain, note that the class declaration above the declaration of the packages:

1  Package Hello;     DeclarationPackage 23publicclass  HelloWorld {4      Publicstaticvoid  main (string[] args) {5         System.out.println ("Hello, world!" ); 6     }7 }
1. Compiling

E:\myjava\javasrc>javac helloworld.java-d.

Note that the storage path is ".." That is, the parent directory, which represents the "Hello" package build path, the class file will be generated in the Hello packet, if Hello does not exist, the program will be automatically created, the results on the desktop, the actual is a normal directory only:

2. Running

The commands for Java runtime programs are:

E:\myjava\javasrc>java. /hello. HelloWorld
Error: The main class could not be found or could not be loaded

The result is that the class is not found, if we now open the cmd to the Hello package in the same sibling directory, the "Myjava" directory, run:

E:\myjava>java Hello. HelloWorld
hello,world!

The program was successfully exported, that is, the Java interpreter is positioned to the class file. In fact, the Java interpreter can only find packets under the current path and the CLASSPATH variable storage path, so I must first switch the cmd path to the sibling directory of Hello and then run Java hello. HelloWorld.

Wouldn't it be troublesome to do so every time?

CLASSPATH

"The class path is the path, the Java Runtime environment (JRE) searches for classes, and other resource files." ----official documentation, Classpath is where the JRE is looking for class files and other resource files, it is necessary to put our generated packages into Classpath variables for ease of operation, and the WIN7 environment is:

Right-click the system variable, environment variable, advanced system settings, properties, computer, double-click "CLASSPATH", and enter "; path" at the end of the variable value, where ";" is a separate symbol for multiple classpath:

OK, after the setup is complete, reopen cmd, and enter the command at any location:

C:\windows>java Hello. HelloWorld
hello,world!

Can be run successfully!

Of course, Java commands can also explicitly use the-classpath parameter to tell the JRE where to look, but the scope of this is a command, or you can use the command line

  set classpath =%classpath%; dir //dir is the absolute path you need to set

When the cmd window is opened for a temporary classpath, the other cmd windows do not take effect (Linux uses #export path= $PATH:dir ). You can use

  Set Classpath

To see if there are any paths you have set in the Classpath currently in effect (Linux uses Echo $classpath view).

Summarize
    • Starting a Java application requires two processes to be compiled and run;
    • Compiling is to translate the Java source file written by the programmer into a Java Virtual machine executable bytecode file;
    • The prerequisite for running a bytecode file is that the virtual machine finds these files through the environment variable classpath of the JRE;
    • You can set the CLASSPATH variable to add your directory to the environment variable so that the JRE can find the bytecode file;
    • After the file is found, the virtual machine will load the bytecode file to run.

Do not accumulate kuibu, not even thousands of miles, do not accumulate small stream, no to become Jianghai. Running a Java program can seem like a small step, in fact it contains complex processes and logic, these technical details, need to accumulate a certain amount of practical experience and knowledge, and then continue to delve into.

Compiling and running Java programs using the cmd command

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.