0

I would like to call an R script from Java. I have done google searches on the topic, but almost all of the results I have seen would require me to add a dependency to some third party library. Can anyone show me a good way to accomplish the same thing without adding any dependencies to my code?

I'm setting up a new application which runs R script file from java using the following line

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

but when I run the application I am getting following exception:

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
5
  • If your windows is not configured to use Rscript to run files ending in .R, you may need to do Runtime.getRuntime().exec("C:\path\to\Rscript.exe C:\Users\Thiru\Desktop\hellowWorld.R"). (I'm not familiar enough with java to know for sure if that should be a string with the cmd and all args, or if you need to pass a list with the cmd as the first, and your R script as the second element.) Commented Dec 29, 2018 at 18:58
  • Dear @r2evans, It's not working I am getting file not found exception, but when I run like this in CMD I am getting the out put in cmd. E:\E Drive\Softwares\R docs\R-3.5.1\bin\x64>Rscript C:\Users\Thiru\Desktop\helloWorld.R Commented Dec 31, 2018 at 10:55
  • What happens if you try Runtime.getRuntime().exec("E:\E Drive\Softwares\R docs\R-3.5.1\bin\x64>Rscript C:\Users\Thiru\Desktop\helloWorld.R")? Commented Dec 31, 2018 at 16:13
  • @r2evans, I am getting file not found exception when I run like this. Commented Jan 4, 2019 at 5:27
  • My guess (again, not a java guy) is that it had to do with including both the executable and its argument(s) in one string. Do you know how to use Runtime.getRuntime().exec? I do not. Commented Jan 5, 2019 at 13:39

1 Answer 1

1

You should set up your path in a bracket like

("your_path\Rscript.exe your_path\filename.R")

If you set the environment, you can use Rscript

Example:

Runtime.getRuntime().exec("C:\\Program Files\\R\\R-3.6.1\\bin\\x64\\Rscript.exe C:\\Users\\Thiru\\Desktop\\helloWorld.R");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.