0

I'm creating a Java library as the backend for a Tomcat web interface. The purpose of the library is to take arguments from the interface and use them to execute other commands on the system.

I know I can use Runtime.getRuntime().exec() to send the command, but is there a way to identify what shell the commands are sent to?

I'd like to use syntax I know from BASH to direct stdout to a target file, but I'm not sure if I'm in BASH, Bourne, or something else unique to the Java runtime.

Desired result: Shell flexibility. Detect when I can do

Runtime.getRuntime().exec(myCommand + " >> " + myTargetFile);

and when that would be an invalid operator.

EDIT: formatting

2mac
  • 1,609
  • 5
  • 20
  • 35
  • 1
    You can exec the desired shell with the command. A search will deliver numerous examples. Runtime or especially ProcessBuilder are not difficult however. – Joop Eggen Jun 27 '14 at 14:50
  • As in `Runtime.getRuntime().exec("bash " + myCommand)` ? – 2mac Jun 27 '14 at 14:52
  • And, BTW, executing "command >> file" will not work from Java. >> is a shell redirect, so unless you're specifically creating a shell it will be treated as a program argument. Many other stackoverflow questions address this point and how to handle it. – user1676075 Jun 27 '14 at 16:21
  • Take a look at `System.getProperty("os.name")` – Elliott Frisch Jun 30 '14 at 08:39

0 Answers0