java - JSch to use Unix Remote Server Environment Variables -
i'm using jsch connect java remote unix server. next step run shell script (.sh) makes use of internal environment variables.
on executing shell script directly terminal works fine, calling java program via jsch not recognize these variables. how work around this. less experienced on java. posting sample code-
jsch jsch = new jsch(); session session = jsch.getsession("username", "server", 22); userinfo ui = new sftpuserinfo(); session.setuserinfo(ui); session.setpassword("mypass"); session.connect(); channelexec channelexec = (channelexec)session.openchannel("exec"); inputstream in = channelexec.getinputstream(); channelexec.setcommand("sh /filepath/myshellscript.sh"); channelexec.connect(); bufferedreader reader = new bufferedreader(new inputstreamreader(in)); string line; while ((line = reader.readline()) != null) { system.out.println(line); }
ps: environment variables set @ ".profile" in users root folder (/home/username/)
thanks,
arya
i execute jar stored on server using jsch. jar uses class processbuilder allows modifying environment variables. example:
processbuilder pb = new processbuilder("sh /filepath/myshellscript.sh"); map<string, string> env = pb.environment(); env.put("profile","/home/username/");