3

I'm trying to compile typescript files in java.

Here is a ".ts" file which has errors:

alert("hello, typescript");
errrrrrrrrrrrrrrrrrrrrrrrrrror

When I compile in windows shell(cmd):

tsc hello.ts

It will report error with message:

E:/WORKSPACE/test/typescripts/hello.ts(2,0): The name 'errrrrrrrrrrrrrrrrrrrrrrrrror' 
does not exist in the current scope

But when I do it in java:

String cmd = "cmd /C tsc hello.ts";
Process p = Runtime.getRuntime().exec(cmd);
String out = IOUtils.toString(p.getInputStream());
String error = IOUtils.toString(p.getErrorStream());
System.out.println("### out: " + out);
System.out.println("### err: " + error);

It prints:

### out:
### err: E:/WORKSPACE/test/typescripts/hello.ts(2,0):

You can see the detail errors is not captured. Where is wrong with my code?


update

I just made sure that the tsc.exe provided by MS has no such problem, and the one I run in this question is the tsc.cmd installed from npm npm install typescript

Freewind
  • 193,756
  • 157
  • 432
  • 708
  • Give a try to apache [common exec](http://commons.apache.org/exec/) and i think that should help you out. – CuriousMind Jan 26 '13 at 06:48
  • I'm afraid it's a problem of the compiler `tsc` of typescript. Since I tried some tools(grunt.js) other than Java, they can only get the first line of error message too. – Freewind Jan 30 '13 at 10:06
  • I just made sure that the `tsc.exe` provided by MS has no such problem, and the one I run in this question is the `tsc.cmd` installed from npm `npm install typescript` – Freewind Jan 30 '13 at 10:40
  • Also see this question: http://stackoverflow.com/questions/13401598/how-can-i-parse-error-messages-from-typescript-in-sublime-text-2 – Freewind Jan 30 '13 at 15:36
  • If it helps, here's a project that does typescript compilation from java: https://github.com/martypitt/typescript4j – Marty Pitt Apr 26 '13 at 06:45

2 Answers2

2

Have you tried using a raw Process/ProcessBuilder combination?

ProcessBuilder pb = new ProcessBuilder("cmd /C tsc hello.ts");

//merge error output with the standard output
pb.redirectErrorStream(true);

Process p = pb.start();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream(), Charset.forName("UTF-8")))) {
    String line;
    while ((line = reader.readLine()) != null) {
        System.out.println(line);
    }
}
assylias
  • 321,522
  • 82
  • 660
  • 783
  • +1 I was really wondering, why there wasn't a solution using BufferedReader – TechSpellBound Jan 26 '13 at 07:37
  • When I try this solution, it introduce another error, which I can't handle: http://stackoverflow.com/questions/14535155/how-to-let-java-output-english-errors-instead-of-other-languages – Freewind Jan 28 '13 at 14:19
  • You need to replace "UTF-8" by the encoding of CMD. – assylias Jan 28 '13 at 14:32
  • See for example: http://stackoverflow.com/questions/13348811/get-list-of-processes-on-windows-in-a-charset-safe-way – assylias Jan 28 '13 at 14:34
0

I just spent a couple hours chasing the same problem.

In the end I worked around it by adding "2> errorfile.txt" to my command line. This redirects the stderr to a file and then I read and print that file.

ForceMagic
  • 6,230
  • 12
  • 66
  • 88