如何在java中打开一个exe文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19756987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 19:57:12  来源:igfitidea点击:

How to open an exe file in java

javaexe

提问by user2947797

I am trying to use java to open an exe file. I'm not sure which program I want to open so I am using Skype as an example. When I try to do it, it gives me errors.

我正在尝试使用 java 打开一个 exe 文件。我不确定要打开哪个程序,所以我以 Skype 为例。当我尝试这样做时,它给了我错误。

 try {
            Process p = Runtime.getRuntime().exec("C:\Program Files (x86)\Skype\Phone\Skype");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

error: Cannot run program "C:\Program": CreateProcess error=2, The system cannot find the file specified

错误:无法运行程序“C:\Program”:CreateProcess error=2,系统找不到指定的文件

回答by Patrick Favre

You are on windows so you have to include the extension .exe

您在 Windows 上,因此必须包含扩展名 .exe

 try {
            Process p = Runtime.getRuntime().exec("C:/Program Files (x86)/Skype/Phone/Skype.exe");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

Maybe use File.separatorinstead of '\'

也许使用File.separator而不是 '\'

回答by Eduardo Dennis

You have to use a string array, change to

您必须使用字符串数组,更改为

try {
        Process p = Runtime.getRuntime().exec(new String[] {"C:\Program Files (x86)\Notepad++\notepad++.exe"});
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

回答by Kong

Try this:

尝试这个:

String path = "/path/to/my_app.exe";
File file = new File(path);
if (! file.exists()) {
   throw new IllegalArgumentException("The file " + path + " does not exist");
}
Process p = Runtime.getRuntime().exec(file.getAbsolutePath());

回答by Stalin

I tried this and it works fine, it's taken from your example. Pay attention to the double \\

我试过这个,它工作正常,它取自你的例子。注意双\\

public static void main(String[] args) {
    try {
        Process p;
        p = Runtime.getRuntime().exec("C:\Program Files\Java\jdk1.8.0_05\bin\Jconsole.exe");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}