1

OS X で Runtime.exec() を使用してアプリケーションを開く方法は?

私のインストラクターは、私が Mac OS X を使用している間に彼が Windows を使用していることを除いて、その方法の例を教えてくれました。これまでにカスタマイズしたコードは次のとおりです。

public class Runtime_execution{
public static void main(String args[]){
    Runtime rt = Runtime.getRuntime();
    Process proc;
    try{
        if(System.getProperty("os.name").startsWith("Mac OS X")){
//                run an OS specific program
            proc = rt.exec("Contacts");
        }else{
            proc = rt.exec("gedit");
        }
        System.out.println("Before calling waitFor() method.");
        proc.waitFor(); //  try removing this line
        System.out.println("After calling waitFor() method.");
    }catch(Exception e){
        System.out.println("Contacts is an unknown command.");
    }
}
}
// find out what the os name is for Mac OS X
class Random{
public static void main(String args[]){
    System.out.println(System.getProperty("os.name"));
}
}

プログラムを実行すると、出力が表示され続けました。

Contacts is an unknown command. 

どうすればこれを修正できますか?

4

1 に答える 1

2

OSX では、Runtime.exec が行っているコマンド ライン経由で実行可能ファイルを実行する必要があります。

これを試して:

rt.exec("/Applications/Contacts.app/Contents/MacOS/Contacts");

またはあなたがすることができます

rt.exec("open /Applications/Contacts.app");
于 2013-06-08T05:29:13.650 に答える