• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

NoClassDefFoundError!?

 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any help would be appreciated:
I have set the CLASSPATH to <jdk_home director>\bin
and every thing is OK(file name, method signature, ...),
but when I try to run "MyfileName.class" I am getting the following known Exception for no obvious reason:
>java MyfileName
>Exception in thread "main" java.lang.NoClassDefFoundError: MyfileName
Thanks
 
author
Posts: 799
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This belongs in the "beginners" forum.
You are apparently confusing the classpath with the path. Your operating system uses the path to determine the location of executables, such as javac.exe or java.exe. Java uses the classpath to determine the location of compiled Java classes.
You should set the classpath to the directory in which your class resides. If you are in the same directory as MyClass, you can execute:

The dot indicates the current directory.
Classpath considerations are slightly more involved when you begin to use packages to organize your classes.
-Jeff-
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jeff Langr:
This belongs in the "beginners" forum.


Indeed, where I am sending it.
 
Pourang Emami
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Than you Mr.Langer for your useful reply. I followed your instruction and it worked, but there is some thing strange:
I have the following in filing system:
C:\hello\Test.java
1-I put C:\hello in CLASSPATH.
2- Compile the Test.java(Test.java has �package hello;� in it�s code. So it�s class is supposed to be in a folder named hello.)
C:\hello>javac Test.java
3-Now none of the following work:
C:\hello>java Test
C:\hello>java hello.Test
To have it run I should create a folder "hello" under the main folder "hello" and put the Test.class inside that:
C:\hello\hello\Test.class
And now it will be run by the following commond:
any where>java hello.Test
So there should be some attribute for javac to create the folder with the same name as the package and put the class file inside that folder?
 
Jeff Langr
author
Posts: 799
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Something like that. Your package and your directory structure must correspond. If you have a package com.langrsoft.junk, then you will have a directory ./com/langrsoft/junk. The classpath must contain the directory in which ./com appears.
So, for example, if you have the path c:\eclipse\workspace\bigProject\classes\com\langrsoft\junk, and this directory contains classes belonging to com.langrsoft.junk, then c:\eclipse\workspace\bigProject\classes would appear on the classpath.
Clear as mud?
 
Pourang Emami
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So clear sir. Thank you so much Mr. Langr.
Is there any way that during compilation the packages declared in the code would be created automatically and the compiled classes will be located inside thoes folders?
 
Jeff Langr
author
Posts: 799
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Ant build tool, an invaluable tool to learn, will do this for you.
http://ant.apache.org
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The -d flag of javac lets you specify the root directory of a package hierarchy. It will create the package directories and place the .class files in the correct directories.
 
Jeff Langr
author
Posts: 799
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh yeah and that! oops.
-J-
 
Pourang Emami
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you my professional friends, but the thing is that:
In spite of declaring "C:\j2sdk1.4.2\bin"(my jdk directory) inside PATH, the "javac" command is not recognized in the command prompt and I get the following message:
c:\>javac
Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/tools/javac/Main
???
 
Pourang Emami
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you my professional friends, but the thing is that:
In spite of declaring "C:\j2sdk1.4.2\bin"(my jdk directory) inside PATH, the "javac" command is not recognized in the command prompt and I get the following message:
c:\>javac
Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/tools/javac/Main
???
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try setting the JAVA_HOME=C:\j2sdk1.4.2.
If the error still occurs, check your C:\j2sdk1.4.2\lib and %JAVA_HOME%\JRE\lib.
 
reply
    Bookmark Topic Watch Topic
  • New Topic