Sudh
Sudh

Reputation: 1335

Executing any .exe file from a Java application

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.

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?

 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>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");
        }
    }

....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:

  1. It runs normally, No exceptions are thrown only problem is Output of AA.exe is not visible anywhere.
  2. It prints an exit code 7 for the process p...any clue????
  3. notepad.exe or MSWord.exe and even TC.exe are running perfectly normal when invoked through this code.

here's the code for AA.exe:

  #include <stdlib.h>
#include <stdio.h>
#include <values.h>
#include <time.h>

int main(void)
{
   int i,j;

      for(j=0;j<150;j++)
      {
     // randomize();
      for(i=0;i<200;i++)
     printf("%d\n", rand() % MAXINT);
      }
   return 0;
}

Upvotes: 0

Views: 4884

Answers (3)

Sudh
Sudh

Reputation: 1335

I think I have got the answer...The problem is not with the source code of Java file...I learned that Turbo C Compiler which I was using to compile the AA.C generates a 16 bit .exe file ( I don't know what it means yet)...But our .getRuntime.exec() function expects a 32 bit or higher .exe in normal mode That was the reason of unexpected behavior of Program.

This is the error I got when I tried to run it outside Eclipse:

image included

When I tried to execute a .exe developed using Visual Studio 2005....I was able to launch the exe from application. Thanks to all of you but still one question remains, How to run 16 bit exe file.

Upvotes: 1

AlBlue
AlBlue

Reputation: 24040

What does the error(s) say?

It's quite possible that the PATH environment is wrong, and it can't find its dependencies (like other DLLs which it needs to launch). Sometimes processes will use different mechanisms in order to find out what their path or environment needs to be.

If you're just seeing 'error' then perhaps you could print out the value of 'e' which will contain your exception as well. You should also print out the error stream, which you aren't doing, as that may contain vital clues.

Upvotes: 0

Sean Patrick Floyd
Sean Patrick Floyd

Reputation: 298838

Use ProcessBuilder instead.

Sample Code:

 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(new File("myDir"));
 Process p = pb.start();

Upvotes: 2

Related Questions