user8472 user8472 - 2 years ago 208
Java Question

Commons Math: java.lang.ClassNotFoundException

I have this tiny bit of Java code:

import org.apache.commons.math3.linear.*;

class Test
public static void main(String[] args){
RealVector alpha= MatrixUtils.createRealVector(new double[10]);

I can compile it successfully using
javac -cp .;commons-math.jar

But when I try to run it using
java Test -cp .;commons-math.jar
, it throws up this:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/ma
at Test.main(
Caused by: java.lang.ClassNotFoundException: org.apache.commons.math3.linear.Mat
at$ Source)
at Method)
at Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 1 more

From the Googling I did, a
usually occurs when your classpath isn't pointing to the right location. But since my code compiles, I don't see why it shouldn't execute. Any ideas?

Answer Source

If you type java into a command window you actually get how to setup a command to start a class file correctly:

Usage: java [-options] class [args...] (to execute a class) or java [-options] -jar jarfile [args...] (to execute a jar file)

"-cp" is an option so it goes before the class name

So a command script would look like this:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download