Created
February 22, 2013 12:21
-
-
Save spacelis/5013071 to your computer and use it in GitHub Desktop.
Run any bash commandlines from Java
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 nl.tudelft.dmirlab.pydo; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.io.OutputStream; | |
public class Bashtest { | |
/** | |
* @param args | |
* @throws IOException | |
* @throws InterruptedException | |
*/ | |
public static void main(String[] args) throws IOException, InterruptedException { | |
String cmdline = "python -c \"import sys; print sys.version\""; | |
String[] cmd = new String[]{"bash", "-c", cmdline}; | |
Runtime rt = Runtime.getRuntime(); | |
Process p = rt.exec(cmd); | |
InputStream stdout = p.getInputStream(); | |
OutputStream stdin = p.getOutputStream(); | |
InputStream stderr = p.getErrorStream(); | |
BufferedReader br = new BufferedReader(new InputStreamReader(stdout)); | |
String line; | |
StringBuffer sb = new StringBuffer(); | |
while((line = br.readLine()) != null){ | |
sb.append(line); | |
} | |
System.out.println(sb.toString()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment