Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to run a process in java and get the file that was saved from that program .

For example, I want to run Photoshop or MSpaint, wait for the user to finish editing and, after saving, to get the file (jpg, png, etc) or at least the path of the file.


Java
public static void main(String argv[]) throws IOException {

  // A Runtime object has methods for dealing with the OS
  Runtime r = Runtime.getRuntime();
  Process p;     // Process tracks one external native process
  BufferedReader is;  // reader for output of process
  String line;

  // Our argv[0] contains the program to run; remaining elements
  // of argv contain args for the target program. This is just
  // what is needed for the String[] form of exec.
  p = r.exec(argv);

  System.out.println("In Main after exec");

  // getInputStream gives an Input stream connected to
  // the process p's standard output. Just use it to make
  // a BufferedReader to readLine() what the program writes out.
  is = new BufferedReader(new InputStreamReader(p.getInputStream()));

  while ((line = is.readLine()) != null)
    System.out.println(line);


  try {
    p.waitFor();  // wait for process to complete
  } catch (InterruptedException e) {
    System.err.println(e);  // "Can'tHappen"
    return;
  }
  System.err.println("Process done, exit status was " + p.exitValue());
  return;
}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900