Francesco Valla
Francesco Valla

Reputation: 214

how to get from Linux command inside shell a string or value to JAVA?

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

Upvotes: 2

Views: 442

Answers (3)

Monis Majeed
Monis Majeed

Reputation: 1378

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

Upvotes: 1

izrik
izrik

Reputation: 952

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

Upvotes: 0

Arvindsinc2
Arvindsinc2

Reputation: 716

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.

Upvotes: 1

Related Questions