Skip to content

Instantly share code, notes, and snippets.

@shalithasuranga
Created January 7, 2019 08:20
Show Gist options
  • Save shalithasuranga/44340320c47972958689f07667daaeb9 to your computer and use it in GitHub Desktop.
Save shalithasuranga/44340320c47972958689f07667daaeb9 to your computer and use it in GitHub Desktop.
package com.mycompany.rhinojsexample.hostobjects;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.mozilla.javascript.ScriptableObject;
public class ConsoleRunner extends ScriptableObject {
public ConsoleRunner() {}
public void jsConstructor() {}
public String jsFunction_exec(String command) throws IOException {
StringBuilder out = new StringBuilder();
Runtime runtime = Runtime.getRuntime();
Process pr = runtime.exec(command);
BufferedReader br = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String outputLine = null;
while((outputLine = br.readLine()) != null) {
out.append(outputLine + '\n');
}
br.close();
return out.toString();
}
@Override
public String getClassName() {
return "ConsoleRunner";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment