0

I came across this question:

What is a classpath and how do I set it?

and the first answer explainns what classpath is and how to set it:

.....First, let's suppose that MyClass is something you built as part of your project, and it is in a directory in your project called output. The .class file would be at output/org/javaguy/coolframework/MyClass.class (along with every other file in that package). In order to get to that file, your path would simply need to contain the folder 'output', not the whole package structure, since your import statement provides all that information to the VM.

Now let's suppose that you bundle CoolFramework up into a .jar file, and put that CoolFramework.jar into a lib directory in your project. You would now need to put lib/CoolFramework.jar into your classpath. The VM will look inside the jar file for the org/javaguy/coolframework part, and find your class.

So, classpaths contain:

JAR files, and Paths to the top of package hierarchies....

but if java only looks for classes in directories specified by CLASSPATH variable how does java find classes from libraries that are part of JRE?

  • "how does java find classes from libraries that are part of JRE" - you basically answered it yourself: they are part of the JRE, i.e. they are added to the classpath automatically. – Thomas Nov 19 '19 at 10:44
  • ok so when i edit the classpath variable the system will automatically add an entry to the classes of jre library, correct ? – user12394078 Nov 19 '19 at 10:48
  • Well, when defining a classpath you tell the classloader where to find classes that should be loaded in addition to those already known to the JRE. Since you can use multiple classloaders you could have multiple classpaths but I'd not bother with that concept unless you really need it. Just think of the classpath you set as "the location of any additional classes". – Thomas Nov 19 '19 at 10:52

0 Answers0