Created
January 7, 2019 08:20
-
-
Save shalithasuranga/44340320c47972958689f07667daaeb9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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