How do i run a UNIX terminal from Java and send commands to it?

后端 未结 1 1729
梦如初夏
梦如初夏 2021-01-07 05:05

Regarding the topic, the code below


    Process proc = null;
    try {
        String[] cmdss= {\"gnome-terminal\"};


        proc = Runtime.getRunti         


        
相关标签:
1条回答
  • 2021-01-07 05:33

    You can give gnome-terminal some options on the command line what it shall execute.

    gnome-terminal -e /my/fortran/program
    

    The -x option gives you roughly the same benefit but you can split the commandline into separate words.

    Both -e and -x run the program with optional arguments while connecting the program`s standard input and output to the terminal. So the user can interact with the terminal properly.

    Example:

    gnome-terminal -x bash -c "ls; echo '<enter>'; read"
    

    This will open the terminal and run the "program" bash. bash will get two arguments: -c and ls; echo ....; read. The -c option makes bash parsing and executing the next argument. This will call ls, then echo ... then read which waits for the return key.

    In Java you must split the arguments appropriately into an array like this:

    String cmd[] = {"gnome-terminal", "-x", "bash", "-c", "ls; echo '<enter>'; read" };
    
    0 讨论(0)
提交回复
热议问题