user3394645
user3394645

Reputation: 87

How to compile and run Typescript from Java?

I'm writing a Typescript programmatically in a Java based app. This produces a script.ts, which in normal Node js environment (whith Typescript installed) compiled and runs by:

tsc script.ts

My question is now: I can preinstall node js en Typescript on same server as the Java app, How can I then run from Java this script (mimic tsc script.ts)? And read the console log of it?

EDIT 1: I am trying to run the script.ts like following but encounter an error

p = Runtime.getRuntime().exec("tsc C:\\Users\\SK\\Documents\\Men\\HAI-main\\Executed\\script.ts");

Error:

Caused by: java.io.IOException: Cannot run program "tsc": CreateProcess error=2, The system cannot find the file specified
        at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
        at java.lang.Runtime.exec(Runtime.java:620)

Upvotes: 2

Views: 5170

Answers (1)

SimpleJ
SimpleJ

Reputation: 14798

In order to compile the TS on the same server as your Java app, you just need to make sure Node, NPM, and Typescript are all installed. You will also need to make sure tsc is in your PATH or use the full path to the tsc executable in the code below.

Then you can just spawn a tsc process. Something like this:

Runtime.getRuntime().exec("tsc /path/to/script.ts");

Upvotes: 1

Related Questions