• Post Reply Bookmark Topic Watch Topic
  • New Topic

Classpath problem  RSS feed

aliasger talib
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey all,
I was trying to run some jave files which had a package structure. Needless to say they the classpath for this package structure needed to be set correctly.
First, I added the directory where the classes were, but that gave me a
Exception in thread "main" java.lang.NoClassDefFoundError: CourseReader
I added every possible path in the claspath and I was still getting errors. Finally, someone suggsted putting "." (fullstop) at the beginning of my classpath. Apparently, this is signifies the current directory to be included in the classpath.
It worked! It compiled and worked.
Now, can someone please explain to me how and why that little fullstop made all the difference??
Thanks guys.
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you add a full stop '.' to the classpath list, you're depending on the current working directory, which is definitely not the way to go. Have a look at the following little scenario. Suppose three .java source files exist:

Also assume that these three files reside in one single directory: /some/path/source. Make a directory for the compiled .class files: /another/path/classes.
Here goes:
1) move to /some/path/source
2) compile all sources as follows: javac -d /another/path/classes *.java
The -d directive tells javac where to store the compiled .class files.
After step 2) as completed, the /another/path/classes directory contains the following files/subdirectories:
- /another/path/classes/X/Foo.class
- /another/path/classes/Y/Bar.class
- /another/path/classes/X/Z/Baz.class
As you might have noticed, javac (the compiler) creates subdirectories for classes contained in packages.
Now set your classpath
- CLASSPATH= /another/path/classes:$CLASSPATH
(or any shell equivalent thereof)
Suppose class Foo contains a public static void main(String[] args) method. This class can be run from anywhere as follows:
- java X.Foo
i.e. it is not necessary to change a current working directory or whatever.
kind regards
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!