i am not able to get the system2 result. tried to execute the sample jar file.
execute_system.R:
workingDir <- \"C:/Code\"
setwd(work
Using your code, it worked for me without error when I did the following:
system2('java', args = c('-jar', 'sample.jar', '123'),
stdout = 'stdout.txt', stderr = 'stderr.txt')
I ran this on Mac OSX 10.10.5 (Yosemite). The results were printed to "stdout.txt".
It seems from the documentation for system2
, the first option is just a command (i.e. no arguments). The arguments are to be specified with the args
parameter.
Here is a snippet from the Details section:
Details
Unlike system, command is always quoted by shQuote, so it must be a single command without arguments.
...
This is an easy mistake to make.
First, let's define some terms:
bash
shell uses semicolons (for foreground execution) or ampersands (for background), while the Windows cmd
shell uses ampersands (for foreground).The design of the system2() function seems to suggest that the authors only intended it to be used to run simple commands. It takes the command word as the first function argument (expected to be a scalar string, meaning a one-element character vector) and the command arguments as the second (also expected to be a character vector, zero or more elements). Here's how the documentation puts it in the description of these two function arguments:
command
the system command to be invoked, as a character string.
args
a character vector of arguments to
command
.
The above doesn't make it perfectly clear, but the first sentence of the Details section helps:
Unlike
system()
,command
is always quoted byshQuote()
, so it must be a single command without arguments.
As you can see, the documentation is a little bit vague in that it throws around the general term command without much clarification. They also use the vague term system command, which doesn't help matters much either. What they mean is that the first function argument command
is intended to be a command word of a simple command. If you want to pass any command arguments, you must specify them in the the second function argument args
.
In the authors' defense, shell code can be very platform-dependent and inconsistent in implementation and behavior. To use the more precise terms that I've defined in this post would have put the documentation writers at risk of committing errors, at least with respect to some systems which R aspires to support. Vagueness can be a safehouse against risk of outright error.
Note that this differs from the other R system command function, system():
command
the system command to be invoked, as a character string.
And in the Details section:
command
is parsed as a command plus arguments separated by spaces. So if the path to the command (or a single argument such as a file path) contains spaces, it must be quoted e.g. byshQuote()
. Unix-alikes pass the command line to a shell (normally ‘/bin/sh
’, and POSIX requires that shell), socommand
can be anything the shell regards as executable, including shell scripts, and it can contain multiple commands separated by;
.
So for system()
, the first function argument command
is a full command line.
So they actually use exactly the same function argument name (command
) and description ("the system command to be invoked, as a character string."), even though the argument has two completely different meanings between system()
and system2()
! Understanding this documentation really does require careful parsing by the reader.
So, finally, we can understand how to correctly use system2()
to invoke the desired java command:
word <- 'java';
args <- c('-jar','sample.jar','674');
result <- system2(word,args,stdout='C:/Code/stdout.txt',stderr='C:/Code/stderr.txt');
Just to try to clarify further, it's helpful to experiment with the behavior of these functions by trying some simple test cases. For example (on my Cygwin bash shell):
system('printf %d:%x\\\\n 31 31');
## 31:1f
system2('printf',c('%d:%x\\\\n','31','31'));
## 31:1f
(Note that the quadrupling of backslashes is necessary because they pass through 3 interpolation contexts, namely (1) R string literal interpolation, (2) bash (non-single-quoted) lexical context, and (3) the printf
command's interpolation of its first command argument. We need printf
to interpolate the final \n
ASCII character code.)
Also, it should be noted that, although system2()
clearly encourages only running simple commands by enforcing separation of the command word and command arguments into separate function arguments, it is very possible to subvert that intention and use shell metacharacters to execute some decidedly non-simple shell code through the system2()
interface:
system('echo a b; echo c d');
## a b
## c d
system2('echo',c('a','b; echo c d'));
## a b
## c d
This is, of course, highly inadvisable.