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
.java
The source code of Java needs to be compiled into a .class
file, 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: src
the source program is stored in the file, and bin
the 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
, , javadoc
etc., which are some command-line utilities
Execute the class file
.java
First compile the .class
file into a file with the following command line command
javac Foo.java
This will get you a file Foo.class
with the .class
filename; then java
run 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
2) javac
Command to compile the source program and get the .class
file
3) java
The command runs the compiled Java class file
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 cd
going 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
.jar
The file is equal to the sum of .class
the 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 .jar
file, you need to use jar. packaging technology
.jar
Command-line tools are also imported into the user's environment along with the JDK
jar
There are many parameters for the command
From a practical and practical point of view, we recommend using the following packaging commands
jar cvfm Foo.class
The m
parameter specifies a mainfest
file (including information about the Java class file)
The simplest mainfest
file can be written like this
Once the file is written, mainfest
it can be packaged
jar cvfm foo.jar mainfest.mf *.class
*.class
The 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 文件名.jar
under terminal using.jar
Explain three points: 1) Finally, do not open
open the .jar
file directly, otherwise the following situation may occur
2) If you use an IDE for development, usually it integrates the .jar
file packaging function (explore yourself)
3) Considering the Java file system, if there is a package, write the full package name