File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes CLASSPATH Environment Variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "CLASSPATH Environment Variable" Watch "CLASSPATH Environment Variable" New topic

CLASSPATH Environment Variable

Scott Updike
Ranch Hand

Joined: Feb 16, 2006
Posts: 92
Can someone explain to me why an incorrect CLASSPATH setting would allow me to compile a java program but not run it? I ran into a scenario where I had an incomplete CLASSPATH variable and was able to compile my program, but when I tried to run it I got the follwoing error:

Exception in thread "main" .....

I feel like I've missed something in all the books that I've been reading...

Any help would be appreciated!
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Could you give an example?
Christophe Verré

Joined: Nov 24, 2005
Posts: 14688

You may be able to compile files in the current directory, even if it is not specified in the classpth. However, you won't be able to run it as long as you do not set the current directory to the classpath.

[My Blog]
All roads lead to JavaRanch
Scott Updike
Ranch Hand

Joined: Feb 16, 2006
Posts: 92
Here's an example..

It seems that the CLASSPATH environment variable "affects" the JVM environment in two ways (and probably more): at compile time and at run time.

Simple Scenario 1 (compile time):

If I've coded a source file and compiled it in a package called com.example.model which is stored in c:\javacode\src\com\example\model\[myjavafile].java. I'm not able to import (successfully) that file into any other source file until I add c:\javacode\src to my CLASSPATH variable. So I assume that in order to import other custom packages, I have to add their directory path (to the source files) to the CLASSPATH variable. Is this true, why do I add
pathnames to source files when CLASSPATH would tell me that I would add the directory path to the compiled class files.

...but, when I follow instructions to download .jar files and add the directory path to my existing CLASSPATH variable, then I am pointing the JVM to look at .class files and not .java files. (I had to do that when I started the JSP and Servlets HF book). Everything works fine, but it's
a little confusing.

Simple Scenario 2 (run time):

If I go into a random directory (c:\random) and create a simple "hello world" class ( I can compile it (generating a hw.class file in same directory), but I cannot execute the class file until I add
c:\random to my CLASSPATH variable; otherwise, I get the error stated in my original post.

I really feel like I'm missing something with this CLASSFILE business eventhough conceptually I feel that if the JVM needs to know where class files are (libraries or downloaded .jar files that need to be imported/used into new code), then I just add the directory paths to those files before I can compile or run code.
subject: CLASSPATH Environment Variable
It's not a secret anymore!