Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NoClassDefFoundError

 
Divya Gopinath
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 136
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 692
Eclipse IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47216
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree. There are so many people and books telling you to set a system classpath, but that advice is mistaken.
 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic