Search code examples
javarbashrserve

How can execute command R CMD Rserve from Java


Im trying to start Rserve from my java backend using Runtime exec function. I can normally execute linux command like killall to stop the process but i was unable to start it. You can start Rserve from command line using this

R CMD Rserve

and works ok. But from java:

Process pr = Runtime.getRuntime().exec("R CMD Rserve")

its not working. How can i use this command from java then?


Solution

  • i get it work changing the way i was trying to invoke the process. The rzwitserloot's answer was really useful to figure this. I dont know about the internals of "R CMD" command but it dont like to java process builder. There is other way to invoke Rserver daemon within R console so i tried to do it this way. This works fine:

    List<String> args = List.of("/bin/sh", "-c","echo 'library(Rserve);Rserve(args=\"--no-save --slave\");'| /usr/lib/R/bin/R --no-save --slave");
            ProcessBuilder pb = new ProcessBuilder(args);
            Process p = pb.start();