Search code examples
javarsystem2

Unable to get R language system2 command result


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.


Solution

  • 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.

    ...