让应用程序在自己的目录中运行

发布于 2024-07-11 17:49:44 字数 1034 浏览 4 评论 0原文

我正在尝试让一个应用程序(游戏)以 Java 脚本启动。 背后有一个很长的解释,所以我将跳过这一部分。

除非从自己的目录执行,否则游戏不会运行,IE:仅告诉 Java 启动 EXE 就会在游戏中出现错误。 它必须从其目录启动。

我已经在谷歌上搜索了几个小时,似乎找不到任何真正的答案。 经过大量谷歌搜索后,我了解到这应该是正确的:

String workingDir = "F:\\Games\\COD4\\";
String cmd = "iw3mp.exe";           

Runtime.getRuntime().exec(cmd,null,new File(workingDir));

但是,我收到此错误:

Exception in thread "main" java.io.IOException: Cannot run program "iw3mp.exe" (in directory "F:\Games\COD4"): CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at cod4launcher.main(cod4launcher.java:29)
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)
... 4 more

请帮忙! 谢谢。

I'm trying to get an application (a game) to start with a Java script. There is a long explanation behind why, so I'll skip that part.

The game won't run unless it is executed from its own directory, IE: Just telling Java to launch the EXE gives errors within the game. It must be launched from its directory.

I've Googled for hours over this and can't seem to find any real answers. After a lot of Googleing, I've learned this should be right:

String workingDir = "F:\\Games\\COD4\\";
String cmd = "iw3mp.exe";           

Runtime.getRuntime().exec(cmd,null,new File(workingDir));

However, I get this error:

Exception in thread "main" java.io.IOException: Cannot run program "iw3mp.exe" (in directory "F:\Games\COD4"): CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at cod4launcher.main(cod4launcher.java:29)
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)
... 4 more

Please help! Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时常饿 2024-07-18 17:49:44

请尝试以下操作:

String workingDir = "F:\\Games\\COD4\\";
String cmd = workingDir + "iw3mp.exe";
Runtime.getRuntime().exec(cmd,null,new File(workingDir));

iw3mp.exe 可能不在您的系统路径上,因此您必须为 Java 提供可执行文件的绝对路径。

Try this instead:

String workingDir = "F:\\Games\\COD4\\";
String cmd = workingDir + "iw3mp.exe";
Runtime.getRuntime().exec(cmd,null,new File(workingDir));

iw3mp.exe probably isn't on your system path so you have to give Java an absolute path to the executable.

冬天旳寂寞 2024-07-18 17:49:44

使用 java.lang.ProcessBuilder#directory(java.io.File)

Processs p = 
    new ProcessBuilder(cmd).
        directory(new File(workingDir)).
        start();

Use java.lang.ProcessBuilder#directory(java.io.File)

Processs p = 
    new ProcessBuilder(cmd).
        directory(new File(workingDir)).
        start();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文