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

Class path Problems

 
Greg Mark
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy, Greg!

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

java -cp . Welcome1
 
Nuwan Arambage
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
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
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.
 
Greg Mark
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you try "java -classpath . Welcome1" (add current directory to classpath)
 
Alex Hurtt
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15272
37
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic