user2957945
user2957945

Reputation: 2413

Error running java code that calls external library

I am trying to run some code found at https://darrenjw.wordpress.com/2011/01/01/calling-java-code-from-r/. It mentions that "It relies on Parallel COLT, which must be installed and in the Java CLASSPATH". This is what I am struggling to do.

This is what I have done (I've included my full paths / directory structure in case the points to some error)

I downloaded ParallelCOLT and saved in the directory

C:/Users/david/Documents/RWorkingDir/javaJAR/ParallelColt

I saved the code from the section "Stand-alone Java code" in the directory (also given below)

C:/Users/david/Documents/RWorkingDir/Gibbs/Gibbs.java

Taking a hint from How to include jar files with java file and compile in command prompt, I have tried to set the path to ParallelColt using

javac -classpath ".;C:/Users/david/Documents/RWorkingDir/javaJAR/ParallelColt/parallelcolt-0.9.4.jar;" 
         C:/Users/david/Documents/RWorkingDir/Gibbs/Gibbs.java # split for presentation

This executes without (visible) error and produced the Gibbs.class file in the Gibbs directory.

I have been unable to run this without error:

C:\>java C:/Users/david/Documents/RWorkingDir/Gibbs/Gibbs 10 1000 1

Error: Could not find or load main class:.Users.david.Documents.RWorkingDir.Gibbs.Gibbs
Caused by: java.lang.ClassNotFoundException:C:.Users.david.Documents.RWorkingDir.Gibbs.Gibbs

and trying to run from the actual directory

C:\>cd C:/Users/david/Documents/RWorkingDir/Gibbs/
C:\Users\david\Documents\RWorkingDir\Gibbs>java Gibbs 10 1000 1

Error: Unable to initialize main class Gibbs
Caused by: java.lang.NoClassDefFoundError: cern/jet/random/tdouble/engine/DoubleRandomEngine

I have had a read of What does "Could not find or load main class" mean? but have not found the error. Where are my errors please?


code from webpage:

import java.util.*;
import cern.jet.random.tdouble.*;
import cern.jet.random.tdouble.engine.*;

class Gibbs {

    public static void main(String[] arg) {
        if (arg.length != 3) {
            System.err.println("Usage: java Gibbs <Iters> <Thin> <Seed>");
            System.exit(1);  
        }
        int N = Integer.parseInt(arg[0]);
        int thin = Integer.parseInt(arg[1]);
        int seed = Integer.parseInt(arg[2]);
        DoubleRandomEngine rngEngine=new DoubleMersenneTwister(seed);
        Normal rngN=new Normal(0.0,1.0,rngEngine);
        Gamma rngG=new Gamma(1.0,1.0,rngEngine);
        double x=0,y=0;
        System.out.println("Iter x y");
        for (int i=0;i<N;i++) {
           for (int j=0;j<thin;j++) {
               x=rngG.nextDouble(3.0,y*y+4);
               y=rngN.nextDouble(1.0/(x+1),1.0/Math.sqrt(x+1));
           }
        System.out.println(i+" "+x+" "+y);
        }
    } 
}

It can be compiled and run stand-alone from an OS shell with the following commands:

javac Gibbs.java
java Gibbs 10 1000 1

Upvotes: 2

Views: 105

Answers (1)

Karol Dowbecki
Karol Dowbecki

Reputation: 44970

You need to run the java command from the directory that contains .class and supply the same -classpath as during compilation with javac.

cd C:/Users/david/Documents/RWorkingDir/Gibbs/
java -classpath ".;C:/Users/david/Documents/RWorkingDir/javaJAR/ParallelColt/parallelcolt-0.9.4.jar;" Gibbs 10 1000 1

If you find this tedious consider building an executable JAR.

Upvotes: 2

Related Questions