如何在运行.exe文件时修复问题

时间:2013-07-24 08:40:06

标签: java processbuilder

我在ThMapInfratab1-2.exe目录下有一个C:\Users\Infratab Bangalore\Desktop\Rod文件。我按照以下方式在命令提示符下执行。它工作正常。

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

我想使用Java技术执行相同的过程。使用StackOverFlow人,我试过两种方式。

案例1:

使用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);
}
}

案例2:

使用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();     
 }
 }

在这两种情况下,我都收到以下错误。

错误:

   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

我没弄明白,问题是什么。任何人都可以建议我。

我正在使用jre 7

由于

2 个答案:

答案 0 :(得分:5)

尝试使用它:

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

答案 1 :(得分:1)

声明

pb.directory(workingDirectory);

仅指定工作目录。这不是要搜索可执行文件ThMapInfratab1-2.exe的目录。但它是要搜索指定为参数TMapInput.txt的文件的目录。由于TMapInput.txt不是绝对路径,因此您的程序将相对于工作目录搜索该文件。

要解决您的问题,您需要指定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");

或者,如果您不需要位置C:\Users\Infratab Bangalore\Desktop\Rod,也只需传递文件的绝对路径并删除语句pb.directory(workingDirectory);

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

或者,您可以扩展PATH环境变量以包含位置C:\Users\Infratab Bangalore\Desktop\Rod。在这种情况下,程序将在您发布时运行得很好。