Use of classpath and Path in Java

Source: Internet
Author: User

Part1

Javac: if other classes (for example, inheritance) are referenced in the Java file to be compiled, but the. Class
The file is not in the current directory. In this case, you must add the-classpath parameter after the javac command, you can use the following three methods to guide the compiler to search for reference classes in the specified path during compilation.

(1). absolute path: javac-classpath C:/junit3.8.1/JUnit. Jar XXX. Java
(2). Relative Path: javac-classpath ../junit3.8.1/JUnit. javr XXX. Java
(3). System variable: javac-classpath % XXX. Java
(Note: % classpath % indicates that the system variable classpath is used for search. Here we assume that the path of JUnit. jar is included in the classpath system variable.
Medium)

 

Part2

Use of the absolute javac path:

Javac: Suppose the name of the class to be compiled is helloworld. Java, and its full path is D:/Java/helloworld. java. However
Your current directory is: C:/Documents and Settings/Peng>. If you want to execute compilation here, what will happen?

(1). C:/Documents and Settings/Peng> javac helloworld. Java
At this time, the compiler will give the following error message: Error: cannot read:
Helloworld. Java: by default, javac searches for class files in the current directory. Obviously, this path is not where class files are stored, so an error is reported.

(2). C:/Documents and Settings/Peng> javac D:/Java/helloworld. Java is compiled successfully.

Therefore, as long as the directory for executing the javac command is not the directory for storing class files, you must explicitly specify the class file path in the javac command.

 

Part3

Use of Java-classpath:

Java: assume that our classpath is set to D:/Peng/Java/pro, and there are three files in this directory: helloworld. Java
/Helloworldextendstestcase/
Helloworldextendshelloworld. The class declarations of these three files are as follows:

Helloworld. Java: public class helloworld
Helloworldextendshelloworld. Java: public class
Helloworldextendshelloworld extends helloworld
Helloworldextendstestcase. Java: public class helloworldextendstestcase
Extends JUnit. Framework. testcase

Suppose we have successfully compiled three files according to the above usage of the javac-classpath and javac absolute paths. Now we can execute these three. class files in the C:/Documents and Settings/Peng> directory.

(1). C:/Documents and Settings/Peng> JAVA helloworld

Hello World

The execution is successful. Why are we in C:/Documents documents and
Settings/Peng> run the command. Can JVM find the D:/Peng/Java/Pro/helloworld. Class file? This is because we
The system variable classpath is configured and points to the Directory D:/Peng/Java/Pro.
. Therefore, by default, the JVM will load class files in this directory without specifying the absolute path of the. Class file.

(2). C:/Documents and Settings/Peng> JAVA helloworldextendshelloworld

Hello World

The execution is successful. Helloworldextendshelloworld inherits the helloworld class.
Whether there is a helloworld. Class file in classpath. Because we have compiled the helloworld class successfully, we can execute it successfully.
Helloworldextendshelloworld. Class

(3). C:/Documents and Settings/Peng> JAVA helloworldextendstestcase exception in thread
"Main" Java. Lang. noclassdeffounderror:
JUnit/framework/testcase: The program throws an exception and prompts that the JUnit. Framework. testcase file cannot be found. Why?
Example: D:/Peng/Java/Pro
Next, helloworldextendshelloworld. class can be successfully executed, and this will not work? This is because
Is: The JUnit. Framework. testcase. Class file does not exist in the current directory, so to make the program run successfully, we must specify
Classpath allows JVM to find the class JUnit. Framework. testcase, such as (4 ):

(4). C:/Documents and Settings/Peng> JAVA-classpath % helloworldextendstestcase

Hello World

Summary:

(1). When to use-classpath: When you wantCompile
OrRun
The. Class file of the referenced class is not in the current directory.
-Classpath to introduce the class (2 ). when to specify the path: When the directory of the class you want to compile is not the same as the directory where the javac command is executed, You need to specify the path of the source file
PATH (classpath is used to specify the. Class path, not to specify the path of the. Java file)

 

Javac specifies the classpath to specify where to find the class to use (because the class path is specified), and the source file is still located in the current directory (unless/users/Andrew /... /helloworld. java)

In Java, classpath is specified to specify the path of the class used for execution. For example, your current directory has a helloworld. java. util. java helloworld can run successfully (packages in the default Java library can always be found ). however, if you specify Java-CP. /temp/Andrew. jar helloworld, then helloworld. the class cannot be found. In this case, you must add Java-CP. /temp/Andrew. jar :. /helloworld. the class or jar file can be included in the classpath. /temp/Andrew. jar :. /: $ classpath helloworld.

Appendix: how to set classpath in bash and CSH

For bash shell:
export CLASSPATH=$CLASSPATH:/java/classes:/home/tchin/myclasses

For tcsh or CSH:
set CLASSPATH = ($CLASSPATH /java/classes /home/tchin/myclasses)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.