Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a Java (.jar) in R?

Tags:

java

r

Is there any way to run a Java jar file in R? I want one of the outputs of a .jar be used in R.

like image 825
Letícia Raposo Avatar asked Mar 16 '23 10:03

Letícia Raposo


1 Answers

You can use system to run an operating system command from R.

To run a java jar on a well-configured system, this should work:

 system("java -jar /path/to/my.jar")

You can add other parameters, for example maybe the Java code takes input from a file which you write with R and pass the filename. Then output from the Java code could be written to files and then read from R. Without knowing what the Java code does we can't be more specific.

It is possible to directly interface with Java code but that requires full knowledge of the internals of the jar so you know what functions to call with what parameters. This is usually referred to as the "API" for that Java.

Otherwise, write a file, call system, read a file, is sometimes the simplest way to run code in other languages.

like image 158
Spacedman Avatar answered Mar 28 '23 11:03

Spacedman