0

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

Prasad Kharkar
  • 13,410
  • 5
  • 37
  • 56
ur truly friend
  • 553
  • 4
  • 11
  • 23

2 Answers2

5

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();     
 }
 }
Gladiator
  • 264
  • 1
  • 5
  • Thanks for your answer,it's working great. can you explain first 3 arguments of the following statement. `ProcessBuilder pb = new ProcessBuilder("cmd", "/c","start" ,executable, argument1);` – ur truly friend Jul 24 '13 at 11:03
  • see this : [What does cmd /C mean](http://stackoverflow.com/questions/515309/what-does-cmd-c-mean) – Gladiator Jul 24 '13 at 11:17
  • and for "start", i think it's just program title, you can alter it. – Gladiator Jul 24 '13 at 11:18
1

The statement

pb.directory(workingDirectory);

specifies only the working directory. This is not the directory where the executable ThMapInfratab1-2.exe is to be searched for. But it is the directory where the file you specify as argument TMapInput.txt is to be searched for. Since TMapInput.txt is not an absolute path, your programm will then search for that file relativly to the working directory.

To solve you problem you need to specify the full path for the excecutable:

String executable = "C:\\Users\\Infratab Bangalore\\Desktop\\Rod\\ThMapInfratab1-2.exe";
String argument1  = "TherInput.txt";
File workingDirectory = new File("C:\\Users\\Infratab Bangalore\\Desktop\\Rod");

Or if you do not need to the location C:\Users\Infratab Bangalore\Desktop\Rod just pass the absolute path of the file too and remove the statement pb.directory(workingDirectory);:

String executable = "C:\\Users\\Infratab Bangalore\\Desktop\\Rod\\ThMapInfratab1-2.exe";
String argument1  = "C:\\Users\\Infratab Bangalore\\Desktop\\Rod\\TherInput.txt";

Alternatively you could extend your PATH environment variable to include the location C:\Users\Infratab Bangalore\Desktop\Rod. In this case the programm will run just fine as you posted it.

A4L
  • 17,353
  • 6
  • 49
  • 70
  • Thanks for your answer,I don't want to pass any arguments, because my `.exe` file takes automatically. For this I tried following code. it showing error. – ur truly friend Jul 24 '13 at 11:50
  • I have a one more doubt also,I don't want to pass any argument. So I wrote the following code.it's not working because I didn't use `pb.directory(workingDirectory);` My code: `public static void main(String args[]) throws IOException { String executable = "C:/Users/Infratab Bangalore/Desktop/Rod/ThMapInfratab1-2.exe"; //String argument1 = "TherInput.txt"; //File workingDirectory = new File("C:/Users/Infratab Bangalore/Desktop/Rod"); ProcessBuilder pb = new ProcessBuilder(executable); // pb.directory(workingDirectory); pb.start(); }`compulsory we need to mention`directory()` – ur truly friend Jul 24 '13 at 11:59
  • @urtrulyfriend Since you have specified the absolute path for `ThMapInfratab1-2.exe` your java program should be finding the exe but if that exe is coded to look for the file using only its name `TherInput.txt` then it will not find it. In that case you have to provide the working directory which has to point to the location where the file is. That is exactly what setting the `working directory` in ProcessBuilder is for! – A4L Jul 24 '13 at 13:07
  • Thanks for your answer.I Modified my previous question.Actually I am working on `Wolfram Mathematica`.Because we faced one problem in`Mathematica`.so we decided to work on `Java`.But I never work on `Java`.Regarding my problem I posted the following link `http://stackoverflow.com/questions/17804790/how-can-i-monitor-external-files-in-java`. Because of this problem I am working from yesterday. Finally using `StackOverFlow` guys I wrote 4 functions.Still I need to figure out. That one I mention in above link.can you check it once. – ur truly friend Jul 24 '13 at 13:24