首页 > 解决方案 > 如何修复未在 Intellij 中设置的 TERM 环境?

问题描述

我目前正在开发一个包含 Jsch 的 java 自动化应用程序。但是,当我运行我的代码时,它会返回一个错误,指出未设置 TERM 环境。

我已经尝试通过选择环境变量在 intellij 中手动添加环境。然后我添加 TERM=xterm。虽然当我运行它时,它仍然失败。

import com.jcraft.jsch.*;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;


public class Driver {

public static void main(String[] args) throws Exception {

    JSch jsch = new JSch();

    Session session;
    try {

        // Open a Session to remote SSH server and Connect.
        // Set User and IP of the remote host and SSH port.
        session = jsch.getSession("username", "host", 22);
        // When we do SSH to a remote host for the 1st time or if key at the remote host
        // changes, we will be prompted to confirm the authenticity of remote host.
        // This check feature is controlled by StrictHostKeyChecking ssh parameter.
        // By default StrictHostKeyChecking  is set to yes as a security measure.
        session.setConfig("StrictHostKeyChecking", "no");
        //Set password
        session.setPassword("password");
        session.connect();

        // create the execution channel over the session
        ChannelExec channelExec = (ChannelExec) session.openChannel("exec");
        // Set the command to execute on the channel and execute the command
        channelExec.setCommand("./script.sh");
        channelExec.connect();

        // Get an InputStream from this channel and read messages, generated
        // by the executing command, from the remote side.
        InputStream in = channelExec.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        String line;
        while ((line = reader.readLine()) != null) {
            System.out.println(line);
        }

        // Command execution completed here.

        // Retrieve the exit status of the executed command
        int exitStatus = channelExec.getExitStatus();
        if (exitStatus > 0) {
            System.out.println("Remote script exec error! " + exitStatus);
        }
        //Disconnect the Session
        session.disconnect();
    } catch (JSchException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}

标签: javaintellij-idea

解决方案


通过导出变量或使用 TERM 的设置变量运行代码,确保在当前 shell 中设置变量。

类似于以下内容的东西应该可以工作:

TERM=linux /path/to/your/executable --some-arguments

以下可能仅与 bash 有关,但也有一种方法可以导出变量以使其成为全局变量。

导出变量后,您可以使用以下方法验证其值:

echo $TERM

空响应意味着未设置变量。否则,嗯......你明白了,我敢肯定。为了全局导出它,在bash中,你可以直接使用命令行或将导出命令添加到你的dotfiles中,应该在登录时加载

export TERM=linux

无论您选择哪种方式,命令都保持不变。有多种终端和类型,“linux”是一个非常通用的终端。一个对颜色更友好的解决方案可能是尝试使用“xterm-256color”。

export TERM=xterm-256color

如果您想了解更多信息,您应该查看终端的基础知识。我希望这可以帮助你达到你想要的结果。

干杯


推荐阅读