Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 03-26-2008, 03:31 PM   #1
Registered: Jul 2003
Location: United States
Distribution: Fedora Core 7 and older, Knoppix, Ubuntu
Posts: 121

Rep: Reputation: 15
Java NoClassDefFoundError when using Jama

I am trying to write a program in Java to do some matrix computations.
I have the code in a directory called bin. Inside bin there is a directory Jama. Jama is the package containing all the linear algebra classes.

If I attempt to execute a test program called MatrixTest I get a NoClassDefFoundError: Jama/Matrix

I have looked everywhere and all the suggestions suggest looking at my classpath or making sure the name of the public class in the is the same as the file name (which it is). I had assumed that Jama would be in the classpath since if was in the directory that I was compiling in but I wasn't sure so I manually added it anyway. This did not help.

The code is in the package bin. This is what is causing the problem, but I do not know why. I want to execute the program from outside of the bin directory, and if I attempted to do this it told me that there was a wrong name. Then I add it to the package bin (by writing pakage bin; at the top of the java file) and try to execute again. And the code starts to execute fine (it prints the stuff it should to the screen) and then once it tries to instantiate a matrix I get the Jama/Matrix

Does any one know why it cannot find the Jama.Matrix class in runtime if the executing file is part of a package?
(If I do not have the MatrixTest class part of the package bin I can run it from inside the bin directory with no problem, everything works, but as I said I would like to run it from outside of the bin directory).
Old 03-26-2008, 08:33 PM   #2
Senior Member
Registered: Nov 2005
Distribution: Arch
Posts: 3,136

Rep: Reputation: 1336Reputation: 1336Reputation: 1336Reputation: 1336Reputation: 1336Reputation: 1336Reputation: 1336Reputation: 1336Reputation: 1336Reputation: 1336
Java packages are supposed to correspond to the file system directories. So if you do java bin/MatrixTest is the same as java bin.MatrixTest. If you have code in bin/Jama, they are supposed to be in package bin.Jama. To execute binaries in a different directory you set PATH, to execute java classes in different directories, you set CLASS_PATH.

In short, don't put MatrixTest in the bin package, do
 java -cp bin MatrixTest
Old 03-28-2008, 08:53 AM   #3
Damon Spector
Registered: Nov 2004
Distribution: Fedora Core 4
Posts: 61

Rep: Reputation: 15
I'm getting the same kind of error when I try and run a Java application but with more on it.

here is the whole message

Exception in thread "main" java.lang.NoClassDefFoundError: MegaMek.jar
at (/usr/lib/
Caused by: java.lang.ClassNotFoundException: MegaMek.jar not found in gnu.gcj.runtime.SystemClassLoader{urls=[file:./,file:./], parent=gnu.gcj.runtime.ExtensionClassLoader{urls=[], parent=null}}
at (/usr/lib/
at java.lang.ClassLoader.loadClass(java.lang.String, boolean) (/usr/lib/
at java.lang.ClassLoader.loadClass(java.lang.String) (/usr/lib/
at java.lang.Class.forName(java.lang.String, boolean, java.lang.ClassLoader) (/usr/lib/
at (/usr/lib/

can you please tell me what is wrong and how to fix it. thank you
Old 03-28-2008, 10:33 AM   #4
Senior Member
Registered: Nov 2005
Distribution: Arch
Posts: 3,136

Rep: Reputation: 1336Reputation: 1336Reputation: 1336Reputation: 1336Reputation: 1336Reputation: 1336Reputation: 1336Reputation: 1336Reputation: 1336Reputation: 1336
I think you need the -jar option when running jar files.
Old 03-28-2008, 04:59 PM   #5
Damon Spector
Registered: Nov 2004
Distribution: Fedora Core 4
Posts: 61

Rep: Reputation: 15
ok I tried it with the -jar but now I'm getting this.

Exception during runtime initialization
<<No stacktrace available>>
Old 03-29-2008, 05:23 AM   #6
LQ Guru
Registered: Nov 2006
Location: Belgium
Distribution: Ubuntu 11.04, Debian testing
Posts: 5,019

Rep: Reputation: 130Reputation: 130
classpath fun ...

- make sure that everything is in the proper directory
- make sure that each package has a package declaration that corresponds to its position within the directory: if the top level is package some.example; then packages at the next level down would be some.example.bin (assuming that they are in a folder called "bin").
- make sure that your source file has import statements if it needs files from a different directory
- you need to tell your compiler and/or interpreter where they are located if your application is not part of your standard classpath. If you need to compile something inside the current dir:
java -cp .
This works fine if both:
- someProgram is in your current directory
- it does not contain any references to source/class files that are inside another directory (with the exception of a file in the top level directory - but only providing that files in lower directories do not themselves reference files located in a different directory - this may work but just as often it will fail).
The reason is that the compiler takes your package statements literally. Here is an example: there are two file inside bin, A and B. The first is some.example.bin.A, the second some.example.bin.B. Now if you are inside some/example/bin and you run javac -cp . A, the compiler will find A but not B... Why? Because when the compiler looks for B, it finds a file that says "package some.example.bin.B" so it is going to follow that path from the current directory - which obviously is not going to work: there isn't any some/example/bin inside some/example/bin; So in order to make it work, you would make sure that you are inside some/example/bin and then run:
javac -cp .;../../../
This instructs the compiler to look not only inside the current directory; it also makes it climb up three levels to do a search for B from there. When it looks for some/example/bin from three levels up, it does find B.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
java.lang.NoClassDefFoundError robbbert Programming 6 07-16-2006 06:47 AM
Java: java.lang.NoClassDefFoundError: javaapplication8/Factorial chief_officer Programming 9 05-31-2006 03:28 PM
java exception: java.lang.NoClassDefFoundError kath Programming 12 05-11-2006 04:37 AM
java.lang.NoclassDefFoundError rlnd Programming 5 02-18-2006 04:54 AM
I've set the CLASSPATH, but I still get a noClassDefFoundError. (Java) Travis86 Programming 6 02-05-2004 09:09 AM

All times are GMT -5. The time now is 02:17 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration