i am not able to get the system2 result. tried to execute the sample jar file.
execute_system.R:
workingDir <- "C:/Code"
setwd(workingDir)
command <- "java -jar sample.jar 674"
commandResult <- system2(command, stdout="C:/Code/stdout.txt", stderr="C:/Code/stderr.txt")
cat("commandResult: ", commandResult)
I am getting the error message when i execute this execute_system.R file and empty file is generated (stdout.txt, stderr.txt)
commandResult: 127
warning message: running command '"java -jar sample.jar 674"' had status 127
I want to read the system2 command result and needs to be process the result data.
When i execute the same command prompt, i am getting the proper result
C:/Code>java -jar sample.jar 123
convert from String to int...
Input: 123
Value: 123
Conversion process done!!!
Actual my Java Code
public class Conversion{
public static void main(String args[]){
System.out.println("convert from String to int...");
String input = args[0];
System.out.println("Input: " + input );
int value = Integer.valueOf(input)
System.out.println("Value: " + value);
System.out.println("Conversion process done!!!);
}
}
I converted this java program into executable jar file (sample.jar).
Please help me. Thanks in advance.
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 Windowscmd
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:The above doesn't make it perfectly clear, but the first sentence of the Details section helps:
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 argumentargs
.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()
:And in the Details section:
So for
system()
, the first function argumentcommand
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 betweensystem()
andsystem2()
! 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: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):
(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 needprintf
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 thesystem2()
interface:This is, of course, highly inadvisable.
Using your code, it worked for me without error when I did the following:
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 theargs
parameter.Here is a snippet from the Details section: