12

I want to open a new terminal window, which will run a certain command upon opening. It preferably needs to be a real native window, and I don't mind writing different code for linux/osx/windows.

I'm assuming an emulated terminal would work, as long as it supports everything a real terminal would do and isn't just printing lines of output from a command.

Matt
  • 11,157
  • 26
  • 81
  • 110

5 Answers5

16

Will this work?

// windows only
Process p = Runtime.getRuntime().exec("cmd /c start cmd.exe");
p.waitFor();
Bala R
  • 107,317
  • 23
  • 199
  • 210
14

Opening an actual terminal window will definitely require different code for each OS. For Mac, you want something like:

Runtime.getRuntime().exec("/usr/bin/open -a Terminal /path/to/the/executable");
Bala R
  • 107,317
  • 23
  • 199
  • 210
Mike Baranczak
  • 8,291
  • 8
  • 47
  • 71
  • ```-a``` states the application you want to use to open a file – in this case it is terminal. For a word document it would be ```-a Word /path/to/word/document.doc``` You surround apps with spaces with quotations. ```-a "Sublime Text" path/to/code/file.js``` – 55 Cancri May 22 '18 at 14:31
6

I've used this on Ubuntu(X11 Desktop) 10.04 ~ 14.04, and other Debian distro's. Works fine; although, you may consider using Java's ProcessBuilder.

     // GNU/Linux -- example

Runtime.getRuntime().exec("/usr/bin/x-terminal-emulator --disable-factory -e cat README.txt");

 //  --disable-factory    Do not register with the activation nameserver, do not re-use an active terminal
//    -e                  Execute the argument to this option inside the terminal.
  • --disable-factory doesn't work in my Peppermint Linux 7 – Stefan Reich Jun 30 '17 at 20:47
  • 1
    x-terminal-emulator is best, but its linked to another program (mate-terminal in my current box). It could be gnome-terminal, xterm , etc... Note: --disable-factory is nice to have, but not always available. – JimmyLandStudios Jan 24 '20 at 17:07
  • @JimmyLandStudios As you mentioned it is linked to another program, does it work on Linux distros other than Ubuntu? Also as tested on Ubuntu 20.04 LTS `x-terminal-emulator` works directly without the path prefix "/user/bin", is it also true for other distros? – Shreck Ye Nov 08 '20 at 20:49
4

You need information about the OS you're running. For that you could use code like this:

public static void main(String[] args)
    {
        String nameOS = "os.name";        
        String versionOS = "os.version";        
        String architectureOS = "os.arch";
        System.out.println("\n    The information about OS");
        System.out.println("\nName of the OS: " + 
        System.getProperty(nameOS));
        System.out.println("Version of the OS: " + 
        System.getProperty(versionOS));
        System.out.println("Architecture of THe OS: " + 
        System.getProperty(architectureOS));
    }

Then for each OS you would have to use different invocations as described by Bala R and Mike Baranczak

0

for Java to use Windows taskkill, try this:

    try {
      // start notepad before running this app
      Process p1 = Runtime.getRuntime().exec("cmd /c start cmd.exe"); // launch terminal first
      p1.waitFor();
      Process p2 = Runtime.getRuntime().exec( "taskkill /F /IM notepad.exe" );  // now send taskkill command
      p2.waitFor();
      Process p3 = Runtime.getRuntime().exec( "taskkill /F /IM cmd.exe" );  // finally, close terminal
      p3.waitFor();
    } catch (IOException ex) {
        System.out.println(ex);
    } catch (InterruptedException ex) {
        Logger.getLogger(RT2_JFrame.class.getName()).log(Level.SEVERE, null, ex);
    } // close try-catch-catch

you need to have the cmd terminal running before taskkill works.

  • Welcome to StackOverflow! Could you add, in addition to the code you provided, a brief explanation of what it does? Remember to always provide some context. – CFV Feb 08 '21 at 17:36