2015-09-16 15 views
5

मेरे पास एक स्ट्रिंग वैरिएबल में एक प्रतिलेख है और मैं इसे जावा प्रोग्राम से निष्पादित करना चाहता हूं और कुछ चर को पास करना चाहता हूं। अगर मैं उस आर स्क्रिप्ट स्टैंडअलोन निष्पादित करता हूं, तो यह ठीक काम करता है। मैं इसे अजगर कार्यक्रम का उपयोग करके सब कुछ से बचने के बाद एक लाइन के लिए परिवर्तित कर दिया है कि आर स्क्रिप्ट के रूप में नीचे दिखाया गया है:जावा प्रोग्राम से आर स्क्रिप्ट निष्पादित करने में सक्षम नहीं है?

import json 

jsonstr = json.dumps({"script": """\ 
#!/usr/bin/Rscript 

# read the data file 
library('jsonlite') 
library('rpart') 

args <- as.list(Sys.getenv(c(
         "path", 
         "client_users"))) 

if (args[["path"]]==""){ 
    args[["path"]] <- "." 
} 

# other stuff here 
# other stuff here 

"""}) 

print jsonstr 

मैं मुद्रित स्ट्रिंग बाहर का उपयोग करें और स्ट्रिंग वैरिएबल में संग्रहीत और फिर मैं नीचे दिए गए कोड के साथ क्रियान्वित कर रहा हूँ और यह बिल्कुल काम नहीं करता है। मैं path और client_users चर से ऊपर आर स्क्रिप्ट के लिए गुजर रहा हूँ।

public static void main(String[] args) throws IOException, InterruptedException { 

    // this is your script in a string 
    // String script = "#!/bin/bash\n\necho \"Hello World\"\n\n readonly PARAM1=$param1\n echo $PARAM1\n\nreadonly PARAM2=$param2\n echo $PARAM2\n\n"; 
    String script = "above R Script here"; 

    List<String> commandList = new ArrayList<>(); 
    commandList.add("/bin/bash"); 

    ProcessBuilder builder = new ProcessBuilder(commandList); 
    builder.environment().put("path", "/home/david"); 
    builder.environment().put("client_users", "1000"); 
    builder.redirectErrorStream(true); 
    Process shell = builder.start(); 

    // Send your script to the input of the shell, something 
    // like doing cat script.sh | bash in the terminal 
    try(OutputStream commands = shell.getOutputStream()) { 
     commands.write(script.getBytes()); 
    } 

    // read the outcome 
    try(BufferedReader reader = new BufferedReader(new InputStreamReader(shell.getInputStream()))) { 
     String line; 
     while((line = reader.readLine()) != null) { 
      System.out.println(line); 
     } 
    } 

    // check the exit code 
    int exitCode = shell.waitFor(); 
    System.out.println("EXIT CODE: " + exitCode); 
} 

ऊपर कोड बैश खोल स्क्रिप्ट के साथ ठीक काम करता है। क्या आरक्रिप्ट के लिए मुझे कुछ करने की ज़रूरत है? मैं बैश स्क्रिप्ट और आर स्क्रिप्ट के लिए भी एक ही कोड का उपयोग करूँगा।

और इस त्रुटि मैं हो रही है:

/bin/bash: line 7: -: No such file or directory /bin/bash: line 10: syntax error near unexpected token `'jsonlite'' /bin/bash: line 10: `library('jsonlite')' 

और अगर मैं commandList.add("/bin/bash"); हटाने और commandList.add("/bin/Rscript"); जोड़ने तो मैं त्रुटि नीचे देखें:

Cannot run program "/bin/Rscript": error=2, No such file or directory 

अपडेट: -

मेरी उपरोक्त लिपि का उपयोग करने के बजाय, मैंने यह देखने के लिए कि क्या मैं इसे निष्पादित कर सकता हूं, में सरल प्रिंट नरक स्क्रिप्ट का उपयोग करने का निर्णय लिया यूघ जावा या नहीं।

