在所有操作系统的单独控制台中运行流程

时间:2014-03-21 16:02:01

标签: java windows command cross-platform exec

我需要在它自己的命令窗口中运行一个程序,但我不知道如何在主要操作系统中执行此操作,我知道在Windows中我可以使用启动命令但是我没有&# 39;不知道start的命令等同于其他操作系统。以下代码是我到目前为止的代码:

    switch(OS.THISOS){//OS has information about the current OS such as what OS it is and the locations of things like appdata, user home, and user working directory

    case OS.WINDOWS :
        cmd.add("/c");
        cmd.add("start");
        cmd.add("java");
        cmd.addAll(Arrays.asList(Init.getCommand()));//this gets the jvm arguments that this program needs
        new Exec().exec("cmd", cmd.toArray(new String[cmd.size()]));//because start can not be directly executed through exec, I run cmd.exe and pass start in as an argument along with the command to run my program
        break;//Exec is a class that handles IO streams for the sub process

    case OS.MAC :
        //I believe open is the command for MAC but I don't know if it can be directly run with exec or if I need to do something similar to what I did for Windows, also if the later is the case how would I do that?
        break;

    case OS.LINUX :
        //I have seen several answers to this one but I don't know which command works best
        break;

    case OS.SOLARIS :
        //no idea what to do here
        break;

    default:
        System.err.println("OS not supported");//this should never happen since the program will crash in Init if the OS is unknown
        return;

    }

修改 我做了一些研究并提出了这个问题,但我不知道这是否有效:

switch(OS.THISOS){

    case OS.WINDOWS :
        cmd.add("/c");
        cmd.add("start");
        cmd.add("java");
        cmd.addAll(Arrays.asList(Init.getCommand()));
        new Exec().exec("cmd", cmd.toArray(new String[cmd.size()]));
        break;

    case OS.MAC :
        cmd.add("-a");
        cmd.add("java");
        cmd.add("--args");
        cmd.addAll(Arrays.asList(Init.getCommand()));
        new Exec().exec("open", cmd.toArray(new String[cmd.size()]));//will this work?
        break;

    case OS.UNKNOWN :
        System.err.println("OS not supported");
        break;

    default://at this point it's Linux, Solaris, or some other Unix system, xterm is the closest I can find to what I want
        cmd.add("-e");
        cmd.add("java");
        cmd.addAll(Arrays.asList(Init.getCommand()));
        new Exec().exe("xterm", cmd.toArray(new String[cmd.size()]));
        return;

    }

如果我这样做有什么问题,请告诉我如何解决它。

0 个答案:

没有答案