// This is the program to Compile a c++ file from a java program, & get compiler generated files in the file's parent directory.
//problem is that though it neither generate err nor throws exception, yet doesn't compile, PLEASE HELP. or suggest any other solution
|
Code:
|
import java.io.*;
class CompileCppFile{
File file;
CompileCppFile(File file){
this.file = file;
}
public void compile(){
try{
String[] cmd = new String[4];
cmd[0] = "cmd.exe";
cmd[1] = "/C";
cmd[2] = "PATH = C:\\TC\\BIN"; //where C++ is installed.
cmd[3] = "TCC " + file.getName(); //not working. but why? http://www.java-forums.org/images/smilies/confused.gif
:confused:
Process p= Runtime.getRuntime().exec( cmd, null, new File( file.getParent()) );
/*
String[] envp = new String[1];
envp[0] = "PATH = C:\\TC\\BIN";
Process p= Runtime.getRuntime().exec( cmd, envp, new File( file.getParent()) );
//has no effect
*/
BufferedReader err = new BufferedReader( new InputStreamReader(p.getErrorStream()) );
String line;
while( (line = err.readLine()) != null)
System.out.println(line);
BufferedReader output = new BufferedReader( new InputStreamReader(p.getInputStream()) );
while( (line = output.readLine()) != null)
System.out.println(line);
}
catch(Exception ioE){
System.out.println(ioE.getMessage());
ioE.printStackTrace();
}
}
}
class CompileCpp{
public static void main(String args[]){
CompileCppFile c = new CompileCppFile(new File("F:/Amit/WorkHouse/Test.cpp"));
c.compile();
}
} |