Same thing works on terminal but not in java Runtime while using Ubuntu 14.04 LTS

I have a Java project which I developed in windows. Now, I want to make it platform independent.

A part of the project is: it takes a c program and executes. takes input from file and gives output in file, then compares the output file with another file and gives a result.

At first I have a class which runs the .cpp file and produce executable file. As far I know, the command for this work is same in both windows and ubuntu and that is:

g++ -std=c++11 -o <name of executable file> <path of .cpp file>

Using the above command, it works good using terminal. But while implementing the same thing in code, it is giving compilation error. That means the following errors:

g++: fatal error: no input files

See the image to be more clear:

enter image description here

My code:

package judgecpp;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Path;
import java.nio.file.Paths;
class CodeCompile implements Runnable, RunInCmd { Thread compilerThread; private final File fileCPP; public File file; public boolean errorHas; public CodeCompile(File fileCPP) { this.fileCPP = fileCPP; file = new File(String.format("compiledFile%d", CodeProcessing.getNewFileNumber())); compilerThread = new Thread(this, "compilerThread"); } @Override public Process cmdRun(String command) throws IOException { Runtime rt; Process pr; rt = Runtime.getRuntime(); pr = rt.exec(command); return pr; } @Override public void run() { try { String command = String.format("g++ -std=c++11 -o \"" + file.toString() + "\" \"" + fileCPP.toString() + "\"");
// if(VerdictGiver.os.equals("uni")) command = String.format("g++ -std=c++11 \"" + fileCPP.toString() + "\" -o \"" + file.toString() + "\"");
// command = "pwd"; System.out.println("Code Compile: "+command); System.out.println("OS: "+VerdictGiver.os); Process pr = cmdRun(command); Path currentRelativePath = Paths.get(""); String s = currentRelativePath.toAbsolutePath().toString(); System.out.println("Current relative path is: " + s); BufferedReader input = new BufferedReader(new InputStreamReader(pr.getErrorStream())); errorHas = false; System.out.println("Compile Error Stream starts -----------------------------------------"); while (input.readLine() != null) { System.out.println(input.readLine()); errorHas = true; } System.out.println("Compile Error Stream ends -------------------------------------------"); } catch (IOException ex) {
// Logger.getLogger(CodeCompile.class.getName()).log(Level.SEVERE, null, ex); } }
}

I am stacked here for a long time.

Update:

I got the solution, but don't know why it works. Instead of line:

String command = String.format("g++ -std=c++11 -o \"" + file.toString() + "\" \"" + fileCPP.toString() + "\"");

I used the following line:

String command = String.format("cmd /c \"g++ -std=c++11 -o \"" + file.toString() + "\" \"" + fileCPP.toString() + "\"\"");

That means generated string would be like:

cmd /c "g++ -std=c++11 -o "compiledFile0" "Submissions/20_masd_0_2015_09_14_01_09_58.cpp""

But in case of running the program, none of the following strings is working:

enter image description here

First (this one works fine in terminal):

"./compiledFile0"<"/home/mamun/Desktop/ContestSettings/icpc2015-data/artichoke/12.in">"output1.txt"

Second:

cmd /c ""./compiledFile0"<"/home/mamun/Desktop/ContestSettings/icpc2015-data/artichoke/12.in">"output1.txt""

Third:

cmd /c ""compiledFile0"<"/home/mamun/Desktop/ContestSettings/icpc2015-data/artichoke/12.in">"output1.txt""

Fourth:

"compiledFile0"<"/home/mamun/Desktop/ContestSettings/icpc2015-data/artichoke/12.in">"output1.txt"

But this time no luck, need help.

2 Reset to default

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like