Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从shell中的Linux命令获取JAVA的字符串或值?_Java_Shell - Fatal编程技术网

如何从shell中的Linux命令获取JAVA的字符串或值?

如何从shell中的Linux命令获取JAVA的字符串或值?,java,shell,Java,Shell,可以将linux shell中的命令输出到JAVA应用程序吗?如果可能的话怎么办?我看到了从JAVA执行shell命令的机会,我希望得到相反的结果:shell命令输出---->to---->JAVA变量或字符串。感谢回复Ok让我们假设您想在linux中运行X命令 然后在终端类型中: x > "myfilename" 举个例子: netstat > "xyz" 这将创建一个文件“xyz”,并将所有输出传输到该文件 这将把所有输出放在“myfilename”中 现在,您可以从java

可以将linux shell中的命令输出到JAVA应用程序吗?如果可能的话怎么办?我看到了从JAVA执行shell命令的机会,我希望得到相反的结果:shell命令输出---->to---->JAVA变量或字符串。感谢回复

Ok让我们假设您想在linux中运行X命令

然后在终端类型中:

x > "myfilename"
举个例子:

netstat > "xyz"
这将创建一个文件“xyz”,并将所有输出传输到该文件

这将把所有输出放在“myfilename”中

现在,您可以从java应用程序读取文件的所有内容

备选方案:

您可以从java运行shell命令并收集输出以供以后处理。

1命令行参数 假设您试图在启动java程序时将linux命令的输出传递给java,这在
bash
中很简单。使用倒勾(`)将linux命令环绕在放置命令行参数的位置。例如:

$ java [... java options, like -jar path/to/file.jar ...] -- `linux-command`
(如果输出包含空格,则可能需要执行一些引号或某种类型的转义。)

然后,在java程序中,该值将位于
args
数组中:

public static void main(String args[]) {
    String linuxCommandOutput = args[0];
    // rest of program...
}
2系统属性 如果由于某种原因无法使用
args
,可以尝试使用系统属性。同样,使用back ticks(`)环绕linux命令,并将其存储在带有
-D
的系统属性中。像这样:

$ java -Dvariable=`linux-command` [... java options ...]
然后,在java程序中,读取系统属性的值:

public static void main(String args[]) {
    String linuxCommandOutput = System.getProperty("variable");
    // rest of program...
}

您可以使用jcraft,然后执行返回输出的命令

范例

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;
}
host=//您的主机IP;
字符串user=“username”;
字符串密码=“pwd”;
String command=“要执行的命令”;
试一试{
java.util.Properties config=new java.util.Properties();
配置放置(“检查”、“否”);
JSch JSch=新的JSch();
Session Session=jsch.getSession(用户,主机,22);
session.setPassword(密码);
session.setConfig(config);
session.connect();
System.out.println(“已连接”);
Channel=session.openChannel(“exec”);
((ChannelExec)channel).setCommand(command);
channel.setInputStream(空);
((ChannelExec)channel.setErrStream(System.err);
InputStream in=channel.getInputStream();
channel.connect();
字节[]tmp=新字节[1024];
while(true){
while(in.available()>0){
inti=in.read(tmp,0,1024);
if(i<0)
打破
append(新字符串(tmp,0,i));
//System.out.print(新字符串(tmp,0,i));//命令输出
}
if(channel.isClosed()){
System.out.println(“退出状态:+channel.getExitStatus());
打破
}
试一试{
睡眠(1000);
}捕获(异常ee){
}
}
通道断开();
session.disconnect();
系统输出打印项次(“完成”);
}捕获(例外e){
e、 printStackTrace();
返回false;
}

suraj的可能重复问题与“如何在java代码中运行linux命令”相反,您的意思是,您想从java执行命令并将该命令的输出存储在java varaiable中吗?