code_junkie

External program from our Java program

Nov 13th, 2011
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. Process p = Runtime.getRuntime().exec("C:\j2sdk1.4.0bin\helloworld.java");
  2. BufferedReader input =
  3. new BufferedReader(new InputStreamReader(p.getInputStream()));
  4. while ((line = input.readLine()) != null)
  5. System.out.println(line);
  6.  
  7. input.close();
  8.  
  9. Runtime r = Runtime.getRuntime();
  10. mStartProcess = r.exec(applicationName, null, fileToExecute);
  11.  
  12. StreamLogger outputGobbler = new StreamLogger(mStartProcess.getInputStream());
  13. outputGobbler.start();
  14.  
  15. int returnCode = mStartProcess.waitFor();
  16.  
  17.  
  18. class StreamLogger extends Thread{
  19.  
  20. private InputStream mInputStream;
  21.  
  22. public StreamLogger(InputStream is) {
  23. this.mInputStream = is;
  24. }
  25.  
  26. public void run() {
  27. try {
  28. InputStreamReader isr = new InputStreamReader(mInputStream);
  29. BufferedReader br = new BufferedReader(isr);
  30. String line = null;
  31. while ((line = br.readLine()) != null) {
  32. System.out.println(line);
  33. }
  34. } catch (IOException ioe) {
  35. ioe.printStackTrace();
  36. }
  37. }
  38.  
  39. }
  40.  
  41. public Process exec(String command, String envp[], File dir)
  42.  
  43.  
  44.  
  45. @param command a specified system command.
  46. @param envp array of strings, each element of which
  47. has environment variable settings in format
  48. <i>name</i>=<i>value</i>.
  49. @param dir the working directory of the subprocess, or
  50. <tt>null</tt> if the subprocess should inherit
  51. the working directory of the current process.
Add Comment
Please, Sign In to add comment