File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Error when running a class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Error when running a class" Watch "Error when running a class" New topic
Author

Error when running a class

Jennifer Wright
Greenhorn

Joined: Nov 16, 2011
Posts: 5

Problem started with not being able to use javac since I fixed that I am not able to run any of the classes that I have created
This is the error I am getting when trying to run a class
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

Have you set the CLASSPATH variable? If yes, what is its value? You can read more about it at: HowToSetTheClasspath


Mohamed Sanaulla | My Blog
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1012
    
    5

If the class Weather is defined in a package (say com.mypackage), you will need to run it using:

java com.mypackage.Weather
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11231
    
  16

try this:

java -cp . Weather

from your Programming directory, and see if that works.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Here's an example I just posted in another thread that covers the very basics of the two cases of no package and yes package.

Example 1, no package:

C:/classes/MyClass.class exists

java -cp C:/classes MyClass


Example 2, MyClass is declared package com.me;

C:/classes/com/me/MyClass.class exists

java -cp C:/classes com.me.MyClass


Note that in #2, you cannot do something like java -cp C:/classses/com/me MyClass. You must provide the full package name, and the root of that package must be inside a classpath root.

If you're still having trouble, copy/paste the exact, complete error message, and indicate clearly and precisely (like the above) what you did, what directory you were in, where your class was, etc.

P.S. Watch out for spaces. You can have a classpath with spaces in it, but you need to quote it. Start with all relevant paths having no spaces, so there will be one less potential source of error. Once you've got it working that way and understand the fundamentals, you can move things around if you want. (Although, if I were you, I would avoid paths with spaces like the plague. They are a source of soooo many headaches.)
Jennifer Wright
Greenhorn

Joined: Nov 16, 2011
Posts: 5

I checked my CLASSPATH and here is what is listed: ,;C:\Program Files\Java\jdk1.6.0_29\bin;C:\Program Files\Java\jre6\lib\ext\QTJava.zip
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Raven Illusion wrote:I checked my CLASSPATH and here is what is listed: ,;C:\Program Files\Java\jdk1.6.0_29\bin;C:\Program Files\Java\jre6\lib\ext\QTJava.zip


You don't need CLASSPATH and shouldn't rely on it. If you specify -cp or -classpath on the command line, that will supersede whatever is in the CLASSPATH environment vraiable.

Additionally, that CLASSPATH looks suspicious. The first element, the comma (",") is not valid. Are you sure it's not a period (".")? (This will no matter if you use -cp or -classpath as suggested. Just pointing out something that looks odd.)

Did you try what I suggested in my previous post? If so, and it didn't work, please provide details.
Jennifer Wright
Greenhorn

Joined: Nov 16, 2011
Posts: 5

Jeff Verdegan wrote:
You don't need CLASSPATH and shouldn't rely on it. If you specify -cp or -classpath on the command line, that will supersede whatever is in the CLASSPATH environment vraiable.

Additionally, that CLASSPATH looks suspicious. The first element, the comma (",") is not valid. Are you sure it's not a period (".")? (This will no matter if you use -cp or -classpath as suggested. Just pointing out something that looks odd.)

Did you try what I suggested in my previous post? If so, and it didn't work, please provide details.



I tried what you listed above and I get the same error message as in my original post.
The CLASSPATH I listed was copy/pasted directly from Enviroment varibles
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Raven Illusion wrote:
I tried what you listed above and I get the same error message as in my original post.


This is getting a bit frustrating.

Please, in one post, indicate clearly exactly what you did (what directory you were in, what exact command you ran, where your .class file is, what the package declaration of your .java file is) and exactly what the result was (the exact, complete error message, copy/pasted).

When stuff is scattered across multiple posts, or not spelled out plainly and completely, it makes it very hard to build a mental picture that matches what you're seeing and doing when you're sitting at your computer.
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1012
    
    5

@Raven Illusion

Could you tell us if the Weather class is defined inside a package or not?
Vyas Sanzgiri
Ranch Hand

