如何运行 jar 文件中的 R 脚本

发布于 2025-01-13 15:49:17 字数 190 浏览 2 评论 0原文

我有 Java 应用程序,它部署为 jar 并通过调用 main 函数从 cmd 行运行。有一些用 R 编写的与应用程序相关的工具随应用程序一起分发。

我想启用从 cmd 行或从 Java 调用 jar 文件中的 R 脚本。

当 R 脚本位于 jar 文件中时(无需解压缩 jar 文件),如何从 cmd 行或 Java 执行 R 脚本。

I have Java application that is deployed as a jar and run from a cmd line by calling a main function. There are some tools that are written in R associated with the application that are distributed with the application.

I'd like to enable calling the R scripts that are in the jar file from either the cmd line or from Java.

How can I execute an R script from either the cmd line or Java when the R script is in a jar file (with out unzipping the jar file).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

星軌x 2025-01-20 15:49:17

您可以通过获取 R 脚本的 InputStream 并读取文件内容来访问 R 脚本:

try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream("script.R")) {
    //read from inputStream
} catch (IOException e) {
    e.printStackTrace();
}

我建议您详细了解 JAR 的打包以及如何包含/存储资源。

有了 R 脚本的内容,您可以使用本文中提到的方法来执行它(使用库执行 R)。

You can access the R script by getting an InputStream for it and reading the file content:

try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream("script.R")) {
    //read from inputStream
} catch (IOException e) {
    e.printStackTrace();
}

I'd recommend reading more into the packing of JARs and how to include/store resources.

With the contents of the R script, you can execute it using approaches such as the one mentioned in this article (using libraries to execute R).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文