Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open a new prompt/terminal window from Java

Tags:

java

terminal

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.

like image 661
Matt Avatar asked Apr 21 '11 00:04

Matt


People also ask

How do I open a new terminal in Java?

Opening a Terminal Window We can open the terminal window with ⌥F12 on macOS, or Alt+F12 on Windows and Linux. The terminal supports all the same commands that the operating system supports.

How do I run a command prompt as administrator in Java?

getRuntime(). exec("c:/elevate Rundll32.exe Powrprof. dll,SetSuspendState ");


1 Answers

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.
like image 106
JimmyLandStudios Avatar answered Oct 03 '22 00:10

JimmyLandStudios