This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes NoClassDefFoundError Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "NoClassDefFoundError" Watch "NoClassDefFoundError" New topic
Author

NoClassDefFoundError

Divya Gopinath
Greenhorn

Joined: Nov 18, 2011
Posts: 1
Hello Everyone,

I have set my JAVA_HOME variable as E:\Program Files\Java\jdk1.6.0_21(system variable)
When I try to compile a simple program(which is in a folder called myproj) with this command -- E:\myproj\javac Ex.java -- it compiles fine. But when I try to run to run it, I am getting this error:: xampl

Program is like this::

public class Ex{
public static void main(String[] args){
System.out.println("hii");
}
}


E:\myproj>java Ex
Exception in thread "main" java.lang.NoClassDefFoundError: Ex
Caused by: java.lang.ClassNotFoundException: Ex
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: Ex. Program will exit.

Please help.

Regards,
Divya
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

JAVA_HOME is irrelevant here.

Assuming Ex.class is in your current directory:

java -cp . Exp

Google for something like java classpath tutorial for more details. Or search this site. I just gave a more detailed example a couple days ago.
Gaurav Raje
Ranch Hand

Joined: Jul 23, 2010
Posts: 136
another gotcha
if you are using packages

make sure the class is present in your/package/fully/qualified/name/folder
again classpath tutorials should explain it.
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

Divya Gopinath wrote:Hello Everyone,

I have set my JAVA_HOME variable as E:\Program Files\Java\jdk1.6.0_21(system variable)
When I try to compile a simple program(which is in a folder called myproj) with this command -- E:\myproj\javac Ex.java -- it compiles fine. But when I try to run to run it, I am getting this error:: xampl

Program is like this::

public class Ex{
public static void main(String[] args){
System.out.println("hii");
}
}


E:\myproj>java Ex
Exception in thread "main" java.lang.NoClassDefFoundError: Ex
Caused by: java.lang.ClassNotFoundException: Ex
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: Ex. Program will exit.

Please help.

Regards,
Divya

add this in your classpath or environment variables


The Only way to learn is ...........do!
Visit my blog http://inaved-momin.blogspot.com/
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

naved momin wrote:
add this in your classpath or environment variables


No, don't.

First, it's generally better not to use the CLASSPATH environment variable. Just put the classpath in the command line with a -cp or -classpath arg.

Second, Don't put the bin directory (or anything under your Java installation's root) on your classpath.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38519
    
  23
Agree. There are so many people and books telling you to set a system classpath, but that advice is mistaken.
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

Campbell Ritchie wrote:Agree. There are so many people and books telling you to set a system classpath, but that advice is mistaken.

can you elaborate your point why doing this is a mistake ?
if he doesnt add that into classpath in environment variable , he need to set the classpath every time he execute java codes
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

naved momin wrote:
Campbell Ritchie wrote:Agree. There are so many people and books telling you to set a system classpath, but that advice is mistaken.

can you elaborate your point why doing this is a mistake ?


CLASSPATH environment variable is okay when your just barely getting started with Java and all you have is one directory with your handful of classes in it. However, once you start doing any serious development, you'll have different projects with their own class tree directories, and each one may need different combinations of thirdparty jars. At that point, if you're using the CLASSPATH environment variable, you'll have to keep changing it every time you switch projects.

Since each project or app has its own classpath needs anyway, you might as well just set it when you invoke the jvm with the -cp argument. If there's a lot there, create a shell script or batch file that does it, and use that script to invoke your app.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NoClassDefFoundError