Home arrow static arrow Java Programming [Archive] - set CLASSPATH does not work, how do I run java.exe?
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - set CLASSPATH does not work, how do I run java.exe?
This topic has 12 replies on 1 page.

Posts:16
Registered: 7/4/03
set CLASSPATH does not work, how do I run java.exe?  
Aug 6, 2004 2:16 PM



 
Hi guys,

I have a problem running the java.exe and I'm using Win XP. Here is my problem:

Under the command prompt, I set the path: path = C:\Program Files\Java 2\j2sdk1.4.2_05\bin

Then I created a folder in E:\Hello. I save my Hello.java in that folder, and I compiled it:

javac Hello.java

Now, there is a Hello.class created in that folder.

Then I try to run it: java Hello

but I got an error: "Exception in thread "main" java.lang.NoClassDefFoundError: Hello"


So, I try to set CLASSPATH, however, the problem remains, no matter how hard I try.

Any ideas?

 

Posts:2,206
Registered: 8/15/02
Re: set CLASSPATH does not work, how do I run java.exe?  
Aug 6, 2004 2:26 PM (reply 1 of 12)



 
Try this on the command line (assuming you are in the same directory as you class file)

java -cp . Hello
 

Posts:10,967
Registered: 4/7/01
Re: set CLASSPATH does not work, how do I run java.exe?  
Aug 6, 2004 2:40 PM (reply 2 of 12)



 
Any Ideas?

