java - 如何修复未在 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();
}
}
}
解决方案
通过导出变量或使用 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
如果您想了解更多信息,您应该查看终端的基础知识。我希望这可以帮助你达到你想要的结果。
干杯
推荐阅读
- mysql - MySQL游标不适用于日期变量
- python - Openpyxl 无法加载工作簿
- r - 在 R 中用 0 拟合 beta 分布
- keycloak - Keycloak accountDisableMessage 属性不起作用
- php - 未定义变量:posts(查看:/app/resources/views/bbs/index.blade.php)
- javascript - 当用户单击用户列时,在链接上添加弹出模式以打开
- email - 使用 Mail 和 AppleScript 转发选定电子邮件的问题
- lotus-notes - 读取“告诉”命令的响应
- .net-core - dotnet publish:清理外部输出目录
- php - 有没有办法获得用于成功交易的卡类型?