gpt4 book ai didi

java - 如何解决运行 .exe 文件时出现的问题

转载 作者:行者123 更新时间:2023-12-01 08:10:32 25 4
gpt4 key购买 nike

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

 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

谢谢

最佳答案

尝试使用这个:

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

关于java - 如何解决运行 .exe 文件时出现的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17829044/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com