Hmm…en Java, Process ne ferme pas les canaux d’entrée/sortie
Je reprends un post publié en 2005 sur mon blog anglophone, concernant la création de Process externes en Java.
Le petit problème, documenté à peu près nulle part à mon avis, est que l’objet Process ne ferme pas les canaux d’entrée/sortie standard automatiquement, ça peut causer des effets assez rigolos. Enfin, rigolos sauf si on doit dépanner ce genre de trucs, bien sûr.
/** Demonstrate that java Process objects eat file handles
* if their stdin, stderr and stdout channels are not explicitely
* closed.
*
* To see this, run the class until it says "done executing" and
* do an lsof on the java process (should show 60 pipe channels).
* Then uncomment the three close() calls and rerun the test.
*/
public class Exec {
public static void main(String[] args) throws Exception {
int NMAX=20;
final String command="bash -c exit";
System.err.println("Executing " + command + ""
+ NMAX + " times....");
for(int i=0; i<NMAX; i++) {
final Process p = Runtime.getRuntime().exec(command);
p.waitFor();
// These three calls are required to avoid eating
// file handles (at least under macosx and linux)
// p.getInputStream().close();
// p.getOutputStream().close();
// p.getErrorStream().close();
if(i % 10 == 0) System.err.print(".");
}
System.err.println("Done executing - press ENTER to exit");
System.in.read();
System.err.println("Done.");
}
}
Un bon candidat pour quelledaube, non?