Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't understand classpath for the life of me

Please explain classpath to me like I'm 10 yo.

When I compile a program with javac, I usually go to the root folder where the main class is:

javac Test.java

it compiles the program and all the classes in sub directories. for the above example:

C:\newApp\ ---> this is the folder where main class (Test.java) is located.

C:\newApp\com\example\class ---> this is the folder where some other classes are there.

javac compiles all java files with that single command. makes .class file for them.

now I can just run the app with :

java Test

What is classpath? What exactly is classpath in this case?? What should I know about classpath?? When using an IDE, do I need to set it??

like image 489
ufosecret Avatar asked Jul 05 '15 20:07

ufosecret


People also ask

How do I find out my classpath?

To check our CLASSPATH on Windows we can open a command prompt and type echo %CLASSPATH%. To check it on a Mac you need to open a terminal and type echo $CLASSPATH.

What is the meaning of classpath?

Classpath is a parameter in the Java Virtual Machine or the Java compiler that specifies the location of user-defined classes and packages. The parameter may be set either on the command-line, or through an environment variable.

What is the importance of classpath?

The classpath is a list of locations (directory or JAR files) in which the JVM will look for classes to load. If a class to be loaded cannot be found, there will be an error and the program may abort.


1 Answers

Let's assume that your command prompt's current directory is C:\newApp and a class called Test lives in C:\newApp\Test.java.

The classpath is simply the location, or set of locations where class files can be found, that your program might need to use. The classpath includes folders, JARs, and (in certain more complex setups) other sources of classfiles.

When Java needs to load a class, it looks on the classpath for it. The default classpath is the set of Jars that contain Java's built-in classes, combined with the current directory of your shell prompt (C:\newApp in this case).

When you try to load a class, say com.example.MyClass, Java will look in your classpath, in the location C:\newApp\com\example\MyClass.class. You can tell it to get the class from a JAR (which is nothing more than a ZIP archive with a .jar file extension) by putting said JAR on the classpath. It'll look inside the jar and "unzip" classes from there as needed.

When using an IDE, you do not generally need to worry about the classpath. The IDE will automatically put your code in the classpath automatically when running. If you want to use code from an external library, you simply need to tell your IDE to add it to the project (usually by putting it into your project folder and right-clicking it in the file list). It will then know that its autocomplete functionality should include available classes from that Jar, and that the Jar is to be placed on the classpath when running your project.

like image 154
nanofarad Avatar answered Sep 23 '22 02:09

nanofarad