JAVA and JAVAC Commands
-classpath option in javac and java command lines
This is a very basic question, but because they are basically using existing IDE tools to develop Java programs, few people realize this.
javac
-classpath, set the path to search for the class, which can be a directory, jar file, or zip file (there are class files inside), which will overwrite all settings in CLASSPATH.
-sourcepath, sets the path to search for the java file required to compile, which can be a directory, jar file, or zip file (all java files are included).
So a complete javac command line should look like this,
Assume that abc.java is in the path c:/src, and in any directory, the following command can be executed 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
It means that the class file below c:/classed is required, the class file inside c:/jar/abc.jar, the class file inside c:/zip/abc.zip also needs the source file below c:/source/project1/src, the source file inside c:/source/project2 /lib/src.jar, the source file inside c:/source/project3/lib/src.zip,
Note: jar, there will be no changes to the source files in zip, and the source files in the directory may be recompiled.
java
-classpath, sets the path of the class to be searched, which can be a directory, jar file, or zip file (all class files are included), which will overwrite all CLASSPATH settings.
Since the class to be executed is also part of the class to be searched, the path of this class must also be placed in the -classpath setting.
It is manifested in that when executing Java in the path of the class to be executed, you must add a dot (.) to indicate that this directory should also be searched.
Assume abc.class is in the path c:/src
The following command can be executed 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 is in the com.cnblogs.jeffchen package, then 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 there are also com.cnblogs.jeffchen in other jar packages. What will happen? mistake?
Under Windows,
The splitter of the file path is backslash/
The splitter of the class or java file list is a semicolon;
Delimiter bit slash of file path in linux/
The separator of the class or java file list is a colon:
An example of compiling and running under linux
/usr/local/java/bin/javac -classpath /tmp/javatest/lib/mail-1.3.3.jar -d /tmp/javatest/bin/ /tmp/javatest/src/jp/co/realseed/Capability.java/usr/local/java/bin/javac -classpath /tmp/javatest/lib/mail-1.3.3.jar:/tmp/javatest/bin/ jp.co.realseed.Capability
Thank you for reading, I hope it can help you. Thank you for your support for this site!