// this will print hello 
String script = "#!/usr/bin/env Rscript\nsayHello <- function(){\n print('hello')\n}\n\nsayHello()\n"; 

जब मैं commandList.add("/bin/bash"); के साथ इस स्क्रिप्ट को निष्पादित, मैं इस त्रुटि मिलती है:

/bin/bash: line 2: syntax error near unexpected token `(' 
/bin/bash: line 2: `sayHello <- function(){' 

लेकिन अगर मैं इस commandList.add("/bin/sh"); साथ निष्पादित, मैं इस त्रुटि मिलती है:

/bin/sh: 2: Syntax error: "(" unexpected 

उत्तर

3

आप होगा सीधे /usr/bin/Rscript चलाने के लिए। इसके अलावा, इस कार्यक्रम के मानक इनपुट (आप Rscript के लिए एक तर्क के रूप पथ को स्क्रिप्ट पर निर्दिष्ट करने के लिए है) से स्क्रिप्ट पढ़ नहीं है, इसलिए आप होगा करने के लिए:

  • एक अस्थायी फ़ाइल बनाएँ
  • लिखें अपनी स्क्रिप्ट
  • Rscript साथ अपनी स्क्रिप्ट निष्पादित
  • अपने अस्थायी फ़ाइल हटाएँ (एक अच्छा प्रोग्रामिंग अभ्यास के रूप में)

उदाहरण के लिए, यह एक POC है:

public static void main(String[] args) throws IOException, InterruptedException { 

    // Your script 
    String script = "#!/usr/bin/env Rscript\n" + 
      "\n" + 
      "sayHello <- function() {\n" + 
      " print('hello')\n" + 
      "}\n" + 
      "\n" + 
      "sayHello()\n"; 

    // create a temp file and write your script to it 
    File tempScript = File.createTempFile("test_r_scripts_", ""); 
    try(OutputStream output = new FileOutputStream(tempScript)) { 
     output.write(script.getBytes()); 
    } 

    // build the process object and start it 
    List<String> commandList = new ArrayList<>(); 
    commandList.add("/usr/bin/Rscript"); 
    commandList.add(tempScript.getAbsolutePath()); 
    ProcessBuilder builder = new ProcessBuilder(commandList); 
    builder.redirectErrorStream(true); 
    Process shell = builder.start(); 

    // read the output and show it 
    try(BufferedReader reader = new BufferedReader(
      new InputStreamReader(shell.getInputStream()))) { 
     String line; 
     while((line = reader.readLine()) != null) { 
      System.out.println(line); 
     } 
    } 

    // wait for the process to finish 
    int exitCode = shell.waitFor(); 

    // delete your temp file 
    tempScript.delete(); 

    // check the exit code (exit code = 0 usually means "executed ok") 
    System.out.println("EXIT CODE: " + exitCode); 
} 

एक विकल्प के रूप में, और आपकी स्क्रिप्ट पहली पंक्ति के रूप में एक "मामला" है, तुम कर सकते हो इन परिवर्तनों:

  • इसके निष्पादन योग्य विशेषता को "सही" सेट
  • उपयोग कमांड सूची में प्रथम तत्व के रूप में temp फ़ाइल का पथ (i। ई।हटाना commandList.add("/usr/bin/Rscript");)

कोड संशोधित करने का हिस्सा होगा:

... 

// create a temp file and write your script to it 
File tempScript = File.createTempFile("test_r_scripts_", ""); 
tempScript.setExecutable(true); 
try(OutputStream output = new FileOutputStream(tempScript)) { 
    output.write(script.getBytes()); 
} 

// build the process object and start it 
List<String> commandList = new ArrayList<>(); 
commandList.add(tempScript.getAbsolutePath()); 
ProcessBuilder builder = new ProcessBuilder(commandList); 

... 
संबंधित मुद्दे