java中shell_java中shell调用

该代码片段展示了如何在Java中使用Runtime类执行`ls-la /`命令,并读取其输出,打印出目录列表。程序首先定义了一个main方法,然后调用callShell方法执行shell命令。callShell方法通过Runtime.exec()创建子进程执行命令,捕获标准输出,并将结果存储到List中。
摘要由CSDN通过智能技术生成

package com.tuxianchao;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.ArrayList;

import java.util.List;

/**

* created by

*

* @author tuxianchao

* @date 2018/6/11 上午10:43

*/

public class Test {

public static void main(String[] args) {

for (String line : callShell("ls -la /")) {

System.out.println(line);

}

/**

* console output

*total 94

* drwxr-xr-x 35 root wheel 1258 May 12 19:21 .

* drwxr-xr-x 35 root wheel 1258 May 12 19:21 ..

* -rw-rw-r-- 1 root admin 6148 Feb 4 2017 .DS_Store

* d--x--x--x 9 root wheel 306 Jun 6 13:17 .DocumentRevisions-V100

* drwxr-xr-x 2 root wheel 68 Nov 13 2014 .PKInstallSandboxManager

* drwxr-xr-x@ 2 root wheel 68 Jun 6 21:43 .PKInstallSandboxManager-SystemSoftware

* drwx------ 5 root wheel 170 Nov 13 2014 .Spotlight-V100

* d-wx-wx-wt 2 root wheel 68 May 8 13:04 .Trashes

* -rw------- 1 root wheel 12288 Oct 25 2016 .bash_profile.swp

* ---------- 1 root admin 0 Feb 21 2017 .file

* drwx------ 97 root wheel 3298 Jun 11 03:30 .fseventsd

* drwxr-xr-x@ 2 root wheel 68 Oct 24 2016 .vol

* drwxrwxr-x+ 61 root admin 2074 Jun 6 11:34 Applications

* drwxr-xr-x+ 63 root wheel 2142 Nov 9 2017 Library

* drwxr-xr-x@ 2 root wheel 68 Oct 24 2016 Network

* drwxr-xr-x@ 4 root wheel 136 May 12 19:23 System

* drwxr-xr-x 7 root admin 238 May 8 13:04 Users

* drwxr-xr-x@ 4 root wheel 136 Jun 8 13:47 Volumes

* drwxr-xr-x@ 38 root wheel 1292 May 12 19:23 bin

* drwxrwxr-t@ 2 root admin 68 Oct 24 2016 cores

* dr-xr-xr-x 3 root wheel 4614 Jun 6 13:17 dev

* lrwxr-xr-x@ 1 root wheel 11 Oct 24 2016 etc -> private/etc

* dr-xr-xr-x 2 root wheel 1 Jun 6 13:32 home

* -rw-r--r--@ 1 root wheel 313 Jul 31 2016 installer.failurerequests

* dr-xr-xr-x 2 root wheel 1 Jun 6 13:32 net

* drwxr-xr-x@ 3 root wheel 102 Oct 22 2016 opt

* drwxr-xr-x@ 6 root wheel 204 Oct 24 2016 private

* drwxr-xr-x@ 63 root wheel 2142 May 12 19:21 sbin

* lrwxr-xr-x@ 1 root wheel 11 Oct 24 2016 tmp -> private/tmp

* drwxr-xr-x@ 10 root wheel 340 Sep 20 2017 usr

* lrwxr-xr-x@ 1 root wheel 11 Oct 24 2016 var -> private/var

* drwxr-xr-x 15 502 staff 510 Aug 26 2016 原来的文件

* lrwxr-xr-x 1 root wheel 49 Mar 23 2015 用户信息 -> /Library/Documentation/User Information.localized

*

*/

}

public static List callShell(String shellString) {

List result = new ArrayList();

BufferedReader bufferedReader = null;

try {

Process process = Runtime.getRuntime().exec(shellString);

int exitValue = process.waitFor();

if (0 != exitValue) {

System.out.println("call shell failed. error code is :" + exitValue);

}

//正常结束,Process的waitFor()方法返回0

bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));

String line;

while ((line = bufferedReader.readLine()) != null) {

result.add(line);

}

} catch (Throwable e) {

System.out.println("call shell failed. " + e);

} finally {

try {

bufferedReader.close();

} catch (IOException e) {

e.printStackTrace();

}

}

return result;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!针对您的问题,我可以给出以下解答: Java JSch 是一个用于远程执行命令和传输文件的 Java 库,通过 JSch,我们可以在 Java 应用程序连接到远程主机,并执行远程主机上的命令。 针对您的问题,如果您在使用 Java JSch 调用 shell 脚本时,脚本执行后立即退出,可能是因为您的 Java 程序没有等待 shell 脚本执行完毕就立即退出了。 为了解决这个问题,您可以在调用 shell 脚本的代码,使用 `ChannelExec.setPty(true)` 方法启用伪终端模式,并使用 `ChannelExec.setCommand(command + ";echo finished")` 方法在 shell 脚本执行完毕后输出一个 "finished" 的标识符。然后您可以使用 `ChannelExec.getInputStream()` 方法获取 shell 脚本的输出流,并等待输出流包含 "finished" 标识符,表示 shell 脚本已经执行完毕,然后再关闭连接。 以下是一个示例代码: ``` JSch jsch = new JSch(); Session session = jsch.getSession(username, host, port); session.setPassword(password); session.setConfig("StrictHostKeyChecking", "no"); session.connect(); ChannelExec channelExec = (ChannelExec) session.openChannel("exec"); channelExec.setPty(true); channelExec.setCommand(command + ";echo finished"); channelExec.setInputStream(null); channelExec.setErrStream(System.err); InputStream inputStream = channelExec.getInputStream(); channelExec.connect(); byte[] tmp = new byte[1024]; while (true) { while (inputStream.available() > 0) { int i = inputStream.read(tmp, 0, 1024); if (i < 0) break; System.out.print(new String(tmp, 0, i)); } if (channelExec.isClosed()) { if (inputStream.available() > 0) continue; System.out.println("exit-status: " + channelExec.getExitStatus()); break; } try { Thread.sleep(1000); } catch (Exception ee) {} } channelExec.disconnect(); session.disconnect(); ``` 希望这个解答能够帮到您!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值