我目前正在开发一个包含 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 中,您可以直接使用命令行或将导出命令添加到您的点文件中,该文件应在登录时加载
export TERM=linux
无论您选择哪种方式,命令都保持不变。有多种终端和类型,“linux”是一种非常非常通用的终端和类型。一种对颜色更友好的解决方案可能是尝试使用“xterm-256color”。
export TERM=xterm-256color
如果您想了解更多信息,您应该查看终端的基础知识。我希望这可以帮助您实现您想要的结果。
干杯
关于java - 如何修复 Intellij 中未设置的 TERM 环境?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54078262/