Hi. So I want to open a application from Java code and open a new terminal for that application also through Java code. We can open the new terminal manually by entering ctrl+alt+t. But I need to do this via code without entering any keys. Can anyone suggest something?

Dani AI

Generated

A couple of clarifications that connect the replies here: was right to point toward ProcessBuilder, but writing keystrokes into a process stdin is not how you open a new desktop terminal window. described an Eclipse-IDE terminal workflow — useful for interactive development inside Eclipse, but not the programmatic solution if your Java program must launch an external terminal and run an .exe at runtime.

To run an executable and show its output in a new terminal window, spawn the terminal emulator and tell it to run your binary. Examples (run from Java):

ProcessBuilder pb = new ProcessBuilder("xterm", "-hold", "-e", "/full/path/to/your/executable");
pb.start();

If you prefer GNOME Terminal (sh wrapper to avoid complex quoting):

String cmd = "gnome-terminal -- bash -c '/full/path/to/your/executable; exec bash'";
new ProcessBuilder("sh", "-c", cmd).start();

On Windows use cmd + start to open a new console (note the empty title argument):

ProcessBuilder pb = new ProcessBuilder(
  "cmd", "/c", "start", "\"\"", "cmd", "/k", "C:\\\\full\\\\path\\\\to\\\\your.exe"
);
pb.start();

Notes and troubleshooting:

  • If the .exe is packaged inside your Eclipse project or JAR, extract it to a temp file (Files.copy from getResourceAsStream), set executable permissions on Unix, then execute the temp path.
  • Use pb.directory(...) and pb.environment() to set working dir and env vars. Use pb.redirectErrorStream(true) or read process streams to capture errors.
  • If launching on Linux from a service or different user, ensure DISPLAY/Wayland variables and X permissions are available. UAC or elevation prompts on Windows will change behavior.

Common failures are wrong paths, missing terminal emulator (xterm/gnome-terminal), or lack of execute permission — check exceptions and the spawned process output to debug.

Recommended Answers

All 2 Replies

Can you send those keys to the app's system input stream via ProcessBuilder?

So what I understood from your question is what are the alternatives to open a new terminal without the use of ctrl+ alt+ t. So, there are quite several methods to do so, and here’s a method which I prefer:

First of all, opening a blank terminal would be required. To do so, click on the window icon visible in the top left part of your screen. Several options will open up, click on the show view option and then on the terminal. Once the terminal is launched, click on the bottom right alongside the terminal window line. A small blue window icon would be visible; click and go ahead with that. Once you have clicked on the window icon, a launch terminal window will appear. Select the terminal as the local terminal and set the encoding to Default (or any other option depending on your needs and wants). Select Ok, and your local terminal will be visible on the screen.

Hope this method works for you, too; please let me know if your problem was resolved or not.

commented: Utterly failed to read the question -> Irrelevant reply. -3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.