我在目录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);
}
}
案例二:
使用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
.
谢谢