Compiling c++ file from java

c++, java

Solution

You need to provide arguments separately:

ProcessBuilder launcher =
    new ProcessBuilder("g++", "HelloWorld.cpp", "-o", "HelloWorld");

Otherwise the whole argument string is passed as one argument to the `g++` executable, and `g++` tries to find a file named `HelloWorld.cpp\ -o\ HelloWorld` (using escaped spaces as you would on a Linux terminal).

See the documentation for details on the usage.

Problem

My problem regards to compiling c++ file within java. I have tried execution of c#, it is fine. This extract code for compiling c# ``` ProcessBuilder launcher = new ProcessBuilder("gmcs","HelloWorld.cs");` ``` However, my code for c++ ``` ProcessBuilder launcher =new ProcessBuilder("g++", "HelloWorld.cpp -o HelloWorld"); ``` returns error=2, No such file or directory To indicate path I used `launcher.directory(new File(path))` in both of cases

Original source