Joined: Jun 16, 2007
Posts: 686

OR

you can use some of the beautiful free editors and focus on your code rather than

Try Netbeans IDE : www.netbeans.org


===Vyas Sanzgiri===
My Blog
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Vyas Sanzgiri wrote:OR

you can use some of the beautiful free editors and focus on your code rather than

Try Netbeans IDE : www.netbeans.org


I respectfully disagree. Understanding how classpath and packages work (along with the general concepts of absolute and relatives paths) is important for any Java developer. Certainly IDEs are useful as projects become bigger and more complex and the developer becomes more comfortable with the fundamentals, but by skipping to just doing everything in an IDE rather than overcoming this relatively minor obstacle, the OP would be doing a disservice to his education.
Vyas Sanzgiri
Ranch Hand

Joined: Jun 16, 2007
Posts: 686

Jeff Verdegan wrote:
Vyas Sanzgiri wrote:OR

you can use some of the beautiful free editors and focus on your code rather than

Try Netbeans IDE : www.netbeans.org


I respectfully disagree. Understanding how classpath and packages work (along with the general concepts of absolute and relatives paths) is important for any Java developer. Certainly IDEs are useful as projects become bigger and more complex and the developer becomes more comfortable with the fundamentals, but by skipping to just doing everything in an IDE rather than overcoming this relatively minor obstacle, the OP would be doing a disservice to his education.


I dont think this is the topic of conversation. It is to run the program. In that respect my current post is also useless

With my experience, I only had to dig up on classpath and package for my SCJP. For everything else, including learning Java, Netbeans has worked wonders

But this will be a never-ending conversation. I am giving my opinion just as you have given 5
Jennifer Wright
Greenhorn

Joined: Nov 16, 2011
Posts: 5

Jeff Verdegan wrote:
This is getting a bit frustrating.

Please, in one post, indicate clearly exactly what you did (what directory you were in, what exact command you ran, where your .class file is, what the package declaration of your .java file is) and exactly what the result was (the exact, complete error message, copy/pasted).

When stuff is scattered across multiple posts, or not spelled out plainly and completely, it makes it very hard to build a mental picture that matches what you're seeing and doing when you're sitting at your computer.


okay going to try to get all the info that you are looking for, sorry to frustrate you, I am very new at this.
From a command line I go to C:\Documents and Settings\User\My Documents\Programming (this contains all of the class files I have created)
I run "java Weather" (sans quotes) when I hit enter I get error:

C:\Documents and Settings\User\My Documents\Programming>java Weather
Exception in thread "main" java.lang.NoClassDefFoundError: Weather
Caused by: java.lang.ClassNotFoundException: Weather
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: Weather. Program will exit.

C:\Documents and Settings\User\My Documents\Programming>

what the package declaration of your .java file is: I do not know how to answer this question

Some other background is that it was all working fine till I uninstalled and older version of the JDK at first I was unable to compile but could still run class files. After I fixed that issue is when I stopped being able to run my class files
I hope this helps you get a clear picture of what's going on.

Vyas Sanzgiri
Ranch Hand

Joined: Jun 16, 2007
Posts: 686

can you your complete Java code here ?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Raven Illusion wrote:From a command line I go to C:\Documents and Settings\User\My Documents\Programming (this contains all of the class files I have created)
I run "java Weather" (sans quotes) when I hit enter I get error:

C:\Documents and Settings\User\My Documents\Programming>java Weather
Exception in thread "main" java.lang.NoClassDefFoundError: Weather
Caused by: java.lang.ClassNotFoundException: Weather
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: Weather. Program will exit.

C:\Documents and Settings\User\My Documents\Programming>


Okay, as I pointed out in an earlier post, the following will work if the Weather class is not in a package (see below) AND if Weather.class is in your current directory:

java -cp . Weather


As I alluded to previously, because you have a CLASSPATH environment variable and did not supersede it with a -cp command line argument, the CLASSPATH variable is what is determining where the JVM will search for classes, and since "." (current directory) is not part of the CLASSPATH variable you showed earlier, your current directory is not being searched for that class.

