it's possible to get the output of a command inside linux shell to a JAVA app? if it's possible how? I see the chance to execute from JAVA a shell command i wish the opposite result : SHELL COMMANDS OUTPUT --->to---> Java VARIABLE or STRING. thanks for reply
- 
                    Possible duplicate of [How to run linux commands in java code?](http://stackoverflow.com/questions/3403226/how-to-run-linux-commands-in-java-code) – Suraj Rao Mar 30 '17 at 14:45
- 
                    suraj it's the opposite question not " How to run linux commands in java code? " – Francesco Valla Mar 30 '17 at 14:51
- 
                    do u mean , you want to execute a command from java and get the output of that command to be stored in java varaiable? – Monis Majeed Mar 30 '17 at 14:59
3 Answers
Ok lets assume you want to run X command in linux.
then in the terminal type:
x > "myfilename"
Just to give an example:
netstat > "xyz"
This will create a file 'xyz' and transfer all the output to it.
This will put all the output to the 'myfilename'.
Now you can read all the contents of the file from a java application.
Alternative:
You can run shell command from java and collect the output for later processing.
 
    
    - 716
- 7
- 18
You can use jcraft and then execute command which returns the output
Example
host = //your hostIP;
String user = "username";
String password = "pwd";
String command = "the command you want to execute";
try {
  java.util.Properties config = new java.util.Properties();
  config.put("StrictHostKeyChecking", "no");
  JSch jsch = new JSch();
  Session session = jsch.getSession(user, host, 22);
  session.setPassword(password);
  session.setConfig(config);
  session.connect();
  System.out.println("Connected");
  Channel channel = session.openChannel("exec");
  ((ChannelExec) channel).setCommand(command);
  channel.setInputStream(null);
  ((ChannelExec) channel).setErrStream(System.err);
  InputStream in = channel.getInputStream();
  channel.connect();
  byte[] tmp = new byte[1024];
  while (true) {
    while (in.available() > 0) {
      int i = in.read(tmp, 0, 1024);
      if (i < 0)
        break;
      area.append(new String(tmp, 0, i));
      //System.out.print(new String(tmp, 0, i)); //command output
    }
    if (channel.isClosed()) {
      System.out.println("exit-status: " + channel.getExitStatus());
      break;
    }
    try {
      Thread.sleep(1000);
    } catch (Exception ee) {
    }
  }
  channel.disconnect();
  session.disconnect();
  System.out.println("DONE");
} catch (Exception e) {
  e.printStackTrace();
  return false;
}
 
    
    - 1,358
- 14
- 21
1 Command line argument
Assuming you're trying to pass the output of a linux command to java when starting the java program, this is simple to do in bash. Use back-ticks (`) to surround the linux command in the place where you put command line arguments. E.g.:
$ java [... java options, like -jar path/to/file.jar ...] -- `linux-command`
(You may have to do some quotes or escaping of some sort if the output contains spaces.)
Then, in your java program, the value will be in the args array:
public static void main(String args[]) {
    String linuxCommandOutput = args[0];
    // rest of program...
}
2 System Property
If you can't use args for some reason, you can try to use system properties. Again, use back-ticks (`) to surround the linux command and store it in a system property with -D. Like so:
$ java -Dvariable=`linux-command` [... java options ...]
Then, in your java program, read the value of the system property:
public static void main(String args[]) {
    String linuxCommandOutput = System.getProperty("variable");
    // rest of program...
}
 
    
    - 918
- 2
- 9
- 20
