如何在java中打开exe文件

问题描述 投票:0回答:5

我正在尝试使用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();
        }

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

java exe
5个回答
5
投票

试试这个:

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

3
投票

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

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

1
投票

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

也许使用

File.separator
代替 '\'


1
投票

我尝试过这个,效果很好,它取自您的示例。注意双

\\

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

0
投票

尝试{ 进程 p = Runtime.getRuntime().exec("C:\ProgramFiles(x86)\Skype\Phone\Skype"); } catch (IOException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } 因为我认为它不允许空间,我知道我有点晚了,但它可能会对将来的人有所帮助。 Mabye 按照另一位用户的建议,将“.exe”添加到“\skype”的末尾,这样它就显示“skype.exe”,而不仅仅是“skype”

© www.soinside.com 2019 - 2024. All rights reserved.