aspose file tools*
The moose likes Java in General and the fly likes NoClassDefFoundError!? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "NoClassDefFoundError!?" Watch "NoClassDefFoundError!?" New topic
Author

NoClassDefFoundError!?

Pourang Emami
Ranch Hand

Joined: Sep 21, 2002
Posts: 127
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


Best Regards,<br />Pourang Emami
Jeff Langr
author
Ranch Hand

Joined: May 14, 2003
Posts: 762
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-


Books: Agile Java, Modern C++ Programming with TDD, Essential Java Style, Agile in a Flash. Contributor, Clean Code.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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

Indeed, where I am sending it.


[Jess in Action][AskingGoodQuestions]
Pourang Emami
Ranch Hand

Joined: Sep 21, 2002
Posts: 127
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
Ranch Hand

Joined: May 14, 2003
Posts: 762
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

Joined: Sep 21, 2002
Posts: 127
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
Ranch Hand

Joined: May 14, 2003
Posts: 762
The Ant build tool, an invaluable tool to learn, will do this for you.
http://ant.apache.org
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8968
    
    9

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.


[How To Ask Questions On JavaRanch]
Jeff Langr
author
Ranch Hand

Joined: May 14, 2003
Posts: 762
oh yeah and that! oops.
-J-
Pourang Emami
Ranch Hand

Joined: Sep 21, 2002
Posts: 127
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

Joined: Sep 21, 2002
Posts: 127
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
???
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

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.


http://home.arcor.de/hirnstrom/bewerbung
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: NoClassDefFoundError!?