Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.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
如何在Java中运行Linux命令时仅显示结果?_Java_Linux_Jsch - Fatal编程技术网

如何在Java中运行Linux命令时仅显示结果?

如何在Java中运行Linux命令时仅显示结果?,java,linux,jsch,Java,Linux,Jsch,我正在使用JSch在远程Linux框中运行一些命令 session = jsch.getSession(user, "host", 1222); ... Channel shellChannel = session.openChannel("shell"); OutputStream ops = shellChannel.getOutputStream(); PrintStream ps = new PrintStream(ops, true); ps.println("pwd"); Inpu

我正在使用JSch在远程Linux框中运行一些命令

session = jsch.getSession(user, "host", 1222);
...
Channel shellChannel = session.openChannel("shell");
OutputStream ops = shellChannel.getOutputStream();
PrintStream ps = new PrintStream(ops, true);

ps.println("pwd");
InputStream in = shellChannel.getInputStream();
...
//print the char stream from 'in' using something similar to 
//http://stackoverflow.com/questions/9126142/output-the-result-of-a-bash-script#
但是,
inputStream
的结果打印输出包含所有内容,包括用户提示、我的原始命令(
pwd
,在本例中),以及结果
/u02/app2/bbisit

bbisit@sdvdbres016$ 
bbisit@sdvdbres016$pwd
/u02/app2/bbisit
但我真正想要的是命令的实际输出,即
/u02/app2/bbisit

是否有办法只获取命令的实际结果,而不获取其他垃圾。

使用“exec”通道,而不是“shell”通道

“exec”通道用于自动执行命令。“shell”通道用于实现交互式shell