[R] Identifying presence of Java

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Sat Nov 9 18:37:54 CET 2019


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:
> R 3.6.3
> OSX and Windows
> 
> Colleagues
> 
> I want to identify if Java is installed on a particular computer.
> 
> When I execute
> 	Java -version
> in a terminal (OSX), but not in R, there are two outcomes:
> 	
> Java installed yields:
> 	java version "13.0.1" 2019-10-15
> 	Java(TM) SE Runtime Environment (build 13.0.1+9)
> 	Java HotSpot(TM) 64-Bit Server VM (build 13.0.1+9, mixed mode, sharing)
> 
> Java not installed yields:
> 	No Java runtime installed
> 
> I assume that there are comparable outputs in Windows.
> 
> I would like to capture this output in R using the system command, then search for “No Java runtime installed” )or the correponding text in Windows).
> 
> I execute something like:
> 	CAPTURE	<- system("Java -version", intern=TRUE, ignore.stderr=FALSE, ignore.stdout=FALSE)	
> (with various permutations of TRUE/FALSE for the options) but I cannot capture what is displayed on the console.
> 
> I have also tried “system2” with various TRUE/FALSE permutations without success.
> 
> Any clever ideas?
> 
> Dennis
> 
> Dennis Fisher MD
> P < (The "P Less Than" Company)
> Phone / Fax: 1-866-PLessThan (1-866-753-7784)
> www.PLessThan.com
> 
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list