런 타임 exec()셸 스 크 립 트 호출

http://blog.csdn.net/moreorless/article/details/4182883
때때로,우 리 는 자바 프로그램 에서 외부 프로그램 을 호출 해 야 합 니 다.우 리 는 Runtime.exec()호출 을 통 해 완성 할 수 있 습 니 다.
 
 
The class java.lang.Runtime features a static method called getRuntime() , which retrieves the current Java Runtime Environment. That is the only way to obtain a reference to the Runtime object. With that reference, you can run external programs by invoking the Runtime class's exec() method. Developers often call this method to launch a browser for displaying a help page in HTML.
 
exec()는 네 개의 리 셋 버 전이 있 습 니 다.
There are four overloaded versions of the exec() command:
  • public Process exec(String command);
  • public Process exec(String [] cmdArray);
  • public Process exec(String command, String [] envp);
  • public Process exec(String [] cmdArray, String [] envp);

  •  
    For each of these methods, a command -- and possibly a set of arguments -- is passed to an operating-system-specific function call. This subsequently creates an operating-system-specific process (a running program) with a reference to a Process class returned to the Java VM. The Process class is an abstract class, because a specific subclass of Process exists for each operating system.
    You can pass three possible input parameters into these methods:
  • A single string that represents both the program to execute and any arguments to that program
  • An array of strings that separate the program from its arguments
  • An array of environment variables

  • Pass in the environment variables in the form name=value . If you use the version of exec() with a single string for both the program and its arguments, note that the string is parsed using white space as the delimiter via the StringTokenizer class.
     
    주의사항:
    1.  호출 된 외부 명령 에 리 셋(<,>),파이프(|)명령 이 포함 되 어 있 을 때 exec(String command)버 전 은 리 셋,파이프 조작 자 를 정확하게 해석 할 수 없습니다.그래서 exec(String[]cmdArray)를 사용 해 야 합 니 다.
       예 를 들 어,echo"hello world">/home/admin/newFile.txt
           ls -e | grep java
       다음 호출 방식 을 사용 해 야 합 니 다.
           String []cmdArray = new String[]{ "/bin/sh", "-c", "ls -e | grep java"};
                 Runtime.getRuntime().exec(cmdArray);
     
    2.  
        
  • waitFor()방법 을 호출 하기 전에 데이터 흐름 을 읽 어야 합 니 다
  • 4.567917.표준 오류 흐름 에서 먼저 읽 은 다음 에 표준 출력 흐름 을 읽 어야 합 니 다.  The next version of Savant is going to focus heavily on the stand-alone runtime and support for dialects and plugins. Supporting all that is largely handled by using a simple executor framework I wrote around Java 1.4 and lower’s Runtime.exec method. A few things to keep in mind when using this:
  • Always read from the streams prior to calling waitFor. Otherwise you could end up waiting forever on Windows and other OS platforms whose I/O buffers can’t store enough from standard out and standard error to ensure the program has finished. These platforms will pause the execution of whatever is running until something reads the buffered content from standard out and standard error. I would imagine all platforms suffer from this, but some platforms have larger buffers than others. Needless to say, always read from the streams first.
  • Always read from standard error first. I ran across a bug where some OS platforms will always open standard out, but never close it. What this means is that if you read from standard out first and the process only writes to standard error, you’ll hang forever waiting to read. If you read from standard error first, you’ll always be okay on these platforms because the OS seems to shutdown standard error. I think however, that the best way to handle all cases is to check both standard error and standard out for readiness and only read from them if they have something to offer. The downside I could see here is that error isn’t ready, but eventually will be.

  •  3.또 주의해 야 할 점 은:     호출 된 스 크 립 트 에 sudo 와 같은 tty 가 필요 한 명령 이 있 을 때 사용 합 니 다.           String []cmdArray = new String[]{ "/bin/sh", "-c", "yourscriptname"};  이러한 호출 방식 은 스 크 립 트 실행 을 위해 tty 환경 을 만 듭 니 다.그렇지 않 으 면 실행 과정 에서"sorry,you must have a tty to run xxx"의 오 류 를 알려 줍 니 다.    Because some native platforms only provide limited buffer size for standard input and output streams,failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block,and even deadlock.exec()후 즉시 waitFor()를 호출 하면 프로 세 스 가 끊 길 수 있 습 니 다.        When Runtime.exec()won't runtime 에 대한 주의사항http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html 2.자바 시스템 MAC 주소 가 져 오기http://www.javapipe.com/web/memory_and_the_java_runtime.exec_process.html 3.  http://pudding.sharera.com/blog/BlogTopic/31232.htm

    좋은 웹페이지 즐겨찾기