Develop applications using Java Command Line Tool

xiaoxiao2021-03-06  135

Eclipse, JBuilder This excellent integrated development tool may already have forgotten the existence of Java Command Line Tool, and we have rarely use Javac or Java to compile and run our programs in the command line. This article is necessary to return, because some of these orders should still be used, at least understand.

Suppose you have installed J2SDK and set up environment variables ClassPath and Path, then we are familiar with how to use Java, Javac and Jar tools.

Suppose we are in D: / Work, first we write a self-feature of its own class is very simple, just provide 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);}

Here we compile this class Enter Javac -d bin testyou.java in the command line, where the bin directory is our directory in the D disk, and we see the following structure in BIN. / COM / MING / Joke / testYou.class, this is the Class file we get, the effect of -D is to specify the output directory. If you don't need -d, how do you try it? Below we have to pack this class to use our own library, we first entered the bin directory, then use Jar CVF Test.jar COM so we can get a package Test.jar in the current directory, this is what we are going on below The user's own defines the lib. Let's talk about Jar, here we don't specify the manifest.mf file, it is a description file for the JAR package. You can provide one and use M parameters to use your own MF to create a JAR package. Specifically, please refer to JAR's use (enter JAR in the command line).

Below we are doing an app, the app is to use our own class library Test.jar, the program is simple, 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 us to find the com.ming.joke package, of course, you can join Test.jar to the ClassPath, here we still use the command line. Enter Javac-ClassPath Bin / Test.jar Hello.java, which generates a Hello.Class class in the current directory, where the classpath parameter is to tell the compiler where to find the user's class file. This parameter is more important. Next, we run Hello.class, enter Java Hello, this time there will be an exception of NoclassDefineFoundClass, the interpreter can not find testyou, we use -cp parameters to solve this problem, enter java -cp bin in the command line ;. Hello, this Time The console will appear Hello, -cp tell the interpreter to go to the directory specified by the cp to find Hello and other class libraries, so be sure to add. Number (indicating the current directory) will not be wrong. How many classes are loaded when the Hello program is running, this simple program will load hundreds of classes! You can use Verbose: Class to monitor, enter java -verbose: class -cp bin in the command line; Hello you will see the case that is loaded in the console, if you want to monitor the running situation of the garbage collection, then you You can use -rrbose: GC to monitor this situation, you have to find a slightly large program, such as Java2Demo, Java2D / Java2Demo, you can enter java -verbose: gc -jar java2demo.jar can see The following output: [GC 27872K-> 26296K (42216K), 0.0069590 SECS] [GC 28973K-> 26455K (42216K), 0.0036812 SECS] [GC 29134K-> 26474K (42216K), 0.0016388 SECS] [GC 29117K-> 26487k ( 42216K), 0.0008859 SECS] [GC 29134K), 0.0009197 SECS] [GC 29180K-> 26479K (42216K), 0.0008711 Secs] [GC 29149K-> 26484K (42216K), 0.0008716 SECS] This is debugging Java and The procedure for mixing together with C / C is very helpful, maybe it will be useful in debugging!

There are also many useful options that have not been introduced, you can enter Java Javac or JAR to see help, more familiarity will be beneficial!

转载请注明原文地址:https://www.9cbs.com/read-127481.html

New Post(0)