im trying to compile a c program using java i have to invoke tinyc compiler from command line ......i need to run these commands in console using java... first set the directory to the location of c file then invoke "tcc filename" how can it be done???

thanks in advance

sorry i couldnt understand can u please explain.....

ProcessBuilder pb = new ProcessBuilder("myCommand", "myArg1", "myArg2");
Map<String, String> env = pb.environment();
env.put("VAR1", "myValue");
env.remove("OTHERVAR");
env.put("VAR2", env.get("VAR1") + "suffix");
pb.directory("myDir");
Process p = pb.start()

myArg1,myArg2 what r they for please explain this and
i tried

ProcessBuilder pb = new ProcessBuilder("tcc test.c");
pb.directory(new File("c:\\"));
Process p = pb.start();

but not working .....
the aim is to run c program(test.c) at location c:\ using java....
tcc is the tiny c compiler....
what wrong did i made????

anyone please tell me whats wrong in the above program...

The same page where you got this code explains the questions you are asking:

1. If you want to start a process with the default working and without modification of the environment use this :

Process p = new ProcessBuilder("myCommand", "myArg").start();

where,
myCommand = the command that starts the process you want to.
myArg = the arguments that the process thus started might require.
So for example if you want start a ping command you do something like this :

Process p = new ProcessBuilder("ping", "192.168.0.1").start();

2. If you want to start a process where you want to modify the working directory and/or the environment you do this:

ProcessBuilder pb = new ProcessBuilder("myCommand", "myArg1", "myArg2");
 Map<String, String> env = pb.environment();
 env.put("VAR1", "myValue");
 env.remove("OTHERVAR");
 env.put("VAR2", env.get("VAR1") + "suffix");
 pb.directory("myDir");
 Process p = pb.start();

Again here the myArg1 and myArg2 are the arguments that you want to pass to the command.

So taking into consideration what you are doing, try doing this :

ProcessBuilder pb = new ProcessBuilder("tcc", "test.c");

If this does not work, let us know the exact error with the stack trace.

stack Trace doesnt show anything .....
but still its not working......
what is the problem....

Well if the stack trace doesn't show anything, then you certainly don't have an error. Maybe the process ran to completion with success. Get an outputstream for the process and check it's output.

i tried that too but the file is empty....

ProcessBuilder pb = new ProcessBuilder("tcc","tt.c");
 pb.directory(new File("c:\\"));
Process p = pb.start();
FileWriter f0 = new FileWriter("e:\\a.txt");
BufferedReader input =new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) 
f0.append(line);

Yes but what output are you expecting from giving the file to turbo C ?
Also try some very simple command such as the one shown - ping. If you still do not get the output, post the entire code you've written, we would like to see that then.

tcc is tiny c compiler on passing c source file to tcc the source code will be compiled and output will be an exe file....
tcc is used as command line compiler in cmd console on giving
tcc <filename> the output will the compiled exe file....

what im trying to do is an ide for c in which the c source code must be compiled and the exe file must be executed...
is processbuilder the only way or is there any other way......??

Yes but what if the .exe cannot be created for some reason say some compiler error or such, and on that you need to throw something back on the standard output stream for it to be caught by your stream.

so what can i do.????
when i compile using cmd console tcc is working fine....

Try reading the error stream, as if an error occurs the message will go to the error stream not the outputstream (of the process inputstream from the program). And check the value of exitValue() (non-zero means an error occurred).

@masijade :
When I said an error during compilation I meant that the program may not be compiling correctly and hence he won't see the expected output (the exe file) this certainly isn't an error in the process. Here the process (tcc) would just provide an output underlying the lines and the errors that have occurred within the c program so I said for this you need to get an output stream for the process

Suppossedly, he's tried reading the outputStream (see the getInputStream above), but, suppossedly, the "sub" program is simply not doing anything. That tells me that some error is coming, unfortunately, the OP won't see that, because the OP is neither reading the error Stream (getErrorStream()), nor evaluating the return code of the program (exitValue()), so it will look as though it is simply not doing anything.

thanks guys whn i put getErrorStream() the file (tt.c) is compiled
if thr is an error it also displays it...
i dont understand when i give getInputStream() its not working but whn i give getErrorStream() its working.........
anyway its working fine....but nw i have another if i run the file using the bellow code...

ProcessBuilder pb = new ProcessBuilder("tt.exe");
                 pb.directory(new File("c:\\"));
                 Process p = pb.start();
                 StringBuffer sb = new StringBuffer(); 
                 BufferedReader buff = new BufferedReader(new InputStreamReader(p.getErrorStream())); 
                   String line = buff.readLine(); 
while( line != null ) 
{ 
sb.append(line + "\n"); 
line = buff.readLine(); 
}	
System.out.println( sb ); 
File f = new File("d:\\test.txt"); 
FileOutputStream fos = new FileOutputStream(f); 
fos.write(sb.toString().getBytes()); 
fos.close()

i get these errors......

java.io.IOException: Cannot run program "tt.exe" (in directory "c:\"): CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(ProcessBuilder.java:459)
at javaapplication3.Main.main(Main.java:25)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(ProcessImpl.java:81)


the file is right under the c:\ directory so why its coming?????

This article has been dead for over six months. Start a new discussion instead.