1

Possible Duplicate:
Capturing stdout when calling Runtime.exec

Ok so,

I am developing an Eclipse plugin that generates some PHP code based a visual interface (i.e. the user drags and drops some stuff on a View and then my plugin will update some php files with the code associated)

I am creating the php files with FileUtils (Apache Commons) and I am generating the PHP code using an ASTParser.

My problem is that I need my freshly generated code to be beautified and nicely indented. I have googled around and found this http://www.waterproof.fr/products/phpCodeBeautifier/ . It comes as a .exe file. I have added it to resources but I'm having some trouble calling it and getting the output from it.

How can I do this? Also I have to say that I need it to work on both a Mac and a Windows machine. Can this be done? Is there an easier way to beautify the code?

Thanks!

Community
  • 1
  • 1
RutZap
  • 381
  • 1
  • 6

2 Answers2

0

If you can run it from command line and pass file path to beautify the code you can use following code to run it and capture the result.

Process p = Runtime.getRuntime().exec("executable.exec");

BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));

while ((String line = input.readLine()) != null) {
  System.out.println(line);
}

see also : How to capture the data returned from an EXE excuted from a batch?

How can i get the console output generated by a exe file?

Capturing stdout when calling Runtime.exec

Community
  • 1
  • 1
Mehdi
  • 4,396
  • 4
  • 29
  • 30
0

I would do it as follows:

  1. Resolve the location of the exe in an OS independent manner.
  2. Create a Process using `Runtime.getRuntime().exec('exe_location');
  3. Take the process.getOutputStream() and print the unformatted code into it which means the exe would read the text to be formatted on STDIN.
  4. Take the process.getInputStream() and read out the formatted code from it which means the exe would print the formatted output on to STDOUT.
  5. Close the streams and end the process.
Vikdor
  • 23,934
  • 10
  • 61
  • 84