首页 > 解决方案 > 无法使用 JSch 从 Java 程序在远程服务器中执行 Linux shell 脚本

问题描述

我正在尝试使用 JSch 从 Java 程序在远程服务器中执行 Linux shell 脚本。当我登录到服务器并使用 putty 运行脚本时,我成功地执行了脚本,但是当我通过我的 Java 程序运行时,它给出了以下错误:

"<<script_name>>: Warning distribution home directory (HOME variable) is not defined or can't be read"

下面是我的Java程序:

JSch jsch = new JSch();
            session = jsch.getSession(username, remoteHost);
            session.setConfig("StrictHostKeyChecking", "no");
            session.setPassword(password);
            session.connect();
            channelExec = (ChannelExec) session.openChannel("exec");
            InputStream in = channelExec.getInputStream();
            channelExec.setCommand("sh "+scriptFileName);
            channelExec.connect();

            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
                result.add(line);
            }
            exitStatus = channelExec.getExitStatus();

请帮忙。我在我的项目中使用 Spring Boot。

标签: linuxspring-bootshelljava-8jsch

解决方案


推荐阅读