简体   繁体   English

运行调用外部库的 java 代码时出错

[英]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/ .我正在尝试运行在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 ".它提到“它依赖于 Parallel COLT,必须安装并在 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我下载了 ParallelCOLT 并保存在目录中

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

I saved the code from the section " Stand-alone Java code " in the directory (also given below)我保存了目录中“独立Java代码”部分的代码(也在下面给出)

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 usingHow to include jar files with java file and compile in command prompt 中得到提示,我尝试使用设置 ParallelColt 的路径

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.这将在没有(可见)错误的情况下执行并在Gibbs目录中生成Gibbs.class文件。

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错误:无法找到或加载主类:.Users.david.Documents.RWorkingDir.Gibbs.Gibbs
Caused by: java.lang.ClassNotFoundException:C:.Users.david.Documents.RWorkingDir.Gibbs.Gibbs引起: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错误:无法初始化主类 Gibbs
Caused by: java.lang.NoClassDefFoundError: cern/jet/random/tdouble/engine/DoubleRandomEngine引起: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:它可以使用以下命令从 OS shell 编译并独立运行:

javac Gibbs.java
java Gibbs 10 1000 1

You need to run the java command from the directory that contains .class and supply the same -classpath as during compilation with javac .您需要从包含.class的目录运行java命令,并提供与javac编译期间相同的-classpath

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 .如果您觉得这很乏味,请考虑构建一个可执行的 JAR

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM