달력

3

« 2024/3 »

  • 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

'Runtime'에 해당되는 글 1

  1. 2013.04.26 JDK 1.7.21에서 Runtime.exec의 변경사항
2013. 4. 26. 00:38

JDK 1.7.21에서 Runtime.exec의 변경사항 Java2013. 4. 26. 00:38

정말.. 오랜만에 포스팅이네요.. 1년 하고도 몇 달만에...
일상 포스팅이야 몰라도 개발쪽 포스팅은 아무래도 준비해야 할 것도 많고 해서 좀 부담스러워 자주 못 하게 됩니다.


여튼.. 이번 건은 최근에 자주 버전업이 되고 있는 자바와 관련한 건인데요..


최근 오라클로 넘어간 다음에 자바에 대해서 이러쿵 저러쿵 말이 많은데...
많이들 사용하다 보니 해커도 꼬이고 보안 구멍은 자꾸 늘어나는데다가 오라클이 하는 짓꺼리 때문에 별로 호응도 못 받고...
(James Gosling 아저씨도 떠났더랬죠..)


그때문이기도 하겠지만 예전처럼 버전이(물론 소숫점 아래 둘째 자리 혹은 _ 아래의 fix 버전 얘기지만) 하나씩 올라가는게 아니라 한번에 몇 개씩 올라가기도 합니다.(정확히 올라가는 기준은 잘 모르겠습니다만)
그러다가 이번에는 최신버전인 1.7.x 버전이 1.7.0_17에서 1.7.0_21로 네 단계올라갔는데요..
지금까지는 그냥 그러려니 했는데 이번 버전업을 한 뒤에 기존 프로그램에 오류가 발생한 사건이 생겨서 혹시나 저처럼 삽질하실 분들이 생길까봐 정리합니다.


기본적으로 업데이트 상세내역은 Update Release Notes 페이지에 잘 나와있습니다.
버전의 특성상 대부분 버그 또는 보안 패치인데요..


그 중에서 이번에 저한테 영향을 줬던 부분은 다음 부분입니다.
Changes to Runtime.exec


링크 가보셔도 됩니다만 아래와 같은 내용이 적혀있습니다.


On Windows platform, the decoding of command strings specified to Runtime.exec(String),Runtime.exec(String,String[]) and Runtime.exec(String,String[],File) methods, has been improved to follow the specification more closely. This may cause problems for applications that are using one or more of these methods with commands that contain spaces in the program name, or are invoking these methods with commands that are not quoted correctly.

For example, Runtime.getRuntime().exec("C:\\My Programs\\foo.exe bar") is an attempt to launch the program "C:\\My" with the arguments "Programs\\foo.exe" and "bar". This command is likely to fail with an exception to indicate "C:\My" cannot be found.

The example Runtime.getRuntime().exec("\"C:\\My Programs\\foo.exe\" bar") is an attempt to launch the program "\"C:\\My". This command will fail with an exception to indicate the program has an embedded quote.

Applications that need to launch programs with spaces in the program name should consider using the variants of Runtime.exec that allow the command and arguments to be specified in an array.

Alternatively, the preferred way to create operating systems processes since JDK 5.0 is usingjava.lang.ProcessBuilder. The ProcessBuilder class has a much more complete API for setting the environment, working directory and redirecting streams for the process.


울렁증 있으신 분들을 위해 간략하게 적어보면 Windows 플랫폼의 경우 자바의 Runtime.exec를 통해 다른 프로세스를 호출하여 실행할 경우 규격에 맞게 좀 더 까탈스러워졌다는 말입니다.
주로 경로(또는 파일명)에 공백이 있을 경우(대표적으로 Program Files와 같은 곳이 되겠죠) 발생할 수 있는 보안 이슈를 대비해 바꿨다고 합니다.


예를 들어 Runtime.getRuntime().exec("C:\\My Programs\\foo.exe bar") 라는 문장이 있었다고 보면
예전같으면 C:\My Programs\foo.exe를 bar라는 인자를 주고 실행하는 정상적인 코드였지만
이제는 C:\My 라는 프로그램에 Programs\foo.exe와 bar라는 두 개의 인자를 주고 실행하는 명령으로 인식을 한다고 합니다.
(즉, 무조건 첫번째 공백 다음에는 인자라는 얘기겠죠..)


앞으로는 Runtime.exec 의 다른 계열 중 실행파일명과 인자를 별도로 입력받는 함수들을 사용하라고 합니다.
아니면 JDK 1.5 버전 이상이면 더욱 더 많은 기능을 제공하는 ProcessBuilder 클래스를 사용하라고 하네요.


스펙에 맞도록 바꿨다는 말인걸 보니 스펙문서를 좀 더 자세히 봐야 하겠습니다만 여튼 위와 같은 방식으로 호출하는 경우가 있으면 이번 업그레이드 후 오류납니다..


저같은 경우는 아래와 같이 사용을 했었습니다.
Process proc = Runtime.getRuntime().exec("C:/Program Files/My/SysinternalsSuite/pslist.exe")


저는 일단 두 번째 방법을 이용해서 ProcessBuilder로 실행방식만 아래와 같이 바꾸니 실행이 가능했습니다.
Process proc = new ProcessBuilder("C:/Program Files/My/SysinternalsSuite/pslist.exe").start()

참고하시기 바랍니다.

'Java' 카테고리의 다른 글

Java에서 SHA 기반 해시암호화  (1) 2015.04.29
"\ub4f1" 와 같은 unicode 문자열 읽어오기  (0) 2014.01.28
:
Posted by hanavy