Skip to content
Prev 381659 / 398502 Next

Identifying presence of Java

Hello,

Here are two ways.

1. system2 returns the return value of the shell command so if Java is 
installed, it should return 0. In the first call it's 'java' (lowercase 
j), in the second 'Java' (uppercase J).

java <- system2('java', '-version')
java
#[1] 0

Java <- system2('Java', '-version')
Java
#[1] 127


2.
Another way is to redirect the shell output to file. In this case I'm 
redirecting to a temp file. Then, readLines from that file.

tmpfile <- tempfile()
if(!dir.exists(dirname(tmpfile))) dir.create(dirname(tmpfile))

system2('java', '-version', '>', tmpfile)
readLines(tmpfile)

unlink(tmpfile)  # final cleanup


Hope this helps,

Rui Barradas


?s 16:51 de 09/11/19, Dennis Fisher escreveu: