I have an issue where I have application code running in abc server but perl script is located in xyz server.
Due to server compatibility issue I cannot run perl script in abc server.
Can someone please help me how to execute the perl script in xyz server and get the output in java application. I am using processbuilder to execute the script.
I already tried using jsch but no help. Please help me how to achieve solution using process builder.
private StringBuilder runCmd(File folder, ArrayList<String> cmd)
throws IOException {
ProcessBuilder pb = new ProcessBuilder().command(cmd).directory(folder);
Map<String, String> env = pb.environment();
pb.redirectErrorStream(true);
log.debug(pb.command());
Process pr = pb.start();
BufferedReader br = new BufferedReader(new InputStreamReader(pr.getErrorStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line).append("\n");
}
try {
int exitVal = pr.waitFor();
log.debug(pb+" value: "+exitVal);
}
catch (InterruptedException e) {
log.error(e.getMessage());
sb.append(e.getMessage());
}
return sb;
}
Jsch code
ProcessBuilder pb = new ProcessBuilder().command(cmd).directory(folder); Map<String, String> env = pb.environment();
String host="xyz";
String user="example";
String password="pw";
String command1="perl abc.perl";
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
JSch jsch = new JSch();
Session session=jsch.getSession(user, host, 22);
session.setPassword(password);
session.setConfig(config);
session.connect();
Channel channel=session.openChannel("exec");
((ChannelExec)channel).setCommand(command1);
channel.setInputStream(null);
((ChannelExec)channel).setErrStream(System.err);
channel.connect();
ByteArrayOutputStream outputBuffer = new ByteArrayOutputStream();
ByteArrayOutputStream errorBuffer = new ByteArrayOutputStream();
StringBuilder sb = new StringBuilder();
InputStream in = channel.getInputStream();
String line;
channel.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
while ((line = reader.readLine()) != null)
{
sb.append(line).append("\n");
}
channel.disconnect();
return sb;