我想从Java调用R脚本。我已经对该主题进行了google搜索,但是我看到的几乎所有结果都需要我向某些第三方库添加依赖项。任何人都可以向我展示一种在不添加任何依赖项的情况下完成同一件事的好方法吗?
我正在设置一个新的应用程序,该应用程序使用以下行从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:\Users\Thiru\Desktop":
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 \ filename.R”)
如果设置环境,则可以使用Rscript
例:
Runtime.getRuntime().exec("C:\\Program Files\\R\\R-3.6.1\\bin\\x64\\Rscript.exe C:\\Users\\Thiru\\Desktop\\helloWorld.R");