【问题标题】:How to fix TERM environment not set in Intellij?如何修复未在 Intellij 中设置的 TERM 环境?
【发布时间】:2019-01-07 16:32:17
【问题描述】:

我目前正在开发一个包含 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();
    }
}

}

【问题讨论】:

  • 可能已经回答:stackoverflow.com/q/13748784/1531971stackoverflow.com/q/12900923/1531971(如果没有,edit 问题并告诉我们为什么这些不适用。)
  • @jdv 顺便感谢一下。虽然您发布的问题的答案只是告诉我去哪里更改或添加环境变量。我已经知道去哪里了,我只需要一种方法来修复我的代码中的一个错误,它给我一个未知的终端错误。我相信这与我的 IDE 及其与 TERM 变量的配置有关。

标签: java intellij-idea


【解决方案1】:

通过导出变量或使用设置的 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

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

干杯

【讨论】:

    【解决方案2】:

    IntelliJ IDEA 运行控制台不是真正的终端,因此存在问题。

    您可以在 IntelliJ IDEA 之外或在终端工具窗口中手动运行代码。

    对于调试,您可以使用远程调试。

    相关请求:Add option to run configuration to launch in real console

    【讨论】:

      猜你喜欢
      • 2013-04-20
      • 2013-10-25
      • 2017-10-30
      • 2014-02-10
      • 1970-01-01
      • 2019-03-24
      • 2017-08-26
      • 1970-01-01
      • 2015-06-18
      相关资源
      最近更新 更多