Skip to content

Instantly share code, notes, and snippets.

@cloojure
Created February 27, 2019 19:36
Show Gist options
  • Save cloojure/28967c07d6964c83c12ef729da4383a8 to your computer and use it in GitHub Desktop.
Save cloojure/28967c07d6964c83c12ef729da4383a8 to your computer and use it in GitHub Desktop.
Java example:
package expr;
import java.io.InputStream;
import static java.lang.System.in;
import static java.lang.System.out;
class Gopher implements Runnable {
public void run() {
try {
out.println( " Gopher started" );
out.println("Type something:");
int charInt = 0;
while (charInt != -1) {
charInt = in.read();
String charStr = Character.toString( charInt );
out.println( "Gopher int=" + charInt + " str=" + charStr );
}
} catch (Exception ex) {
out.println( " Gopher caught:"+ ex );
} finally {
out.println( " Gopher finished" );
}
}
}
public class Demo {
public static void main(String[] args) {
try {
out.println( "Hello ProcessBuilder!" );
out.println( "Starting Gopher... " );
Thread th = new Thread( new Gopher() );
th.setDaemon( false );
th.start();
out.println( " done! daemon? " + th.isDaemon() );
if (false) {
out.println( "main sleeping..." );
Thread.sleep( 10 * 1000 );
out.println( "main leaving" );
}
ProcessBuilder pb = new ProcessBuilder( "/bin/bash", "-c", "echo yolo" );
Process proc = pb.start();
InputStream istream = proc.getInputStream();
byte[] bytes = istream.readAllBytes();
String result = new String( bytes );
out.println( "proc result='" + result + "'" );
if (true) {
out.println( "main sleeping..." );
Thread.sleep( 10 * 1000 );
out.println( "main leaving" );
}
// assert proc.getInputStream().read() == -1;
} catch (Exception ex) {
}
}
}
contents of ./go.bash
----------------------------------
#!/bin/bash
rm src/expr/*.class
javac src/expr/*.java
ls -ldF src/expr/*.class
java -cp src expr.Demo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment