2

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

3
  • Possible duplicate of How to run linux commands in java code? Commented Mar 30, 2017 at 14:45
  • suraj it's the opposite question not " How to run linux commands in java code? " Commented Mar 30, 2017 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? Commented Mar 30, 2017 at 14:59

3 Answers 3

1

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;
}
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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...
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.