How to open a command terminal in Linux?

I want to open a terminal (command line) on a Linux machine using Java code. I know how to open a command prompt in windows. The following code I used on windows

String command= "cmd c/start cmd.exe" Runtime rt = Runtime.getRuntime(); Process pr = rt.exec(command); 

I need the same thing on Linux.

Thank you for your responses. I would also like to run sh script.

Does the following code work.

 String command= "usr/bin/xterm myshell.sh"; Runtime rt = Runtime.getRuntime(); Process pr = rt.exec(command); 
+7
java linux terminal
source share
8 answers

There are several terminal emulators on Linux that allow you to interact with various shells . Each shell is basically a command interpreter that understands Linux commands (GNU and Unix commands are more correct, I suppose ...). The terminal emulator provides an interface (window) for the shell and some other features for using the command line. To open a terminal window, you just need to change your command line as follows: -

 import java.io.*; class TerminalLauncher { public static void main(String args[]) throws IOException { String command= "/usr/bin/xterm"; Runtime rt = Runtime.getRuntime(); Process pr = rt.exec(command); } } 

The main assumption I made is that you want to open xterm , which is available for almost any system (with X installed, of course). You might want to open another terminal emulator, such as rxvt, eterm, aterm, gnome-terminal or the console. The command line can also be modified to use different shells such as zsh . I suggest you catch an exception if the terminal you have selected is missing and processes it, asking the user to install it. The best solution is to accept command line arguments for your preferred user shell or use a configuration file that the user can modify so that your script opens the shell of his choice.

Note
1. As others have already indicated, xterm (or any other terminal of your choice) may not be specified in the specified path (/ usr / bin / ...) and may not even be installed, so you may have to use some ( Ex: pipelined navigation through grep to get the path to xterm before starting), which is not such a great idea. I think the best way is to let the user customize all this.

2.I received a comment on this answer (by ypnos), suggesting that I avoid using absolute paths and rather rely on the command found in the PATH environment variable. I have to say, I agree. In this case, the command line should be -

 String command = "xterm" 

Look at the comment because it also indicates a problem with find.

+8
source share

Linux does not have a single standard β€œterminal” command - the available ones depend on which GUI is present (for example, KDE, Gnome, etc.).

You should be able to rely on xterm , but on modern versions of Linux that are not a terminal of choice:

 String command= "/usr/bin/xterm"; Runtime rt = Runtime.getRuntime(); Process pr = rt.exec(command); 

Of course, "xterm" may not be in this particular path ...

+3
source share

Under Gnome, this is gnome-terminal .

In KDE, this is konsole .

Or you can use the more general terminal program xterm .

You probably want to use the options with most of this, so find the manual pages for what you want.

+3
source share

xterm will most likely be available on most Linux-based operating systems, and if found in a variable path.

So you will need to do something like this:

 try { Runtime r = Runtime.getRuntime(); String myScript = ..... String[] cmdArray = {"xterm", "-e", myScript + " ; le_exec"}; r.exec(cmdArray).waitFor(); } catch (InterruptedException ex){ ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } 

The -e option can be used to call either a script or another program, for example: javac. In this case, you would do the following task: myScript = "javac".

You will need the " ; le_exec" part if you do not want the window to close immediately after execution, otherwise cancel it. A semi-colon is used to separate commands, and le_exec is just a script that waits for the user to press Enter.

However, if your script arguments need arguments, then you will need to replace String myScript and an array of strings.

+3
source share

Actually there is no "terminal". What you really want is a shell. Most Linuxes use BaSH as a shell, but in order to be safe, you must limit yourself to / bin / sh

Edit: Hmm, I probably missed this question. If you need an interactive terminal, you should learn it using the tools that provide the component for this.

Edit2: Perhaps your needs may be served by VTE, which is a component of GTK + for embedding a terminal.

+1
source share

I think it would be nice if your application opens the default user application.

Unfortunately, there seems to be no universal way to define it.

In my opinion, the most preferred solution would be (stopping whenever you can open something):

  • try to restore the user's desktop environment and determine its default terminal application. You can read something about it here .
  • check environment variable $ TERM
  • Ask the user about her preference and save it in the configuration file.

Finding a user desktop environment and its default terminal may be too complicated for your purpose, so I would use the last two options. You can run the application from $ TERM with the following code:

 String command = System.getenv().get("TERM"); Runtime rt = Runtime.getRuntime(); Process pr = rt.exec(command); 
+1
source share

I would definitely provide an easy way to configure the path to the desired terminal in a place that can be easily edited. For example, perhaps in a configuration configuration file.

Different distributions will keep this in different places, so it’s best to check the mailing list documentation for the platforms you are targeting (and document how to change them).

"/ usr / bin / xterm" should be on most machines, but I would not put a farm on it.

0
source share

since you should assume that you know almost nothing about the system in which you work, I would say that the lowest common denominator would be:

String command = "/ bin / sh";

  • or even more "guaranteed" -

String command = "sh";

0
source share

All Articles