0

I am trying to execute the C code from Java code which is already compiled and executed, but, I am not getting any output from the executable file. Can anyone help me to complete this task?

Code is as follows.

public class Test {
    public static void main(String args[]) {
        try {
            Process processCompile = Runtime.getRuntime().exec("e:/Sample.exe");
        } catch(Exception ex) {
            ex.printStackTrace();
        }
    }
}
2
  • C executables are no different from other executables, at least from the point of view of process execution. As @JunedAhsan points out, the problem is that you have to read the executable's output yourself (and either show it or do something with it). Commented May 18, 2013 at 6:46
  • Go through the Java World article linked from the exec info. page, implement all the recommendations, then use a ProcessBuilder to create the Process. Commented May 18, 2013 at 7:01

2 Answers 2

5

Try this:

BufferedReader stdInput = new BufferedReader(new 
     InputStreamReader(processCompile .getInputStream()));


// read the output from the command
System.out.println("EXE OUTPUT");
while ((s = stdInput.readLine()) != null) {
    System.out.println(s);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Make sure that your C process is actually returning something to the console. Above code is to capture a process output provided there is some output.
@Sankar you can accept the answer so that others can be benefited with it.
0

This method would work only if you run the java program with admin privileges.

If you have privileges, then can you try running your process under "cmd" shell (Which is forked by your java process). An implementation do so this is done here "LinuxInteractor" ( But is in linux). Just minor change needed to port to Windows version.

Finding hard and soft open file limits from within jvm in linux (ulimit -n and ulimit -Hn)

1 Comment

The C program (Sample.exe)is executing from Java Code, I have checked in the task manager, but the problem is that i am not getting any console (output) from the C Program

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.