gpt4 book ai didi

java - 运行使用 Maven 构建的 jar 会导致 "java.lang.NoClassDefFoundError: org/rosuda/JRI/Rengine"错误

转载 作者:塔克拉玛干 更新时间:2023-11-01 23:10:02 26 4
gpt4 key购买 nike

我正在尝试构建一个可以调用 R 代码的 JAR 库。我基本上希望这个 jar 能够在任何支持运行 jar 可执行文件的机器上运行(不需要单独的 R 软件)。为此,我正在使用 Maven。我能够毫无错误地编译和创建一个 jar。但是,当我运行它时,我无法产生成功的结果。

这是我的java代码

package com.company.analytics.timeseries;

import org.rosuda.JRI.REXP;
import org.rosuda.JRI.Rengine;

public class App {
public static void main(String[] args) {
System.out.println("Creating Rengine (with arguments)");
String[] Rargs = { "--vanilla" };
Rengine re = new Rengine(Rargs, false, null);
System.out.println("Rengine created, waiting for R");
if (!re.waitForR()) {
System.out.println("Cannot load R");
return;
}
System.out.println("Done.");
}
}

这是我的 pom.xml 文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.company.analytics</groupId>
<artifactId>timeseries</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>timeseries</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.nuiton.thirdparty</groupId>
<artifactId>JRI</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>org.rosuda.REngine</groupId>
<artifactId>REngine</artifactId>
<version>2.1.0</version>
</dependency>
</dependencies>

<repositories>
<repository>
<id>central</id>
<name>Maven Central</name>
<url>http://repo1.maven.org/maven2</url>
</repository>
</repositories>

</project>

我使用 mvn clean 然后使用 mvn package 创建 jar 文件。

C:\MVN\project\analytics\timeseries\target 中创建了一个 4KB 的 JAR 文件。然后,在 Windows 的命令行中,当我运行执行此 jar 文件时,出现以下错误

C:\MVN\project\analytics\timeseries\target\classes>java com.company.analytics.timeseries.App

Creating Rengine (with arguments)

Exception in thread "main" java.lang.NoClassDefFoundError: org/rosuda/JRI/Rengine

at com.company.analytics.timeseries.App.main(App.java:10)
Caused by: java.lang.ClassNotFoundException: org.rosuda.JRI.Rengine
at java.net.URLClassLoader.findClass(Unknown 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

我想弄清楚我犯了什么错误。我试图通过谷歌搜索找到答案,但无法修复。

最佳答案

因为我已经用一天的时间来反对这个了,我以后可能会忘记并引用这个页面——按照 Gergely Basco 在上面的评论中的建议,严格来说 R 和 rJava 都需要安装在机器上以解决实例化 org.rosuda.REngine.REngine 对象时的 Cannot find JRI native library! 问题,这不能通过 exclusively 方式完成在您的 pom.xml 中添加 JRIEngine 依赖项(糟糕)。

步骤(无论如何我是如何为我以后的图像做的):

  1. 安装 Brew(我只是碰巧将 Brew 用于其他依赖项)

     /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
  2. 使用 brew 安装 R:

    brew tap homebrew/science
    brew install R
  3. 用 R 安装 rJava(需要一点编译时间,喝杯咖啡)

    install.packages("rJava")
  4. 将 rJava/jri 添加到 java.library.path 类路径,将 R_HOME 添加到环境变量(您安装 R 的位置 - 在我的例子中,Brew 安装它的位置)。请注意,如果您尝试在您的 IDE 中运行它(我正在运行 IDEA16),它不会继承您在 ~/.bash_profile 中设置的路径,您需要在运行配置中设置它。

     -Djava.library.path="/usr/local/lib/R/3.3/site-library/rJava/jri/
    R_HOME=/usr/local/Cellar/r/3.3.1_2/R.framework/Resources
  5. 确保 maven 对 pom.xml 中的 JRIEngine 具有依赖性

    <dependency>
    <groupId>com.github.lucarosellini.rJava</groupId>
    <artifactId>JRIEngine</artifactId>
    <version>0.9-7</version>
    </dependency>
  6. 实例化 REngine(我需要这个版本以便将数据帧从 java 传递给 R)

    String[] Args = {"--vanilla"};
    REngine engine = REngine.engineForClass("org.rosuda.REngine.JRI.JRIEngine", Args, new REngineStdOutput (), false);

如果您使用回调参数 (new REngineStdOutput () ) 进行实例化,那么在运行时您应该得到的结果看起来像这样;否则,如果你只是用字符串 engineForClass("org.rosuda.REngine.JRI.JRIEngine") 实例化,你将不会在启动时/否则从 R 获得以下输出,这取决于你是否想要是不是:

    /**R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-apple-darwin15.5.0 (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.**/

希望这对将来的人有所帮助,使他们免于痛苦。

关于java - 运行使用 Maven 构建的 jar 会导致 "java.lang.NoClassDefFoundError: org/rosuda/JRI/Rengine"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32170664/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com