자바에서 Process 와 Runtime.getRuntime().exec()를 이용한 외부 프로그램 실행시..
특정디렉토리에서 수행되도록 하는 경우.
주의할 점은 명령실행 후 출력되는 내용이 getInputStream()으로 대부분 나오지만,
간혹 getErrorStream() 으로 내보내지는 경우도 있으니, 둘다 확인해봐야 한다.
public class Test {
public static void main( String[] args ) {
try {
Process child = Runtime.getRuntime().exec( "java -version", null, new File( "D:\\Java\\jdk1.6.0_16\\bin\\" ) );
System.out.println( readAll( child ) );
child.waitFor();
System.out.println( child.exitValue() );
}
catch ( Exception e ) {
e.printStackTrace();
}
}
private static String readAll( Process child ) {
StringBuffer sb = new StringBuffer();
sb.append( read( child.getInputStream() ) ).append( "\n" );
sb.append( read( child.getErrorStream() ) );
/**
* (필수) OutputStream 을 닫는다.
* waitFor() 무한대기 방지를 위해
*/
try {
child.getOutputStream().close();
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString().trim();
}
private static String read( InputStream in ) {
StringBuffer sb = new StringBuffer();
BufferedReader br = null;
try {
String line = null;
br = new BufferedReader( new InputStreamReader( in ));
while ( ( line = br.readLine() ) != null ) {
sb.append( line ).append( "\n" );
}
}
catch (IOException e) {
e.printStackTrace();
}
finally {
/**
* (필수) InputStream 을 닫는다.
* waitFor() 무한대기 방지를 위해
*/
try {
if ( br != null ) br.close ( );
} catch (IOException e) {
e.printStackTrace();
}
}
// 문자열의 앞/뒤 공백을 제거하고 담는다.
return Util.emptyString( sb.toString() ).trim();
}
}
2012년 9월 12일 수요일
2012년 2월 14일 화요일
JAVA CLOSE_WAIT 관련 내용
JAVA CLOSE_WAIT 이슈는 자바의 버그에서 비롯된 것으로 판단됨.
관련 버그 내용.
bug: 6215050 java classes_nio (so) SocketChannel created in CLOSE_WAIT and never cleaned up.. File Descriptor leak
http://www.oracle.com/technetwork/java/javase/releasenotes-142123.html
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6215050
이와 관련한 내용 및 문제제기 관련 사이트 (해결방법은 아니므로 참고만 하세요.)
http://stackoverflow.com/questions/8326058/how-to-kill-tcp-connection-with-close-wait-state
https://forums.oracle.com/forums/thread.jspa?threadID=1160014
http://tux.hk/index.php?m=05&y=09&entry=entry090521-111844
http://www.thatisjava.com/java-core-apis/35975/
2011년 5월 18일 수요일
[Java] MacAddress 갖고 오기 (JDK 6.0)
http://suein1209.tistory.com/373?srchid=BR1http%3A%2F%2Fsuein1209.tistory.com%2F373 MacAddress 갖고 오기 (JDK 6.0) 자바철학/자바 2009/09/01 17:56 자바 에서 맥 주소를 갖고 와야 할때가 있다... ㅋㅋ 그때 사용 하기~ 이건 JDK 6.0 부터 추가된 사항이다. import java.net.NetworkInterface; import java.net.SocketException; import java.util.Enumeration; public class NetworkInterfaceTest { public static void main(String[] args) throws SocketException { Enumeration<NetworkInterface> nienum = NetworkInterface.getNetworkInterfaces(); while (nienum.hasMoreElements()) { NetworkInterface ni = nienum.nextElement(); System.out.print(ni.getName()); System.out.print(" : "); byte[] hardwareAddress = ni.getHardwareAddress(); String div = ""; if (hardwareAddress != null) { for (byte b : hardwareAddress) { System.out.print(div); System.out.format("%02X", b); div = "-"; } } System.out.println(); } } } |
라벨:
맥어드레스,
자바,
Java,
JDK6.0,
MacAddress
[JAVA] 대소문자 관계없이 검색어 찾아 강조문구로 바꾸기..
| [JAVA] 대소문자 관계없이 검색어 찾아 강조문구로 바꾸기.. // 검색시 대소문자 관계없이 강조문구로 바꾸기... public String convertWords(String oriString, String findword) { // replaceAll ==> 버그 있음.. 검색어 [강풀] ==> 결과 : [강풀][강풀] 일케 두번 나온다.. StringBuffer newString = new StringBuffer(oriString); StringBuffer tmpText = new StringBuffer(oriString.toUpperCase()); // 임시로 사용할 원본의 대문자.. String newText = oriString.toUpperCase(); // 원문의 복사본을 대문자로 바꾸기.. findword = findword.toUpperCase(); // 검색어도 대문자로.. newText = newText.replaceAll(findword, "<font color=red>" + findword + "</font>"); // 일단 복사본을 원하는 강조문구로 바꾸어 놓는다.. int leng = newText.length() - 1; for (int i = 0; i < leng; i ++ ) { char chr = newText.charAt(i); try { if (chr != tmpText.charAt(i)) // 강조문구 적용부분을 찾는다... { tmpText.insert(i, chr); // 길이를 맞추기위해 여기에도 삽입한다. newString.insert(i, chr); // 원문에 강조문구를 삽입한다.. } } catch (Exception ee) // 맨뒤쪽에 강조문구가 들어가는경우 길이차이로 String index out of range 발생.. { tmpText.append(chr); newString.append(chr); } } return newString.toString(); } ===> 제목, 내용에 적용하면 좋을 듯...냐하하.. |
[JAVA] RandomAccessFile 을 사용한 파일 엑세스
/* * 파일에 기록하기 세번째 * 파일 전체를 다시쓰지 않고 덧붙이는 경우에 사용한다. */ public void setEvent3(String filename, String str) { try { RandomAccessFile rFile = new RandomAccessFile(filename,"rw"); //r, w, rw rFile.seek(rFile.length()); rFile.write(str.getBytes()); rFile.close(); } catch (IOException e) { e.printStackTrace(); } } 랜덤엑세스파일을 사용하면 원하는 부분에 추가/삭제를 할 수 있다..!!! |
피드 구독하기:
글 (Atom)