Note that there are some explanatory texts on larger screens.

plurals
  1. POExecuting any .exe file from a Java application
    text
    copied!<p>I have a Java application in which user can give any executable file (.exe) and the application will run it on the system. Like cmd.exe, notepad.exe or on unix a.out etc.</p> <p>Now the code I have written after going through numerous examples just doesnt seem to work for the user created files notepad.exe works fine but the files written using TC++ and all don't work. Can anyone point out what can be the cause of the error here?</p> <pre><code> import java.io.*; class NewThread implements Runnable{ Thread t; NewThread(){ t = new Thread(this, "Demo Thread"); System.out.println("child thread:" + t); t.start(); } public void run(){ try { String line; Process p = Runtime.getRuntime().exec("C:\\TC\\BIN\\AA.EXE"); InputStream in = p.getInputStream(); OutputStream out = p.getOutputStream(); InputStream err = p.getErrorStream(); BufferedReader br= new BufferedReader(new InputStreamReader(in)); System.out.println("Chid running"); while((line=br.readLine())!=null){ System.out.println(line); } //p.destroy(); } catch (Exception e) { System.out.println("ERROR"); } System.out.println("Child thread exiting"); } } class ThreadDemo { public static void main (String args[]){ new NewThread(); try { for(int i=05;i&gt;0;i--){ System.out.println("Main Thread:" + i); Thread.sleep(1000); } } catch (InterruptedException e){ System.out.println("Main thread Interrupted"); } System.out.println("Main thread exiting"); } } </code></pre> <p>....OK...doesn't work means When I run it using Eclipse-> Child thread exits[all the system.out messages are printed on console but not the ones to be printed by .exe AA.exe doesn't run at all. Some other points:</p> <blockquote> <ol> <li>It runs normally, No exceptions are thrown only problem is Output of AA.exe is not visible anywhere.</li> <li>It prints an exit code 7 for the process p...any clue????</li> <li>notepad.exe or MSWord.exe and even TC.exe are running perfectly normal when invoked through this code.</li> </ol> </blockquote> <p>here's the code for AA.exe:</p> <pre><code> #include &lt;stdlib.h&gt; #include &lt;stdio.h&gt; #include &lt;values.h&gt; #include &lt;time.h&gt; int main(void) { int i,j; for(j=0;j&lt;150;j++) { // randomize(); for(i=0;i&lt;200;i++) printf("%d\n", rand() % MAXINT); } return 0; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload