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:
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.
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 (hw.java). 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.
Catch Ernie! Catch the egg! And catch this tiny ad too: