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