what the package declaration of your .java file is: I do not know how to answer this question




If your .java file starts with a package statement, then you must take that into account, which I showed in my earlier example.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Vyas Sanzgiri wrote:
Jeff Verdegan wrote:
Vyas Sanzgiri wrote:OR

you can use some of the beautiful free editors and focus on your code rather than

Try Netbeans IDE : www.netbeans.org


I respectfully disagree. Understanding how classpath and packages work (along with the general concepts of absolute and relatives paths) is important for any Java developer. Certainly IDEs are useful as projects become bigger and more complex and the developer becomes more comfortable with the fundamentals, but by skipping to just doing everything in an IDE rather than overcoming this relatively minor obstacle, the OP would be doing a disservice to his education.


I dont think this is the topic of conversation. It is to run the program.


That's pretty shortsighted. "Give a man a fish, and you feed him for a day. Teach a man to fish, and you feed him for life." It's not realistic to think the OP will only ever execute Java programs from within an IDE. He's going to have to learn to do it on the command line at some point, and since that's what he's trying and asking about now, it's perfectly appropriate to help him learn that aspect of the Java world.

With my experience, I only had to dig up on classpath and package for my SCJP. For everything else, including learning Java, Netbeans has worked wonders


Then that's extremely limited experience. I choose not to assume the OP's Java career will follow that pattern.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Vyas Sanzgiri wrote:can you your complete Java code here ?


That's not necessary. The problem isn't in the body of the code. The only part of the code we need to see here is the package statement, if there is one. (And, I suppose, the class name, to make sure it matches what's being provided on the command line in spelling and case.)
Jennifer Wright
Greenhorn

Joined: Nov 16, 2011
Posts: 5

thank you Jeff Verdegan

java -cp . Weather worked this time for some reason.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Raven Illusion wrote:thank you Jeff Verdegan

java -cp . Weather worked this time for some reason.


Cool. Glad you got it straightened out.

I caution you against the "for some reason" mentality though. If it worked this time, it will work every time you have the rest of the pieces in place. When things weren't working, you were doing something different. Maybe you forgot the -cp . or maybe Weather.class was not in your current directory. Point being--there's nothing magical or mysterious here. The behavior is consistent and predictable. :-)
Vyas Sanzgiri
Ranch Hand

Joined: Jun 16, 2007
Posts: 686

You may also try some IDEs which will help you focus on code and learn java. Of course there is classpath and packages.

Finally Jr is here to help
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1012
    
    5

Vyas

I believe the point Jeff is making is that an IDE can hide some of the fundamentals that every Java developer should know. Along with using the likes of Eclipse, NetBeans etc, it is crucial that a developer also understands the options available from the command line for the compiler and JVM.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

James Boswell wrote:Vyas

I believe the point Jeff is making is that an IDE can hide some of the fundamentals that every Java developer should know.


Yes, exactly, thank you. And in this particular case, since the OP was specifically trying to get Java working on the command line, I was pointing out that that would be a more beneficial path to follow in this thread.

(And, a bit off-topic, but since it came up, the other problem with IDEs for beginners is that they often can't tell where the line is between the IDE and Java.)
Vyas Sanzgiri
Ranch Hand

Joined: Jun 16, 2007
Posts: 686

James Boswell wrote:Vyas

I believe the point Jeff is making is that an IDE can hide some of the fundamentals that every Java developer should know. Along with using the likes of Eclipse, NetBeans etc, it is crucial that a developer also understands the options available from the command line for the compiler and JVM.


I am happy Jennifer could run the program

True I understand I was providing an alternative easier way that I endorse for learning Java through IDEs like Netbeans

There is no need to point all posts at me [pretty shortsighted, limited experience] for the my suggestions (which I know you & Jeff disagree)

Let us focus on helping the poster and provide them all alternatives.


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
Campbell Ritchie,
Your post was moved to a new topic.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
Discussion about the conduct of this thread in the link in the post I sent to myself.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Error when running a class