0

I am trying to execute a Linux shell script in a remote server from Java program using JSch. I was successfully able to execute the script when I logged in to the server and run the script using putty, but when I am running through my Java program, it is giving the following error:

"<<script_name>>: Warning distribution home directory (HOME variable) is not defined or can't be read"

Below is my Java program:

JSch jsch = new JSch();
            session = jsch.getSession(username, remoteHost);
            session.setConfig("StrictHostKeyChecking", "no");
            session.setPassword(password);
            session.connect();
            channelExec = (ChannelExec) session.openChannel("exec");
            InputStream in = channelExec.getInputStream();
            channelExec.setCommand("sh "+scriptFileName);
            channelExec.connect();

            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
                result.add(line);
            }
            exitStatus = channelExec.getExitStatus();

Please help. Iam using Spring Boot in my project.

Anirban
  • 925
  • 4
  • 24
  • 54
  • Does this answer your question? [JSch: Is there a way to expose user environment variables to "exec" channel?](https://stackoverflow.com/questions/46349113/jsch-is-there-a-way-to-expose-user-environment-variables-to-exec-channel) – Martin Prikryl Apr 03 '20 at 06:35
  • No @MartinPrikryl, my problem did not get solved and the HOME variable is not accessible, even if we do setPty(true) on ChannelExec – Anirban Apr 03 '20 at 12:54
  • That's only one of at least three proposed solutions. Did you try the other ones? – Martin Prikryl Apr 03 '20 at 12:57
  • I am not able to get the first solution. Regarding the secong solution of modifying the startup scripts, it is not feasible in our case. – Anirban Apr 03 '20 at 13:27
  • I was trying this right now and came across a solution. The solution is to pass the environment variable in the command for executing the script. i.e. calling the script like HOME=VARIABLE sh script.sh. This solved my problem – Anirban Apr 03 '20 at 13:29

0 Answers0