Skip to content

Instantly share code, notes, and snippets.

@spacelis
Created February 22, 2013 12:21
Show Gist options
  • Save spacelis/5013071 to your computer and use it in GitHub Desktop.
Save spacelis/5013071 to your computer and use it in GitHub Desktop.
Run any bash commandlines from Java
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