0

I know that this subject have been asked many several times here. but i didn't find any clearly answer.

I want to execute linux shell on linux commands from windows using java.

I have two options:

  1. To execute the commands remotely.

  2. To create a service on the linux and call him from windows.

which one is prefered and what is the best way to implement it?

KfirZuberi
  • 51
  • 7

2 Answers2

0

There is no clear answer and there is a reason for it. The answer depends on the application's requirements. You have two possible solutions which have their pros and cons.

  • Creating a service means that you follow a Service Oriented Architecture. This would be a good choice if your service is going to be used by many clients and you want it to be client agnostic. This is an overkill if you have only one specific client.

  • If you have one specific client which is written to communicate with the specific server you could always use the direct ssh solution. Perhaps a library like Java Secure Channel would the most straight forward solution.

  • A third option would be to use something like RMI (Remote Method Invocation). This is a more complicated solution and I would not suggest it because in my opinion it has no pros.

Hope I helped!

Pantelis Natsiavas
  • 5,293
  • 5
  • 21
  • 36
0

you can use jcraft api.

below is syntax:

Session session = jsch.getSession("userName", "serverName", 22);
session.setPassword("password");
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
ChannelExec channelExec = (ChannelExec) session.openChannel("exec");
InputStream in = channelExec.getInputStream();
channelExec.setCommand("uptime");
channelExec.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String tmp;
while ((tmp= reader.readLine()) != null) {
System.out.println(tmp);
}
PrkInto
  • 63
  • 5