LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices



Reply
 
Search this Thread
Old 03-26-2008, 04:31 PM   #1
jdwilder
Member
 
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 MatrixTest.java 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 MatrixTest.java 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 java.land.NoClassDefFoundError: 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, 09:33 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,541

Rep: Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878
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
Code:
 java -cp bin MatrixTest
 
Old 03-28-2008, 09:53 AM   #3
Damon Spector
Member
 
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 gnu.java.lang.MainThread.run() (/usr/lib/libgcj.so.6.0.0)
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 java.net.URLClassLoader.findClass(java.lang.String) (/usr/lib/libgcj.so.6.0.0)
at java.lang.ClassLoader.loadClass(java.lang.String, boolean) (/usr/lib/libgcj.so.6.0.0)
at java.lang.ClassLoader.loadClass(java.lang.String) (/usr/lib/libgcj.so.6.0.0)
at java.lang.Class.forName(java.lang.String, boolean, java.lang.ClassLoader) (/usr/lib/libgcj.so.6.0.0)
at gnu.java.lang.MainThread.run() (/usr/lib/libgcj.so.6.0.0)


can you please tell me what is wrong and how to fix it. thank you
 
Old 03-28-2008, 11:33 AM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,541

Rep: Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878
I think you need the -jar option when running jar files.
 
Old 03-28-2008, 05:59 PM   #5
Damon Spector
Member
 
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
java.lang.NullPointerException
<<No stacktrace available>>
 
Old 03-29-2008, 06:23 AM   #6
jay73
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 . someProgram.java
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 .;../../../ A.java
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.
 
  


Reply


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 07:47 AM
Java: java.lang.NoClassDefFoundError: javaapplication8/Factorial chief_officer Programming 9 05-31-2006 04:28 PM
java exception: java.lang.NoClassDefFoundError kath Programming 12 05-11-2006 05:37 AM
java.lang.NoclassDefFoundError rlnd Programming 5 02-18-2006 05:54 AM
I've set the CLASSPATH, but I still get a noClassDefFoundError. (Java) Travis86 Programming 6 02-05-2004 10:09 AM


All times are GMT -5. The time now is 12:53 PM.

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