aspose file tools*
The moose likes Beginning Java and the fly likes Getting Exception while running a class file. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Getting Exception while running a class file." Watch "Getting Exception while running a class file." New topic
Author

Getting Exception while running a class file.

Vishnu Sharma
Ranch Hand

Joined: Feb 03, 2010
Posts: 55
Hi,
I am able to compile .java file, but while executing the .class file, i am getting following exception.

Exception in thread "main" java.lang.NoClassDefFoundError: Example
Caused by: java.lang.ClassNotFoundException: Example
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:303)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:316)
Could not find the main class: Example. Program will exit.

Please tell, what can be cause for this??
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Your class needs to be in the classpath. If you are executing it from the same directory, then you can use "java -cp . Example" to tell the JVM that the current directory should be in the classpath.


[My Blog]
All roads lead to JavaRanch
Swati Save
Greenhorn

Joined: Mar 06, 2010
Posts: 14
Hey hi,

you must save your program with the same name as your class name.
if you are taking your class name as Test and while saving your writing test.java
program will compile without any error but while execution it will give such exception.

Cheers!
Vishal Srivastav
Ranch Hand

Joined: Nov 29, 2008
Posts: 46

Swati Save wrote:
you must save your program with the same name as your class name.
if you are taking your class name as Test and while saving your writing test.java


This is not necessary. Its only needed if your class is public. Just lookout for the class which contains main method. You should use the classpath switch -cp while running the class if you are getting such exception.


Vishal Srivastava, Software Engineer (Android), Paradigm Creatives
SCJP 5, SCWCD 5 - http://in.linkedin.com/in/srivastavavishal
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36514
    
  16
Swati Save wrote: . . . if you are taking your class name as Test and while saving your writing test.java
program will compile . . .
No it won't. Java is case-sensitive and will complain about the difference between test and Test.
Vishnu Sharma
Ranch Hand

Joined: Feb 03, 2010
Posts: 55
Christophe Verré wrote:Your class needs to be in the classpath. If you are executing it from the same directory, then you can use "java -cp . Example" to tell the JVM that the current directory should be in the classpath.


Hi,
Here is the program,

public class Example {
public static void main (String args[]) {
System.out.println("This is a simple Java program.");
}
}


I have saved this file with name "Example.java". I am able to get "Example.class". But JVM is not running bytecode & giving that exception.
Can you please tell how to confirm, that Example class is in classpath or not??


Regards,
Vishu
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36514
    
  16
Have you set a system CLASSPATH variable? What operating system are you using? Have a look at this "Common Problems" link, and use ctrl-F to find the error you are suffering. It tells you how to get rid of your CLASSSPATH (for the lifetime of the current command prompt). Does that help at all?
Vishnu Sharma
Ranch Hand

Joined: Feb 03, 2010
Posts: 55
Campbell Ritchie wrote:Have you set a system CLASSPATH variable? What operating system are you using? Have a look at this "Common Problems" link, and use ctrl-F to find the error you are suffering. It tells you how to get rid of your CLASSSPATH (for the lifetime of the current command prompt). Does that help at all?


Hi,

Classpath is set to C:\Program Files\Java\jdk1.6.0_17\lib. OS is Windows XP. my class file is getting created at desktop, where i have kept my .java file.
So, you want to say that only classpath is causing this error. Right??

Regards,
Vishu
09988883238
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36514
    
  16
Delete that CLASSPATH entirely. I presume you have your PATH set correctly otherwise you would have "javac is not recognized . . ." errors.

Then try again.
Vishnu Sharma
Ranch Hand

Joined: Feb 03, 2010
Posts: 55
Campbell Ritchie wrote:Delete that CLASSPATH entirely. I presume you have your PATH set correctly otherwise you would have "javac is not recognized . . ." errors.

Then try again.


Hi,
It worked. Thanks

Regards,
Vishu
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36514
    
  16
Well done, and "you're welcome"
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting Exception while running a class file.
 
Similar Threads
Exception: java.lang.ClassVersionError:
Problem: Could not find the main class: weblogic.appc. Program will exit.
Why am I not able to this code in eclipse?
help with marcus questions
help !!