Blocage exécution shell via JSch
Bonjour,
J'ai un petit outil graphique qui me permet d’exécuter des commandes shell via JSch. Le plus souvent je ne rencontre aucun problème à l’exécution des commande shell or pour certaine commande, JSch se bloque et ne me rend pas la main.
Par exemple si je lui passe la commande oninit (pour démarrer une base informix) il se bloque. je passe cette même commande sur le serveur, je n'ai aucun problème, .
Voici la méthode(ci-dessous) qu j'utilise pour passer les commande shell, je récupère le flux d’écriture standard de sortie ainsi que le flux d'erreur pour l'afficher dans un objet de type JtextArea.
Merci pour votre aide.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
public int executeCommand(String command) {
int exitCode = -1;
ChannelExec channel = null;
BufferedReader stdoutReader = null;
BufferedReader stderrReader = null;
String line ;
String line_error="" ;
try {
channel = (ChannelExec) oSession.openChannel("exec");
channel.setCommand(command + "\n");
InputStream stdout = channel.getInputStream();
InputStream stderr = channel.getErrStream();
channel.connect();
stdoutReader = new BufferedReader(new InputStreamReader(stdout));
stderrReader = new BufferedReader(new InputStreamReader(stderr));
//while (stdoutReader.read() != -1) {
while ((line = stdoutReader.readLine()) != null ||(line_error = stderrReader.readLine()) != null) {
//System.out.println(stdoutReader.readLine());
//System.out.println(line) ;
if(line_error.length() > 0)
{
textarea.append(line_error+"\n") ;
line_error="";
}
textarea.append(line+"\n") ;
textarea.repaint() ;
}
//&& (line_error = stderrReader.readLine()) != null
exitCode = channel.getExitStatus();
return exitCode;
} catch (IOException e) {
System.out.println("Cannot execute remote command: " + command
+ " : " + e.getMessage());
} catch (Exception e) {
System.out.println("Cannot execute remote command: " + command
+ " : " + e.getMessage());
} finally {
try {
stdoutReader.close();
stderrReader.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
if (channel != null) {
channel.disconnect();
}
}
return exitCode;
} |