如何从 java 执行 R 脚本文件



我想从Java调用R脚本。我已经对该主题进行了谷歌搜索,但是我看到的几乎所有结果都需要我将依赖项添加到某些第三方库中。谁能告诉我一种在不向代码中添加任何依赖项的情况下完成相同事情的好方法?

我正在使用以下行设置一个新应用程序,该应用程序从java运行R脚本文件

Runtime.getRuntime().exec("C:\Users\Thiru\Desktop helloWorld.R");

但是当我运行应用程序时,我收到以下异常:

SEVERE: Servlet.service() for servlet [RenjinServlet] in context with 
path 
[/REngineCheck] threw exception
java.io.IOException: Cannot run program "C:UsersThiruDesktop": 
CreateProcess error=5, Access is denied
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at org.r.Check.RenjinServlet.doGet(RenjinServlet.java:102)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at 
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter 
(ApplicationFilterChain.java:291)
at 
org.apache.catalina.core.ApplicationFilterChain.doFilter
(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter (WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter 
(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter 
(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke 
(StandardWrapperValve.java:217)
at org.apache.catalina.core.StandardContextValve.invoke 
(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke 
(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke 
(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke 
(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke 
(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke 
(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service 
(CoyoteAdapter.java:518)
at org.apache.coyote.http11.AbstractHttp11Processor.process 
(AbstractHttp11Processor.java:1091)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process 
(AbstractProtocol.java:673)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun 
(NioEndpoint.java:1500)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run 
 (NioEndpoint.java:1456)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run 
  (TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
Caused by: java.io.IOException: CreateProcess error=5, Access is denied
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
... 28 more

您应该在括号中设置路径,例如

("your_path\Rscript.exe your_path\文件名。R")

如果设置环境,则可以使用Rscript

例:

Runtime.getRuntime().exec("C:\Program Files\R\R-3.6.1\bin\x64\Rscript.exe C:\Users\Thiru\Desktop\helloWorld.R");

相关内容

  • 没有找到相关文章

最新更新