I would like to run in Java with the Runtime’s exec() method a C program. The Java code is the following:
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(command);
process.waitFor();
How can I define in this case the compiled C program’s running time? I mean, if I can set a time interval and if the code hadn’t finished running in that interval, to give back a Time limit exceeded error?
I think you should better think in terms of multi threaded programming here.
Once you’ve started to execute the program outside the JVM you can’t really tell from Java ‘stop executing this program’…
So you should probably run the code you’ve presented here in a separate thread and use
Object.wait(long milliseconds)to wait for the results of this thread.
if you didn’t get the result proceed your program.