[url http://java.sun.com/j2se/1.4.2/docs/tooldocs/windows/classpath.html]Setting the class path[/url]
[url http://java.sun.com/j2se/1.4.2/docs/tooldocs/findingclasses.html]How Classes are Found[/url]
 

Posts:16
Registered: 7/4/03
Re: set CLASSPATH does not work, how do I run java.exe?  
Aug 7, 2004 1:23 AM (reply 3 of 12)



 
None of the suggestion worked, I wonder why???

Here is my location for the JDK:
C:\Program Files\Java 2\j2sdk1.4.2_05\bin

Here is my location for my Java project file:
E:\JavaProject\Hello

Here is the file name:
Hello.java

Here is the command prompt:

C:\Documents and Settings\Alex Ngai> e:

E:\> cd JavaProject

E:\JavaProject> cd Hello

E:\JavaProject\Hello> path = C:\Program Files\Java 2\j2sdk1.4.2_05\bin

E:\JavaProject\Hello> javac Hello.java

E:\JavaProject\Hello> java Hello
Exception in thread "main" java.lang.NoClassDefFoundError: Hello

E:\JavaProject\Hello> java -cp Hello
// Does not work, it lists all of the options for java

E:\JavaProject\Hello> set CLASSPATH = E:\JavaProject\Hello
CLASSPATH="C:\Program Files\Java\j2re1.4.2_04\lib\ext\QTJava.zip"

E:\JavaProject\Hello> java Hello
Exception in thread "main" java.lang.NoClassDefFoundError: Hello

// I have tried a lot more, none of them won't work!!


Now what should I try in the following command prompt?

E:\JavaProject\Hello>

 

Posts:1,135
Registered: 1/16/04
Re: set CLASSPATH does not work, how do I run java.exe?  
Aug 7, 2004 2:20 AM (reply 4 of 12)



 
Now what should I try in the following command prompt?
E:\JavaProject\Hello>

Well, before we go on further, lets try this...

Copy and compile this code and name it HelloWorld.java
Note: the H and the W are capitals...
public class HelloWorld {	   public static void main( String [] args ){	   		  System.out.println("Hello World");	   }}

This below is un-formatted, so it easier to copy...

public class HelloWorld {
public static void main( String [] args ){
System.out.println("Hello World");
}
}

Once you have copied the code, then try to compile and run...
If this does not work, then it will help point us in the right direction...

- MaxxDmg...

- ' He who never sleeps... '
 

Posts:18,384
Registered: 21.03.00
Re: set CLASSPATH does not work, how do I run java.exe?  
Aug 7, 2004 2:26 AM (reply 5 of 12)



 
E:\JavaProject\Hello> java -cp Hello
// Does not work, it lists all of the options for java

Hi, note that that isn't what EvilEdna said you should try.
java -cp . Hello
(Note the dot)

Also, have you declared the class to be in a package, if so, remove it.

/Kaj
 

Posts:11,200
Registered: 7/22/99
Re: set CLASSPATH does not work, how do I run java.exe?  
Aug 7, 2004 2:29 AM (reply 6 of 12)



 
E:\JavaProject\Hello> java -cp Hello
// Does not work, it lists all of the options for java
This would work if you had "." between -cp and Hello. The command should be "java -cp . Hello" The . means the current working directory so it has the same effect as typing E:\JavaProject\Hello, it's only much shorter.

E:\JavaProject\Hello> set CLASSPATH = E:\JavaProject\Hello
CLASSPATH="C:\Program Files\Java\j2re1.4.2_04\lib\ext\QTJava.zip"
This would work too if you had not typed spaces around the equals-sign. DOS doesn't allow spaces.
 

Posts:447
Registered: 3/8/01
Re: set CLASSPATH does not work, how do I run java.exe?  
Aug 7, 2004 4:50 AM (reply 7 of 12)



 
On Windows XP (maybe other versions too), an easier way to set the PATH and CLASSPATH is the control panel. Type [Windows Key] [Pause] to open System Properties (or double-click it in the control panel), then go to Advanced, and Environment Variables. Under System Variables, press New, and add a variable named CLASSPATH with whatever you want on it.
 

Posts:18,384
Registered: 21.03.00
Re: set CLASSPATH does not work, how do I run java.exe?  
Aug 7, 2004 4:59 AM (reply 8 of 12)



 
The CLASSPATH environment variable should not be used any longer. You should use java -cp, or an entry in the manifest for a jar.

/Kaj
 

Posts:668
Registered: 9/2/03
Re: set CLASSPATH does not work, how do I run java.exe?  
Aug 7, 2004 5:34 AM (reply 9 of 12)



 
The CLASSPATH environment variable should not be used
any longer.

What the reason behind this?
 

Posts:18,384
Registered: 21.03.00
Re: set CLASSPATH does not work, how do I run java.exe?  
Aug 7, 2004 6:02 AM (reply 10 of 12)



 
The reason is that the CLASSPATH affects all installed java applications, and different applications might need different versions of some libraries. It's always better to use -cp. That has been the recommendation from sun for quite some time.

http://java.sun.com/j2se/1.3/docs/tooldocs/win32/classpath.html

/Kaj
 

Posts:10,967
Registered: 4/7/01
Re: set CLASSPATH does not work, how do I run java.exe?  
Aug 7, 2004 1:24 PM (reply 11 of 12)



 
The CLASSPATH environment variable should not be
used
any longer.

What the reason behind this?

The reason is threads like this that show how people get it screwed up. If time is taken to learn to use classpath settings correctly (see my earlier post in this thread that provides the necessary links to do that) then it can be used if one wants.

 

Posts:27,518
Registered: 11/3/97
Re: set CLASSPATH does not work, how do I run java.exe?  
Aug 7, 2004 3:45 PM (reply 12 of 12)



 
None of the suggestion worked, I wonder why???

Here is my location for the JDK:
C:\Program Files\Java 2\j2sdk1.4.2_05\bin

Here is my location for my Java project file:
E:\JavaProject\Hello

Here is the file name:
Hello.java

Here is the command prompt:

C:\Documents and Settings\Alex Ngai> e:

E:\> cd JavaProject

E:\JavaProject> cd Hello

E:\JavaProject\Hello> path = C:\Program Files\Java
2\j2sdk1.4.2_05\bin

E:\JavaProject\Hello> javac Hello.java

E:\JavaProject\Hello> java Hello
Exception in thread "main"
java.lang.NoClassDefFoundError: Hello

E:\JavaProject\Hello> java -cp Hello
// Does not work, it lists all of the options for java

E:\JavaProject\Hello> set CLASSPATH =
E:\JavaProject\Hello
CLASSPATH="C:\Program
Files\Java\j2re1.4.2_04\lib\ext\QTJava.zip"

E:\JavaProject\Hello> java Hello
Exception in thread "main"
java.lang.NoClassDefFoundError: Hello

// I have tried a lot more, none of them won't work!!


Now what should I try in the following command prompt?

E:\JavaProject\Hello>


Given the above there is only one possible explaination.

You have a file named "Hello.java".

In that file you have a non-public class which is NOT named "Hello". Instead it is named something like "HelloWorld" or even "hello" (case matters.)
 
This topic has 12 replies on 1 page.