Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

NoClassDefFoundError

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 74354
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic