1

I am currently working on a java automation application incorporating Jsch. When I run my code however, it passes back an error saying that the TERM environment is not set up.

I already tried to manually add the environment in intellij by choosing environment variables. Then I add TERM=xterm. Though when I run that, it still fails.

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();
    }
}

}

tharriott
  • 101
  • 2
  • 11
  • Maybe already answered: https://stackoverflow.com/q/13748784/1531971 https://stackoverflow.com/q/12900923/1531971 (If not, [edit] the question and tell us why these don't apply.) –  Jan 07 '19 at 16:34
  • @jdv Thanks by the way.Though the answer to the question you posted, just tells me where to go to change or add an environmental variable. I already know where to go, I just need a way to fix an error in my code that is giving me a unknown terminal error. I believe it has something to do with my IDE and its configuration with the TERM variable. – tharriott Jan 07 '19 at 16:55

2 Answers2

2

Make sure that the variable is set in your current shell by either exporting it or running your code with a set variable for TERM.

Something similar to the following should work:

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

The following could possibly only be relevant to bash but there is also a way to export a variable so as to make it global.

After exporting a variable, you can verify its value using:

echo $TERM

Empty response means variable is not set. Else, well... you get it i am sure. In order to export it globally, in bash that is, you can use the command line directly or add the export command into your dotfiles, which should be loaded upon login

export TERM=linux

Either way you choose, the command stays the same. There are multiple terminals and types, 'linux' being a very very generic one. A more color-friendly solution could be to try using 'xterm-256color' instead.

export TERM=xterm-256color

You should check out the basics of terminal if you wish to learn more. I hope this can help you achive your desired outcome.

Cheers

OldFart
  • 1,640
  • 11
  • 20
  • I've set export TERM=xterm-256color in my .bashrc on remote host. In terminal echo $TERM gives right result, but still trying connest to backend with Jetbrains Gateway reports this error – Adam Mierzwiak Apr 28 '23 at 09:57
1

IntelliJ IDEA Run console is not a real Terminal, hence the problem.

You can run the code manually outside of IntelliJ IDEA or in the Terminal tool window.

For debugging you can use the Remote debug.

Related request: Add option to run configuration to launch in real console.

CrazyCoder
  • 389,263
  • 172
  • 990
  • 904