title: Execute java code on remote system via ssh
body: <p>I want to run a java program on my local PC which connects to a remote PC with ssh and executes some methods (java code, not bash scripts!) there. I found for example <code>JSch</code> which enables the ssh connection with and remote execution of code with something like</p>
  
  <pre><code>JSch jsch=new JSch();
  Session session=jsch.getSession(user, host, 22);
  session.setPassword(passwd);
  Channel channel=session.openChannel("shell");
  channel.setInputStream(System.in);
  </code></pre>
  
  <p>(from <a href="http://www.jcraft.com/jsch/examples/Shell.java.html" rel="nofollow">http://www.jcraft.com/jsch/examples/Shell.java.html</a>)</p>
  
  <p>or <code>ssxcute</code> which can be used with </p>
  
  <pre><code>ConnBean cb = new ConnBean("ip ", "username","password");
  ssh = SSHExec.getInstance(cb);      
  ssh.connect();
  CustomTask sampleTask = new ExecCommand("echo 123");
  ssh.exec(sampleTask);
  </code></pre>
  
  <p>(from <a href="https://code.google.com/p/sshxcute/" rel="nofollow">https://code.google.com/p/sshxcute/</a>)</p>
  
  <p>but what I want to do is something like</p>
  
  <pre><code>...
  ssh.connect();
  MyExecuteClass execClass = new MyExecuteClass();
  ssh.exec(execClass.runLongJob(a, b, c));
  MyReturn return = ssh.getReturnValue();
  </code></pre>
  
  <p>Is this possible? The objects <code>a,b,c</code> would have to be transmitted to the remote system, as well as all global variables, other classes, imports,... And the return value of the method must be returned somehow... If other objects are modified in the <code>runLongjob</code> method those changes have to be send back to the local PC as well.</p>
  
  <p>Is there a solution for this?</p>