title: Execute perl script in remote server process builder in java
body: <p>I have an issue where I have application code running in abc server but perl script is located in xyz server.</p>
  <p>Due to server compatibility issue I cannot run perl script in abc server.</p>
  <p>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.</p>
  <p>I already tried using jsch but no help. Please help me how to achieve solution using process builder.</p>
  <pre><code>    private StringBuilder runCmd(File folder, ArrayList&lt;String&gt; cmd)
          throws IOException {
      ProcessBuilder pb = new ProcessBuilder().command(cmd).directory(folder);
      Map&lt;String, String&gt; 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(&quot;\n&quot;);
      }
  
      try {
          int exitVal = pr.waitFor();
          log.debug(pb+&quot; value: &quot;+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&lt;String, String&gt; env = pb.environment();
  String host=&quot;xyz&quot;;
  String user=&quot;example&quot;;
  String password=&quot;pw&quot;;
  String command1=&quot;perl abc.perl&quot;;
  java.util.Properties config = new java.util.Properties();
  config.put(&quot;StrictHostKeyChecking&quot;, &quot;no&quot;);
  JSch jsch = new JSch();
  Session session=jsch.getSession(user, host, 22);
  session.setPassword(password);
  session.setConfig(config);
  session.connect();
  Channel channel=session.openChannel(&quot;exec&quot;);
  ((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(&quot;\n&quot;);
    }
  channel.disconnect();
  return sb;
  </code></pre>