0

I am writing a platform independent Java console tool and within which will have to execute some commands in separate independent terminal at runtime.

However, I need to know which terminal should I be using gnome-terminal or xterm. i.e. the one available on that particular system. Since there are many different linux variants are available. I want to support at least the most obvious ones.

Normally in Java System.getProperty("os.name") returns Linux but it doesn't tell you if its Ubuntu or any other Linux varient. So It is difficult to know which terminal to start at runtime. Whether gnome-terminal or xterm or if there is anything else as well that I don't know.

Working sets:

In Windows:

cmd /c start java -jar jarTool arguments

In Ubuntu:

gnome-terminal --execute java -jar jarTool arguments

or

xterm -e java -jar jarTool arguments

Also just for additional information,I am using Runtime.getRuntime().exec(args or commands) to start terminal from Java program at runtime.

Indigo
  • 109

2 Answers2

1

There's no easy method for determining which terminal program to use on Linux. Even though it's possible to get distribution info, use may or may not remove default GUI terminal program and install others. Generally the approach is to get a list of common terminal programs and find the first one usable. As an example, here is how virtualbox linux guest searches for usable terminal:

GUESS_XTERMS="xterm rxvt dtterm eterm Eterm kvt konsole aterm"
for a in $GUESS_XTERMS; do
  if type $a >/dev/null 2>&1; then
    XTERM=$a
    break
  fi
done
  • Indeed, there is not easy way to know which linux distribution iss and get a default terminal. For now I am going to use generic xterm. Until I find a proper solution. I tried this script and it gave me xterm as result rather than gnome-terminal on ubuntu. – Indigo Jul 28 '14 at 21:24
  • 1
    FYI, lsb_release -a shall show the distribution info if distro is conforming LSB standard. Though xterm is quite a safe fallback for old school distro, it may not be installed by default on some newer ones, such as Linux Mint. Therefore checking among a list is safer than just picking single one, especially if platform independent is a goal. – Abel Cheung Jul 28 '14 at 21:33
  • Thanks, but I am looking for something to use within Java. Will play around a bit. – Indigo Jul 28 '14 at 21:45
0

It's better if you execute the commands using /Bin/Bash <terminal command> This would work for every linux distribution.

switch87
  • 926
  • Using your example it would be: /bin/bash java -jar jarTool arguments – switch87 Jul 28 '14 at 17:31
  • thanks that's what I what looking for. well just tried and got this error without even opening a separate terminal window,

    /usr/bin/java: /usr/bin/java: cannot execute binary file

    With command /bin/bash java -jar jarTool args

    Any idea

    – Indigo Jul 28 '14 at 17:38
  • I don't know for shure, but I think you may have the wrong version of jdk installed? – switch87 Jul 28 '14 at 17:46
  • No that's not the case. The jar tool works well if manually executed in Windows, Ubuntu, and Mac. I know this could be a problem of being a java console tool. javaw does not open java console. So you have to force the cmd or terminal to open. – Indigo Jul 28 '14 at 21:22