Skip to content

Instantly share code, notes, and snippets.

Created July 8, 2011 03:15
Show Gist options
  • Select an option

  • Save anonymous/1071059 to your computer and use it in GitHub Desktop.

Select an option

Save anonymous/1071059 to your computer and use it in GitHub Desktop.
getRuntime check device
/*** Main Thread ***/
import java.io.IOException;
public class MyTest {
public static void main(String[] args) throws InterruptedException{
System.out.println("Test start");
Thread thread = new Thread(new Runnable(){
public void run(){
try {
checkDevice();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} //check if video0 device exists
}
});
thread.start();
thread.join();
}
private static void checkDevice() throws IOException{
String[] args = new String[2];
args[0]="ls";
args[1]="/dev/video0";
Runtime rt = Runtime.getRuntime();
ProcessExecuter.executeWait(rt, args);
}
}
/*** ProcessExecuter ***/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class ProcessExecuter {
private static class DummyReader extends Thread{
private InputStream in;
public DummyReader(InputStream in){
this.in = in;
}
public void run(){
InputStreamReader isr = new InputStreamReader(in);
BufferedReader br = new BufferedReader(isr);
try {
String line = null;
do{
line=br.readLine();
System.out.println(line);
} while( (line=br.readLine())!=null );
} catch (IOException e) {}
finally {
try {
in.close();
} catch (IOException e) {}
}
}
}
public static void executeWait(final Runtime rt, final String[] args) throws IOException {
System.out.println("#### before rt.exec ####");
Process process = rt.exec(args);
System.out.println("#### after rt.exec ####");
new DummyReader(process.getInputStream()).start();
new DummyReader(process.getErrorStream()).start();
try {
int val = process.waitFor();
System.out.println("exitValue : "+val);
} catch (InterruptedException e) {}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment