*
The moose likes Beginning Java and the fly likes CLASSPATH driving me crazy Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "CLASSPATH driving me crazy" Watch "CLASSPATH driving me crazy" New topic
Author

CLASSPATH driving me crazy

Sean Mack
Greenhorn

Joined: Oct 12, 2006
Posts: 7
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

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Aug 16, 2005
Posts: 14074
    
  16

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


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
sven studde
Ranch Hand

Joined: Sep 26, 2006
Posts: 148
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

Joined: Oct 12, 2006
Posts: 7
Got it sorted.

Thanks Guys.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: CLASSPATH driving me crazy
 
Similar Threads
how java looks for classes to load
how to set java env variables
Cannot find symbol class
javac: no classpath found
Error: Could not find or load main class com.sun.tools.javac.Main