tags:

views:

193

answers:

4

Greetings,

I'm playing around with mahout, I've written a basic java class which imports some of the libraries. It seems my classpath is correct when compiling, I get no errors or complaints at all.

However when I run the compiled class I get an exception saying...

Exception in thread "main" java.lang.NoClassDefFoundError: Test
Caused by: java.lang.ClassNotFoundException: Test
+5  A: 

My guess is that . is not on your classpath. For example, you might be compiling with:

javac -cp foo.jar:bar.jar Test.java

but then to run the code you'd need

java -cp foo.jar:bar.jar:. Test

The code that you're compiling doesn't need to be on the classpath as you're providing the code (so there's nothing to find) - that's why it manages to compile but not run.

That's only a guess, of course - if you could post the commands you're using to compile and run the code, that would help.

Jon Skeet
I've tried adding the . as suggested with the java command. I'm now getting an error saying java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory . Would this be due to my classpath being incorrect in someway as it points to the first actual line of code? Javac still works fine.
steve
p.s java as follows javac -cp /home/bob/trunk/core/src/main/java/:/home/bob/javadep/uncommons-math/:/home/bob/javadep/slf4jx:. Test.java , also t he exact same for java
steve
@scott: You probably need to refer to the jar file within the slf4jx directory, not just the directory itself. How did you get it to compile before?
Jon Skeet
@Jon thats what I've been doing the entire time. I've just removed the entire reference to slf4j (After trying just the JAR and getting the same error). It compiles fine, but obviously wont run using java. Therefore I can at least see my javac is actually not highlighting problems. I'll look into it further. Thanks :)
steve
@steve: Are you sure you're actually compiling all of your code?
Jon Skeet
Hi Jon, Turns out my classpath was just fubar. I cheated and used eclipse to work out if I was doing something wrong. I'll upload the actual classpath as soon as possible.
steve
+1  A: 

Compile time classpath sounds right; runtime classpath is wrong.

From the javadocs for that class:

Thrown if the Java Virtual Machine or a ClassLoader instance tries to load in the definition of a class (as part of a normal method call or as part of creating a new instance using the new expression) and no definition of the class could be found.

The searched-for class definition existed when the currently executing class was compiled, but the definition can no longer be found.

Do you see a Test.class file in the current directory? Maybe you compiled it to another path by mistake.

duffymo
If this is the case, what should i be considering when setting runtime classpath/ I'm using exact same classpath as compile time
steve
You need a classpath that includes Test.class, obviously.
duffymo
Thanks, but class file is in the current directory.
steve
+2  A: 

I'm now getting an error saying java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory

You're missing slf4j-api.jar on your class path. With SLF4J, you always need slf4j-api.jar and another jar to bind a logging framework. And actually, if you don't care about logging, use slf4j-nop.jar instead of slf4j-log12.jar.

Update: Mahout seems to be available in Maven central repository so using Maven could ease the class path setup process. And if you're not into learning Maven, consider using MOP which is a command line launcher to run Java stuff that can transparently download Maven artifacts and their dependencies and setup your classpath.

Pascal Thivent
My latest approach is just dump every single jar into the home dir and use classpath as . , however I did also try your approach as well which threw the Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactoryI can only assume that this will be a long process to fix :) and is probably down to mahout using various api's etc.
steve
@steve `org/slf4j/LoggerFactory` is in `slf4j-api.jar` (at least in the version 1.4.2). Try to use the same version as mahout (i.e. 1.5.8).
Pascal Thivent
Thanks pascal, i'll check mavan out. I've been using that to run it via jetty but i wanted to write stand alone classes
steve
A: 

If you are using Mahout, be aware that after you build it with Maven, it will generate "*.job" files in the target/ directory, which contain all dependencies packaged together. It is just a .jar file.

Sean Owen