An excellent integrated development tool such as Eclipse and JBuilder may have forgotten the existence of the Java command line tool, we seldom use javac or Java in the command line to compile and run our program. This article is a return to the truth, because sometimes we should use these commands, at least to understand.
If you have installed j2sdk and set the environment variables classpath and path, we will be familiar with how to use Java, javac, and jar tools.
Assume that we are working on D:/. First, we can write a class of our own. Its function is very simple. It only provides a print () method and a constructor. The content is as follows:
Package com. Ming. Joke;
Public class testyou
{
Private string;
Public testyou (string S)
{
This. String = s;
}
Public void print ()
{
System. Out. println (string = NULL? "Null String": string );
}
}
Below we will compile this class and input javac-D bin testyou under the command line. java, where the bin directory is the directory we created in advance on the d disk. At this time, we can see that the following structure in the bin contains/COM/Ming/joke/testyou. class: This is the class file we get.-D is used to specify the output directory. What if-D is not used. Next we will package this class for use in our own library. First we will go to the bin directory and then use jar CVF test. jar COM, so that we can get a test package in the current directory. jar, which is the User-Defined lib we will use below. Let's talk about jar first. Here we didn't specify the manifest. MF file, which is a description file for the jar package. You can provide one by yourself and use the M parameter to use your MF to create a jar package. For details, refer to the use of jar (enter jar in the command line ).
Next we will create an application. The application will use our own class library test. Jar. The program is very simple and the code is as follows:
Import com. Ming. Joke. testyou;
Public class hello
{
Public static void main (string [] ARGs)
{
Testyou test = new testyou ("hello ");
Test. Print ();
}
}
Below we compile javac hello. java, the compiler will prompt that we cannot find COM. ming. joke package, of course you can put test. jar is added to classpath. Here we still use the command line. Enter javac-classpath bin/test. Jar hello. Java to generate the hello. Class class in the current directory. The classpath parameter tells the compiler where to find the user's class file. This parameter is important. Next, run Hello. class: Enter Java hello. At this time, the noclassdefinefoundclass exception will occur. The interpreter cannot find testyou. We use the-CP parameter to solve this problem. Enter Java-CP bin in the command line ;. hello, at this time, the console will show Hello,-CP tells the interpreter to go to the directory specified by CP to find hello and other libraries required, so be sure to add it after Bin. no. (indicates the current directory). Otherwise, an error will occur.
How many classes will be loaded when the hello program runs? A simple program will load hundreds of classes! You can use verbose: class to monitor and enter Java-verbose: Class-CP bin in the command line ;. hello, you will see the loaded classes in the console. If you want to monitor the running status of the garbage collector, you can use-verbose: GC to monitor this situation, you need to find a slightly larger program, such as java2demo under java_home/demo/jfc/java2d/. You can enter Java-verbose: GC-jar java2demo. jar shows the following output:
[GC 27872 K-> 26296 K (42216 K), 0.0069590 secs]
[GC 28973 K-> 26455 K (42216 K), 0.0036812 secs]
[GC 29134 K-> 26474 K (42216 K), 0.0016388 secs]
[GC 29117 K-> 26487 K (42216 K), 0.0008859 secs]
[GC 29134 K-> 26498 K (42216 K), 0.0009197 secs]
[GC 29180 K-> 26479 K (42216 K), 0.0008711 secs]
[GC 29149 K-> 26484 K (42216 K), 0.0008716 secs]
This is helpful in debugging a program that is mixed with Java and C/C ++, and may be useful to you during debugging!
There are also many useful options not introduced. You can directly enter Java javac or jar in the command line to see help. It is good to be familiar with it!