Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

CLASSPATH driving me crazy

 
Sean Mack
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there I'm pretty new to Java and having some trouble so I hope someone here can help me..
(I've searched some threads on this topic but still having trouble!)

I downloaded JDK5.0 Update 9 with NetBeansIDE and downloaded it into the directory
C:\Program Files\Java\jdk1.5.0_09...

I am using the command line as recommended in the book Headfirst Java as NetBeans is much too complex for me at this stage.

I am storing my programs in the following directory structure
C:\java\myclasses\HelloWorldApp\HelloWorldApp.java
compiling to
C:\java\myclasses\HelloWorldApp\HelloWorldApp.class

C:\java\myclasses\SimpleDotComGame\SimpleDotComGame.java
compiling to
C:\java\myclasses\SimpleDotComGame\SimpleDotComGame.java
etc.

I have set up my PATH correctly as
C:\Program Files\Java\jdk1.5.0_09\bin and it is working fine.

However I am having serious problems with CLASSPATH.
I have tried a few of the suggestions here but none seem to work consistently. I have used
set CLASSPATH=.;
and then gone into one of my program directories and the program has compiled ok but it only works in that directory for that program and won't work for any others. I get NoClassDefFoundError.

It is very frustrating. I have also set this variable from the System menu in Control panel with no luck.

Is there any way that you can set your classpath to always look for the classes in your current directory?

Please help!!

Cheers, agshean.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can ignore the system CLASSPATH and specify a CLASSPATH to use with the -cp option.

java -cp . MyClass
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15272
37
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any way that you can set your classpath to always look for the classes in your current directory?

That is what you already did by including "." (the current directory) in your CLASSPATH:

set CLASSPATH=.;

If you do not set the CLASSPATH environment variable at all (i.e. do not create a CLASSPATH variable in Windows), Java will look for classes in the current directory by default.

If you want to be able to run your classes from any directory, you'll have to include the directory that contains your class files in the CLASSPATH:

set CLASSPATH=C:\java\myclasses\HelloWorldApp
java HelloWorldApp


or

java -cp C:\java\myclasses\HelloWorldApp HelloWorldApp

See this: How to set the classpath
 
sven studde
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
However I am having serious problems with CLASSPATH.
I have tried a few of the suggestions here but none seem to work consistently. I have used
set CLASSPATH=.;

I suggest you completely avoid creating or setting a CLASSPATH environment variable. You need to learn how to compile programs using the -classpath option in both the javac and java commands. The -classpath option overrides the CLASSPATH environment variable anyway, and the paths you list in the -classpath option will be different for different files.

For a simple program that has no package statement as the first line in the file and does not have any lines that start with "import", you can compile your programs like this:

somePrompt> cd C:\someDir\theFolderMyProgramIsIn>
...> javac -classpath "" MyClass.java
...> java -classpath "" MyClass

Note: the -classpath "" zeros out anything set in the CLASSPATH environment variable and essentially means you aren't using any classpaths.

I am storing my programs in the following directory structure
C:\java\myclasses\HelloWorldApp\HelloWorldApp.java

Do your files have package statements in them?
[ October 13, 2006: Message edited by: sven studde ]
 
Sean Mack
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it sorted.

Thanks Guys.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic