Question

I am reviewing the core fundamentals in Java right now. I encountered a topic which is about classpath. I am confused what is this classpath?How do they differ with path? How are they related with environment variables?

I have been developing Java applications using IDEs for quite a fair amount of time. And everything seem to be too automatic and abstracted that sometimes basic things like these are not seen by some developer, well at least in my part. I would like to learn this basic things to strengthen my foundation in the Java programming language. Thanks in advance!

Was it helpful?

Solution

One of the drawbacks to an IDE is that it can 'cover up' some of the basics of a language by automating it, and then as a result when you need to analyse without an IDE you get a bit stuck as it has always taken care of various elements of the program for you. So it is definitely a good idea to try and grasp the basics of language so you know what the IDE is automating for you.

A Classpath is normally as it literally sounds, a pathway to class files used by an application. The main function behind it is to tell the JVM where to find certain resources that it requires. For example in an IDE like Eclipse you can add external jars to reference 3rd party librarys, and it will take care of the neccessary path setting. Were you to do this via a command line you would need to include a classPath to tell the JVM where the required libraries are, in a fashion similar to the following:

javac -cp "path/to/lib.jar" MyProgram.java //-cp is set classpath option

The reason you don't need to do this with the 'core api' library is because of Environment Variables set on your computer. Environment variables are used by your Operating system as stored pathways to various essential bits and bobs. For example in my environment variables under the Path variable I have:

C:\Program Files\Java\jdk1.7.0_25\bin

This goes to a directory containing the executable files such as java and javaw. How I 'think' this works (I don't have any definitive sources on this bit) is that when you compile/run a java application using something like:

java MyApplication or javac MyApplication.java

The computer will check along it's environment variables at some point looking for those executables, which is why javac doesn't work until you set the correct Path in the environment var section. Once found it runs the executables. I expect that it is also able to find the main Java api (rt.jar?) from this pathway as well.

The main difference I feel is that Classpath is an argument supplied at run/compile time to add extra resources for the JVM, Environment variables are permanent paths on your machine used for various things including telling the PC where to find the JVM and it's important files.

Oracle do have tutorials on setting classpaths such as: http://docs.oracle.com/javase/7/docs/technotes/tools/windows/classpath.html

Regarding the Environment variable this answer links to an article about looking into Environment variables.

Hope this helps

Good Luck!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top