Search code examples
javaprocessbuilder

How can I fix problems while running .exe file


I have a ThMapInfratab1-2.exefile under C:\Users\Infratab Bangalore\Desktop\Rod directory. I executed in command prompt in the following way.it's working fine.

 C:\Users\Infratab Bangalore\Desktop\Rod>ThMapInfratab1-2.exe TMapInput.txt

I want to do same procedure using Java technology. using StackOverFlow guys,I tried in 2 ways.

Case 1:

Using getRuntime().

   import java.util.*;
   import java.io.*;
   public class ExeProcess
{
public static void main(String args[]) throws IOException
{
    Runtime rt = Runtime.getRuntime();
    File filePath=new File("C:/Users/Infratab Bangalore/Desktop/Rod");
    String[] argument1  = {"TMapInput.txt"};
    Process proc = rt.exec("ThMapInfratab1-2.exe", argument1, filePath);
}
}

Case 2:

Using ProcessBuilder

 import java.io.File;
 import java.io.IOException;
 public class ProcessBuilderSample {

 public static void main(String args[]) throws IOException 
 {
  String executable = "ThMapInfratab1-2.exe";
  String argument1  = "TherInput.txt";
  File workingDirectory = new File("C:/Users/Infratab Bangalore/Desktop/Rod");

  ProcessBuilder pb = new ProcessBuilder(executable, argument1);
  pb.directory(workingDirectory);
  pb.start();     
 }
 }

in both cases, I am getting the following error.

Error:

   Exception in thread "main" java.io.IOException: Cannot run program "ThMapInfratab1-2.exe" (in directory "C:\Users\Infratab Bangalore\Desktop\Rod"): CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(Unknown Source)
at ProcessBuilderSample.main(ProcessBuilderSample.java:16)
  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>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
... 2 more

I did't figure out, what's the problem. can anyone suggest me.

I am using jre 7.

Thanks


Solution

  • try to use this :

    import java.io.File;
     import java.io.IOException;
     public class ProcessBuilderSample {
    
     public static void main(String args[]) throws IOException 
     {
      String executable = "ThMapInfratab1-2.exe";
      String argument1  = "TherInput.txt";
      File workingDirectory = new File("C:/Users/Infratab Bangalore/Desktop/Rod");
    
      ProcessBuilder pb = new ProcessBuilder("cmd", "/c","start" ,executable, argument1);
      pb.directory(workingDirectory);
      pb.start();     
     }
     }