【问题标题】:Having trouble starting a program with the Runtime.getRuntime().exec(command)使用 Runtime.getRuntime().exec(command) 启动程序时遇到问题
【发布时间】:2012-05-23 07:09:21
【问题描述】:

我目前正在编写一个可以在我的 PC 上打开 .exe 程序的 Java 程序,例如 MS Word。 不过我遇到了问题,因为 Runtime.getRuntime().exec() 只会成功打开某些程序。我对所有程序都使用了完全相同的代码,但无论如何,有些程序无法打开。

这是我下载的程序 Picasa 3 的代码:

class picasaHandler implements ActionListener
{
    public void actionPerformed(ActionEvent r)
    {
        try 
        {
            Runtime.getRuntime().exec("cmd /c start Picasa3.exe");

        }
        catch (IOException t)
        {
            JOptionPane.showMessageDialog(null,
                    "Sorry, could not find Picasa 3");
        }
    }
}

所以我的问题是,为什么 Runtime.getRuntime().exec() 不能运行我使用它的所有程序,以及如何运行像 Picasa 3 这样的程序,我目前无法使用这种方法运行.

【问题讨论】:

    标签: java runtime exe actionlistener runtime.exec


    【解决方案1】:

    我猜 Picasa3.exe 不在您的 %PATH% 上,所以它不知道如何加载它。您是否尝试过指定 Picasa3.exe 的完整路径?

    Runtime.getRuntime().exec("cmd /c \"c:\\program files (x86)\\Google\\Picasa3\\Picasa3.exe\"")
    

    【讨论】:

    • 嗯,你必须把正确的路径,我只是在猜测你的机器的路径是什么!
    • 代码Runtime.getRuntime().exec("cmd /c start \"h:\\program files\\Picasa\\Picasa3.exe");
    • 在末尾添加\"Runtime.getRuntime().exec("cmd /c start \"h:\\program files\\Picasa\\Picasa3.exe\""); -- 如果有帮助,请告诉我!
    • 只是告诉你们,我机器的路径是c:\program files (x86)\Google\Picasa3\Picasa3.exe
    • 所以我将该路径输入到我的机器中,它只是打开命令提示符。但是,当我将路径放入命令提示符时,Picasa 会打开
    【解决方案2】:
    File file=new File("picassa3");
    String filename=file.getAbsolutePath(file);
    try 
        {
            Runtime.getRuntime().exec(filename);
    
        }
        catch (IOException t)
        {
            JOptionPane.showMessageDialog(null,
                    "Sorry, could not find the file");
        }
    

    【讨论】:

      【解决方案3】:

      运行时的 exec 只能启动 Windows 路径上的应用程序。有些程序会自动在路径上,而其他程序(例如 Picasa)则不会。

      唯一的解决方法是确定正确的路径,然后启动该应用程序。

      【讨论】:

      • 我将如何找到我的程序所在的路径,并且我仍然能够使用 Runtime 的 exec?
      • 是的,如果您知道路径,您仍然可以使用 exec 调用。路径是系统上可执行程序(.exe 文件)的位置。我看到已经有一个很好的答案,所以我不会重复它。
      【解决方案4】:

      这可能对你有用。 如果您想使用 Runtime.exec() 运行某个程序,只需将其安装路径添加到系统变量中的路径变量即可。要找到它的安装路径,只需右键单击它的快捷方式并选择“查找目标”。然后将整个地址连接到路径变量的末尾。

      【讨论】:

        猜你喜欢
        • 2011-04-15
        • 1970-01-01
        • 2015-04-27
        • 1970-01-01
        • 1970-01-01
        • 2013-11-10
        • 2012-11-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多