*
The moose likes Beginning Java and the fly likes Class path Problems Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Class path Problems" Watch "Class path Problems" New topic
Author

Class path Problems

Greg Mark
Greenhorn

Joined: Dec 13, 2010
Posts: 8
Hello all. I've been reading on Javas site and working out of 'Java How to Program' the past couple of days when I get the chance.

This isn't a syntax error, at least I don't think it is, so much as I don't think my class path is setting properly.

C:\Users\Sparrow\Desktop\My Stuff\Java\Projects\Tuts\Java HTP\2.1 Welcome\Welcom
e1\src>java Welcome1
Exception in thread "main" java.lang.NoClassDefFoundError: Welcome1
Caused by: java.lang.ClassNotFoundException: Welcome1
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: Welcome1. Program will exit.

I get this for anything I try to run using the 'java' command. 'javac' works fine.

I have this 'C:\Program Files (x86)\Java\jre6\lib\ext\QTJava.zip' set for it. I also tried it with the ' .; ' ahead of it. I'm kind of at a loss here. I'm running vista if that makes a difference.

This is the code:


Netbeans displays the results. Could Netbeans be a part of the issue?


Thank you for your time =)

Edit: I'm looking around on google and from what I'm reading, I have to set this each time I work in a different folder? I thought I simply set in the environment variables and that was that.
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2265
    
    3

Howdy, Greg!

Try including the current directory in your class-path when running your program:

java -cp . Welcome1


Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
Nuwan Arambage
Ranch Hand

Joined: May 05, 2010
Posts: 76
Hi Greg,

this is the common error you would get when you don't set the classpath properly. set the envirnment variables correctly.
set JAVA_HOME to where you install JDK. e.g. JAVA_HOME=C:\Program Files\Java\jdk1.6.0_13
one more thing, add bin folder (e.g. C:\Program Files\Java\jdk1.6.0_13\bin) to the path variable.it allow you to use java related commands irrespective of dirctory path
that's it.

Now for sure, your example shoud work.


thanks & regards,

Nuwan Arambage


Thinker
Nuwan Arambage
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

That QTJava.zip thing is something from Apple's QuickTime. I don't know why QuickTime sets this, but it's not necessary.

It's best to not set the CLASSPATH environment variable at all. If you have it set, then delete the whole variable. Note: You must close and re-open the Windows command prompt after changing the environment variables, otherwise the command prompt won't see the change.

If you don't set the CLASSPATH environment variable, Windows will look in the current directory for class files by default.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Greg Mark
Greenhorn

Joined: Dec 13, 2010
Posts: 8
Thank you everyone for the quick replies. I deleted the CLASSPATH from my environment variables and it's working now. =)

Will quick time create this variable again if I run it? I don't use the program, just wondering.

Edit: Also created the JAVA_HOME variable.
Ram anil
Greenhorn

Joined: Dec 16, 2010
Posts: 2
Can you try "java -classpath . Welcome1" (add current directory to classpath)


pakkatechies.blogspot.com
Alex Hurtt
Ranch Hand

Joined: Oct 26, 2010
Posts: 98
Greg Mark wrote:Thank you everyone for the quick replies. I deleted the CLASSPATH from my environment variables and it's working now. =)

Will quick time create this variable again if I run it? I don't use the program, just wondering.

Edit: Also created the JAVA_HOME variable.


There shouldn't be any need to create a JAVA_HOME environment variable unless you are using some tool or library (like ANT maybe for example) which specifically calls for it. The documentation for any such product should mention if it needs this. Otherwise you don't need to set JAVA_HOME as an Environment variable other than for your own convenience or use. I think it is a common misconception that the JAVA_HOME env. variable is somehow required by the java runtime.
Greg Mark
Greenhorn

Joined: Dec 13, 2010
Posts: 8
Alex Hurtt wrote: There shouldn't be any need to create a JAVA_HOME environment variable unless you are using some tool or library (like ANT maybe for example) which specifically calls for it. The documentation for any such product should mention if it needs this. Otherwise you don't need to set JAVA_HOME as an Environment variable other than for your own convenience or use. I think it is a common misconception that the JAVA_HOME env. variable is somehow required by the java runtime.


I had eclipse setup when I was using Head First before I realized the need for something with better explanations of the whats and whys. I think that had ANT, I switched over to NetBeans b/c that's what the Java site was making use of. Could this possibly mess me up in future endeavors or can I just leave it as is, currently everything seems to be in working order. Then again I am only doing simple programs as per java how to program.
Alex Hurtt
Ranch Hand

Joined: Oct 26, 2010
Posts: 98
Leaving it there probably won't hurt or mess anything up. Just didn't want you getting the wrong impression that the use of Java required that it be set.
Greg Mark
Greenhorn

Joined: Dec 13, 2010
Posts: 8
Alex Hurtt wrote:Leaving it there probably won't hurt or mess anything up. Just didn't want you getting the wrong impression that the use of Java required that it be set.


Alright, cool. Thank you =) It's good to have everything I need for the command prompt in working right.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

Greg Mark wrote:Will quick time create this variable again if I run it? I don't use the program, just wondering.

No, only when you install QuickTime the installation program for it will try to add this to your CLASSPATH.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Class path Problems