• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

CLASSPATH Environment Variable

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
Thanks,
Scott
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you give an example?
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Scott Updike
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 (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.
reply
    Bookmark Topic Watch Topic
  • New Topic