Bonjour,

Je suis entrain d'executer des commandes shell a partir de java. En fait, la seconde etape est de stocker les resultats (output) de chaque commande executee dans une variable (String) separemment.

Voici le morceau de code :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
58
59
60
61
 
                File wd = new File("/bin");
		System.out.println(wd);
		Process proc = null;
		StringBuilder builder = new StringBuilder();
 
 
		try {
			   proc = Runtime.getRuntime().exec("/bin/bash", null, wd);
			}
			catch (IOException e) {
			   e.printStackTrace();
			}
 
 
			if (proc != null) {
				   BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
				   PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(proc.getOutputStream())), true);
				    List<PrintWriter> outs= new ArrayList();
				    PrintWriter out2 = new PrintWriter(new BufferedWriter(new OutputStreamWriter(proc.getOutputStream())), true);
 
 
 
				   out.println("cd ..");
 
 
				   System.out.println("moving to the parent directory");
				   out.println("cd /var/");
				   out.println("ls");
 
				   out.println("cd ..");
				   out.println("cd /etc/");
				   out.println("ls -a");
				   out.println("ps");
				   outs.add(out);
				   out2.println("ls -la");
				   outs.add(out2);
				   out.println("exit");
				   List arrayLines = new ArrayList();  
				   try {
					      String line;
					      while ((line = in.readLine()) != null) {
					    	  arrayLines.add(line);  
					        builder.append(line).append("\n");
 
					      }
 
					      String commandOutput = builder.toString();
					      logger.debug("here is the command in string"+commandOutput);
 
					      System.out.println(arrayLines + "\n");  
 
					      proc.waitFor();
					      in.close();
					      out.close();
					      proc.destroy();
					   }
					   catch (Exception e) {
					      e.printStackTrace();
					   }
					}
Le resultat obtenu de toutes les commandes shell est dans une variable String commandOutput. Le but est de stocker pour chaque output d'une commande shell dans une variable String ...Merci