Java SE 7ã®ProcessBuilderã§ãªãã¤ã¬ã¯ã
Java SE 7ããJavaããå¤é¨ã³ãã³ããå®è¡ã§ããProcessBuilderã«ä¾¿å©ãªæ©è½ã追å ããã¦ãããpingãå®è¡ãã¦ãã¡ã¤ã«ã«ãªãã¤ã¬ã¯ããç°¡åã«ã§ãããªã©ãæ´¾æã§ã¯ãªããå¬ããæ©è½è¿½å ã§ããã以ä¸ã§ã¯ã追å ãããæ©è½ã使ã£ã¦è²ã ãªpingãå®è¡ãã¦ã¿ãã
pingçµæããã¤ãçµç±ã®å ¥åã¹ããªã¼ã ããåå¾
ProcessBuilder pb = new ProcessBuilder("ping", "-c", "3", "127.0.0.1"); try { Process p = pb.start(); // ping ãå®äºããã®ãå¾ ã¤ p.waitFor(); // å®è¡çµæãåå¾ããã¹ããªã¼ã ã®ç¨®å¥ãåºå System.out.println(pb.redirectInput()); try (BufferedReader br = new BufferedReader( new InputStreamReader(p.getInputStream()))) { // pingçµæã®åºå for(String line = br.readLine(); line != null; line = br.readLine()) { System.out.println(line); } } } catch (IOException | InterruptedException e) { // ä¾å¤ãã³ããªã³ã°å¦ç }
Mac OS X 10.7.5 ã§è©¦ãã¨ã以ä¸ã®ããã«åºåãããã
PIPE PING 127.0.0.1 (127.0.0.1): 56 data bytes 64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.068 ms 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.109 ms 64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.175 ms --- 127.0.0.1 ping statistics --- 3 packets transmitted, 3 packets received, 0.0% packet loss round-trip min/avg/max/stddev = 0.068/0.117/0.175/0.044 ms
ä¸çªæåã«PIPEã¨è¡¨ç¤ºããããããã¯ProcessBuilderã®ããã©ã«ãã®æåã§ãProcessBuilderã®å®è¡ã«ãã£ã¦çæãããåããã»ã¹ã¯ãçµæããã¤ãã«æ¸ãè¾¼ãããã親ããã»ã¹ã®Javaãããã¤ãã«å¯¾ããå ¥åã¹ããªã¼ã çµç±ã§çµæãåå¾ã§ããã
pingçµæããã¡ã¤ã«ã«æ¸ãè¾¼ã
ãã¤ã¦ã¯ã>ãã使ã£ã¦ã³ãã³ãå®è¡çµæããã¡ã¤ã«ã«æ¸ãè¾¼ããã¨ãã¦ããããã¨ãå¤ããã£ãããJava SE 7ã§ã¯ãã¡ã¤ã«ã«ãªãã¤ã¬ã¯ãããããã®APIãæ´åããã¦ããã
ProcessBuilder pb = new ProcessBuilder("ping", "-c", "3", "127.0.0.1"); File log = new File("ping.log"); // æ¨æºã¨ã©ã¼ãæ¨æºåºåã«ãã¼ã¸ãã pb.redirectErrorStream(true); // åºåã®ãªãã¤ã¬ã¯ãå ã«ãã¡ã¤ã«ãæå® (ä¸æ¸ã) pb.redirectOutput(log); // ãã¡ã¤ã«ã«è¿½è¨ããå ´å // pb.redirectOutput(Redirect.appendTo(log)); try { Process p = pb.start(); // ping ãå®äºããã®ãå¾ ã¤ p.waitFor(); } catch (IOException | InterruptedException e) { // ä¾å¤ãã³ããªã³ã°å¦ç }
ä¸è¨ã®ã³ã¼ãä¾ã§ã¯ãpingã®åºåçµæãjavaèµ·åãã£ã¬ã¯ããªã®ping.logã«æ¸ãåºãã¦ãããProcessBuilder.redirectErrorStream(true)ãè¨å®ãããã¨ã«ãã£ã¦ãæ¨æºåºåã¨æ¨æºã¨ã©ã¼åºåããã¼ã¸ã§ããã®ã§ãã©ã¡ããåä¸ã®ãã¡ã¤ã«ã«æ¸ãåºãããå ´åã«ä¾¿å©ãã¾ããã³ã¡ã³ãã¢ã¦ãããã¦ããé¨åã®ããã«ãProcessBuilder.Redirect.appentTo(File file)ã«ãã£ã¦ãã¡ã¤ã«ãè¨å®ããã¨ä¸æ¸ãã§ã¯ãªãã>>ãã®ããã«è¿½è¨ãããã¨ãã§ããã
pingçµæãJavaã®æ¨æºåºåã«è¡¨ç¤ºãã
ãªãã¤ã¬ã¯ã種å¥ãINHERITã«è¨å®ãããã¨ã«ããã親ããã»ã¹(java)ã®æ¨æºåºå/ã¨ã©ã¼åºåãå¼ãç¶ããã¨ã§ãpingå®è¡çµæãjavaã®æ¨æºåºåã«æ¸ãè¾¼ããã¨ãã§ããã
ProcessBuilder pb = new ProcessBuilder("ping", "-c", "3", "127.0.0.1"); // åããã»ã¹ã®æ¨æºå ¥åºåã»æ¨æºã¨ã©ã¼åºåãjavaã¨åããã®ã«è¨å®ãã pb.inheritIO(); try { Process p = pb.start(); p.waitFor(); System.out.println(pb.redirectInput()); } catch (IOException | InterruptedException e) { // ä¾å¤ãã³ããªã³ã°å¦ç }
ProcessBuilder.inheritIO()ã®å®è¡ãããã¨ã§ãæ¸ãè¾¼ã¿å ã¹ããªã¼ã ã¯è¦ªããã»ã¹ã§ããJavaã¨åãã¹ããªã¼ã ã«ãªããçµæã¯ä»¥ä¸ã®ããã«è¡¨ç¤ºãããã
PING 127.0.0.1 (127.0.0.1): 56 data bytes 64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.055 ms 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.049 ms 64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.045 ms --- 127.0.0.1 ping statistics --- 3 packets transmitted, 3 packets received, 0.0% packet loss round-trip min/avg/max/stddev = 0.045/0.050/0.055/0.004 ms INHERIT
PIPEã表示ãããæåã®ä¾ã¨ç°ãªããpingçµæã®å¾ã«INHERITã表示ããã¦ãããããã¯p.waitFor()ã§pingçµäºãå¾ ã¤éã«ãjavaã¨åãæ¨æºåºåã«pingã®å®è¡çµæãåºåããããã¨ã示ããProcessBuilder.redirectInput()ããè¿ãããINHERITã¯ã¾ããåããã»ã¹ã®æ¨æºå ¥åºåã¹ããªã¼ã ã親ããå¼ãç¶ããã¦ããç¶æ ã§ãããã¨ã示ãã
ã¾ã¨ã
- çµæãã¹ããªã¼ã çµç±ã§åå¾ãããå ´åã¯ãProcessBuilder.getInputStream()
- çµæããã¡ã¤ã«ã«æ¸ãè¾¼ã¿ããå ´åã¯ãProcessBuilder.redirectOutput(File file)
- çµæãjavaã®æ¨æºåºåã«è¡¨ç¤ºãããå ´åã¯ãProcessBuilder.inheritIO()
詳細ã«ã¤ãã¦ã¯ãjava.lang.ProcessBuilderã¯ã©ã¹ã®JavaDocãåèã«ãã¦ãã ããã