Compiling a C ++ file from java

My problem is with compiling a C ++ file in java. I tried to execute C #, this is normal. This extraction code to compile C #

ProcessBuilder launcher = new ProcessBuilder("gmcs","HelloWorld.cs");` 

However, my code is for C ++

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

returns error = 2, There is no such file or directory To indicate the path that I used launcher.directory(new File(path)) in both cases

+4
source share
1 answer

You need to specify the arguments separately:

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

Otherwise, the entire argument line is passed as one argument to the g++ executable, and g++ tries to find a file called HelloWorld.cpp\ -o\ HelloWorld (using escaped spaces, like on a Linux terminal).

See the documentation for more details .

+6
source

All Articles