Java and javac commands are introduced in detail

Java and javac commands

The - classpath option on the javac and Java command lines

This is a very basic problem, but few people realize this because they basically use the existing ide tools to develop java programs.

javac

-Classpath, set the path of the class to be searched, which can be a directory, jar file, zip file (all of which are class files), which will overwrite all the settings in classpath.

-Sourcepath, set the path to search for java files required for compilation, which can be directory, jar file and zip file (all java files inside).

So a complete javac command line should be like this,

Suppose ABC Java is in the path C: \ Src. In any directory, you can execute the following commands to compile.

javac -classpath c:\classes; c:\jar\abc. jar; c:\zip\abc. zip -sourcepath c:\source\project1\src; c:\source\project2 \lib\src. jar; c:\source\project3\lib\src. zip c:\src\abc. java

Indicates that the following class file is required for compilation, C: \ jar \ ABC The class file in jar, C: \ zip \ ABC The class file in zip also needs the source file under C: \ source \ project1 \ SRC, C: \ source \ project2 \ lib \ Src Jar, C: \ source \ project3 \ lib \ Src The source file in zip,

Note: the source files in jar and zip will not be changed. The source files in the directory may be recompiled.

java

-Classpath, set the path of the class to search, which can be directory, jar file and zip file (all of which are class files), which will overwrite all classpath settings.

Since the class to be executed is also part of the class to be searched, you must also put the path of this class in the setting of - classpath. When executing Java in the path of the class to be executed, you must add a dot (.) to indicate that this directory also needs to be searched.

Suppose ABC Class is in the path C: \ Src

You can execute the following commands in any path

java -classpath c:\classes; c:\jar\abc. jar; c:\zip\abc. zip; c:\src abc

Question: if main Class belongs to C: \ jar \ ABC Jar, and on COM cnblogs. In the jeffchen package, execute Java - classpath C: \ classes; c:\jar\abc. jar; c:\zip\abc. zip; com. cnblogs. jeffchen. Main, but what if the classpath contains multiple jar packages? And other jar packages also have com cnblogs. What happens to jeffchen? Wrong?

Under windows, the delimiter of file path is backslash \ class or semicolon of java file list;

Under Linux, the separator of file path is slash / class or the separator of java file list is colon:

An example of compiling and running under Linux

Thank you for reading, hope to help you, thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>