栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

通过JSch在Java上运行Linux命令

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

通过JSch在Java上运行Linux命令

这就是我所做的。

exec 2>&1
repoUpdate.sh 文件的顶部添加了,并添加了这段代码以按@DanielMartin的建议读取输出错误,结果是:

public void cmremove(){    try    {        JSch jsch = new JSch();        Session session = jsch.getSession(user, host, port);        UserInfo ui = new SUserInfo(pass, null);        session.setUserInfo(ui);        session.setPassword(pass);        session.connect();        ChannelExec channelExec = (ChannelExec)session.openChannel("exec");        InputStream in = channelExec.getInputStream();        channelExec.setCommand("./repoUpdate.sh");        channelExec.connect();        BufferedReader reader = new BufferedReader(new InputStreamReader(in));        String line;        int index = 0;        while ((line = reader.readLine()) != null)        { System.out.println(++index + " : " + line);        }        int exitStatus = channelExec.getExitStatus();        channelExec.disconnect();        session.disconnect();        if(exitStatus < 0){ System.out.println("Done, but exit status not set!");        }        else if(exitStatus > 0){ System.out.println("Done, but with error!");        }        else{ System.out.println("Done!");        }    }    catch(Exception e)    {        System.err.println("Error: " + e);    }}

因此,实际上有很大帮助。它确认命令实际上没有按照我的想法正确执行。我在我的java输出中得到了这个:

run:1 :  ****Repository update****2 :  Location: /home/cissys/repo/3 :  Command: svn update /home/cissys/repo/2.3.04 : ./repoUpdate.sh[6]: svn: not found [No such file or directory]Done!BUILD SUCCESSFUL (total time: 2 seconds)

然后,我尝试修改 repoUpdate.sh 文件,为svn命令添加绝对路径(谢谢@ymnk)

exec 2>&1echo  ' ****Repository update****'echo  ' Location: /home/cissys/repo/'echo -e ' Command: svn update /home/cissys/repo/2.3.0'/opt/subversion/bin/svn update /home/cissys/repo/2.3.0

对于我的Java,我得到了我想要的东西:

run:1 :  ****Repository update****2 :  Location: /home/cissys/repo/3 :  Command: svn update /home/cissys/repo/2.3.04 : At revision 9443.Done!BUILD SUCCESSFUL (total time: 3 seconds)

我发现

$PATH
通过java从会话中获取的信息与直接从linux控制台获取的信息不同。因此,添加svn路径实际上可以解决问题。非常感谢您的帮助!



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/570612.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号