Created
July 8, 2011 03:15
-
-
Save anonymous/1071059 to your computer and use it in GitHub Desktop.
getRuntime check device
This file contains hidden or 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
| /*** 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