Reputation: 2525
I am trying to connect to a remote machine using JSch. All my systems are running on ubuntu including my production machines. I am able to successfully connect and run the jar file. But when i close the window the program automatically shuts down. What i'm trying to achieve now is to run this jar file in the background.
I have appended the sudo command with '&' and the server doesn't even start in this case. I have tried using a shell channel but here to the server doesn't startup or closes on stopping my program.
I've also tried the nohup and disown commands to run in the background. When i get the process state using ps ux the STAT column shows T which according to this means the process has stopped. I've been stuck here since 3 days.
i am using the following version of JSch:
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.53</version>
</dependency>
I am using the following code:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
public class App extends Application {
@Override
public void start(Stage primaryStage) {
Button btn = new Button();
btn.setText("click");
btn.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent arg0) {
try {
JSch objJSch = new JSch();
Session objSession;
objSession = objJSch.getSession("userName", "host");
objSession.setPassword("password");
objSession.setConfig("StrictHostKeyChecking", "no");
System.out.println("Establishing Connection...");
objSession.connect();
System.out.println("Connection established.");
Channel objChannel = objSession.openChannel("exec");
((ChannelExec) objChannel).setCommand("cd {{path to my jar file}};sudo java -jar start.jar");
((ChannelExec) objChannel).setErrStream(System.err);
((ChannelExec) objChannel).setPty(true);//why should i use this??
InputStream in = objChannel.getInputStream();
OutputStream out = objChannel.getOutputStream();
objChannel.connect();
out.write(("password" + "\n").getBytes());
out.flush();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String s;
while ((s = br.readLine()) != null) {
System.out.println(s);
}
while (!objChannel.isClosed()) {
System.out.println("Waiting to close channel");
}
System.out.println("disconnecting...");
objChannel.disconnect();
objSession.disconnect();
System.out.println("disconnected.");
} catch (JSchException e) {
if (e.getMessage().equalsIgnoreCase("Auth fail"))
System.out.println("Authorization failed...");
else
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Upvotes: 3
Views: 1919
Reputation: 52
Your Code
((ChannelExec) objChannel).setCommand("cd {{path to my jar file}};sudo java -jar start.jar");
Modify it to :
((ChannelExec) objChannel).setCommand("cd {{path to my jar file}};sudo java -jar start.jar > /dev/null 2>&1 &");
Which will resolve your issue.
Upvotes: 3