How to run a Java program from the command line

This is a simple but easy to ignore problem. Novices are often blocked by some seemingly inconspicuous pits. This article aims to explain how to run Java programs under the command line interface in the most relaxed atmosphere, including packaging using the Jar tool.


write in front

Java file system

.javaThe source code of Java needs to be compiled into a .classfile, that is, a class file (the full name is Java class file). Its task in terms of platform independence is to provide a binary service for Java programs that is independent of the underlying host platform. - anyway, it's a binary file

And to deal with computers, it is through binary files

In addition, if a Java program is written in an IDE, the IDE usually provides a comfortable development environment for the developer: srcthe source program is stored in the file, and binthe compiled class file is stored in the file

JDK Toolkit for Java

Go to Sun's official website to download the JDK (Java Development Kit) of the Java SE version (standard Java), you will get: 1) JRE (Java Runtime Environment), 2) Java Core Class Library, 3) Java Toolkit

Among them, the Java toolkit includes java, javac, , javadocetc., which are some command-line utilities


Execute the class file

.javaFirst compile the .classfile into a file with the following command line command

javac Foo.java

This will get you a file Foo.classwith the .classfilename; then javarun the binary with the command pair

java Foo.class

An example is as follows:

1) Write a simple demo, note that the file name is the same as the class name of the public class

write picture description here

2) javacCommand to compile the source program and get the .classfile

write picture description here

3) javaThe command runs the compiled Java class file

write picture description here

It should be noted that if we use IDE to write (or for coder with good habits), our compiled files are usually under the package folder. After cdgoing to the project directory (project directory), we need to display The form written in the formula 包名.类文件名to specify the package name (equivalent to relative path addressing)


Execute the jar file

.jarThe file is equal to the sum of .classthe file and the material files necessary for some other programs. For example, a Java project references a picture and a piece of music in the directory. If you want to combine them with the code file into an entry file, that is, a .jarfile, you need to use jar. packaging technology

.jarCommand-line tools are also imported into the user's environment along with the JDK

jarThere are many parameters for the command

write picture description here

From a practical and practical point of view, we recommend using the following packaging commands

jar cvfm Foo.class

The mparameter specifies a mainfestfile (including information about the Java class file)

The simplest mainfestfile can be written like this

write picture description here

Once the file is written, mainfestit can be packaged

jar cvfm foo.jar mainfest.mf *.class

*.classThe asterisk wildcard will match all current Java class files (there is only one class file that is not very useful, just for illustration)

Then run the file java -jar 文件名.jarunder terminal using.jar

write picture description here

Explain three points: 1) Finally, do not openopen the .jarfile directly, otherwise the following situation may occur

write picture description here

2) If you use an IDE for development, usually it integrates the .jarfile packaging function (explore yourself)

3) Considering the Java file system, if there is a package, write the full package name

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324695141&siteId=291194637