java.lang 패키지의 Runtime 클래스는 자바에서 외부 프로세스를 생성하는 방법을 제공한다.
예를 들어 자바를 이용하여 MS 윈도우에서 MS 엑셀을 실행하는것등에 이 Runtime클래스를 이용할 수 있다.
예를 들어 C:\Program Files\Microsoft Office\Office\excel.exe 위치에 있는 excel.exe를 실행
하는 코드는 아래와 같다.
[code]
try {
Runtime run = Runtime.getRuntime ();
run.exec ( "C:\\Program Files\\Microsoft Office\\Office\\excel.exe" );
} catch ( IOException ie )
{
System.err.println ( ie );
}
[/code]
어떤가, 무척 간단하지 않는가?
그럼, 한걸음 더 나아가 보자.
윈도우는 파일확장자별로 대표 프로그램을 등록하여 파일을 더블클릭하는것만으로 해당 프로그램을 실행 할 수가
있는데 이렇게 특정 파일을 선택하여 그와 연결된 프로그램을 실행하는 코드도 자바로 가능 할까?
물론, 가능하다. 여기에는 윈도우즈 명령어를 조금 알아야 하는 부분이 있지만 그 부분만 알고 있다면 나머지는
위와 동일하게 Runtime 객체로 그 명령어를 실행 하기만 하면된다.
아래 코드를 보자.
[code]
package javacodesnipet;
import java.io.File;
import java.io.IOException;
/**
* Runtime클래스를 이용하여 해당 파일과 연결된 프로그램을 실행한다.
* @author 신윤섭
*/
public class RuntimeExample {
public static void main(String[] args){
try {
File file = new File("d:\\sample.pdf");
//MS Windows Only
Process p= Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " +
file.getAbsolutePath());
// or
//Process p= Runtime.getRuntime().exec("rundll32 SHELL32.DLL,ShellExec_RunDLL " +
// file.getAbsolutePath());
//Apple Mac Only
//Process p= Runtime.getRuntime().exec("open " + file.getAbsolutePath());
p.waitFor();
System.out.println("Process Done");
} catch (InterruptedException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
[/code]
위 코드는 윈도우 탐색기에서 d:\sample.pdf를 더블클릭하여 아크로뱃리더를 실행하고 해당 pdf문서를
여는것과 동일한 효과를 얻을 수 있다. 윈도우의 rundll32 명령으로 해당 파일과 연결된 프로그램을 구동
할 수 있는데 이를 Runtime객체를 이용하여 수행하는 코드이다.
rundll32 more..
█ rundll32.exe shell32.dll,Control_RunDLL
Control Panel
█ rundll32.exe shell32.dll,Control_RunDLL access.cpl,,1
Accessability Properties (Keyboard)
█ rundll32.exe shell32.dll,Control_RunDLL access.cpl,,2
Accessability Properties (Sound)
█ rundll32.exe shell32.dll,Control_RunDLL access.cpl,,3
Accessability Properties (Display)
█ rundll32.exe shell32.dll,Control_RunDLL access.cpl,,4
Accessability Properties (Mouse)
█ rundll32.exe shell32.dll,Control_RunDLL access.cpl,,5
Accessability Properties (General)
█ rundll32.exe shell32.dll,Control_RunDLL appwiz.cpl,,1
Add/Remove Programs Properties (Install/Uninstall)
█ rundll32.exe shell32.dll,Control_RunDLL appwiz.cpl,,2
Add/Remove Programs Properties (Windows Setup)
█ rundll32.exe shell32.dll,Control_RunDLL appwiz.cpl,,3
Add/Remove Programs Properties (Startup Disk)
█ rundll32.exe shell32.dll,Control_RunDLL desk.cpl,,0
Display Properties (Background)
█ rundll32.exe shell32.dll,Control_RunDLL desk.cpl,,1
Display Properties (Screen Saver)
█ rundll32.exe shell32.dll,Control_RunDLL desk.cpl,,2
Display Properties (Appearance)
█ rundll32.exe shell32.dll,Control_RunDLL desk.cpl,,3
Display Properties (Settings)
█ rundll32.exe shell32.dll,Control_RunDLL intl.cpl,,0
Regional Settings Properties (Regional Settings)
█ rundll32.exe shell32.dll,Control_RunDLL intl.cpl,,1
Regional Settings Properties (Number)
█ rundll32.exe shell32.dll,Control_RunDLL intl.cpl,,2
Regional Settings Properties (Currency)
█ rundll32.exe shell32.dll,Control_RunDLL intl.cpl,,3
Regional Settings Properties (Time)
█ rundll32.exe shell32.dll,Control_RunDLL intl.cpl,,4
Regional Settings Properties (Date)
█ rundll32.exe shell32.dll,Control_RunDLL joy.cpl
Joystick Properties (Joystick)
█ rundll32.exe shell32.dll,Control_RunDLL main.cpl @0
Mouse Properties
█ rundll32.exe shell32.dll,Control_RunDLL main.cpl @1
Keyboard Properties
█ rundll32.exe shell32.dll,Control_RunDLL main.cpl @2
Printers
█ rundll32.exe shell32.dll,SHHelpShortcuts_RunDLL AddPrinter
Add Printer Wizard
█ rundll32.exe shell32.dll,Control_RunDLL main.cpl @3
Fonts
█ rundll32.exe shell32.dll,Control_RunDLL mlcfg32.cpl
Microsoft Exchange Profiles (General). Mail and Fax Options
█ rundll32.exe shell32.dll,Control_RunDLL mmsys.cpl,,0
Multimedia Properties (Audio)
█ rundll32.exe shell32.dll,Control_RunDLL mmsys.cpl,,1
Multimedia Properties (Video)
█ rundll32.exe shell32.dll,Control_RunDLL mmsys.cpl,,2
Multimedia Properties (MIDI)
█ rundll32.exe shell32.dll,Control_RunDLL mmsys.cpl,,3
Multimedia Properties (CD Music)
█ rundll32.exe shell32.dll,Control_RunDLL mmsys.cpl,,4
Multimedia Properties (Advanced)
█ rundll32.exe shell32.dll,Control_RunDLL mmsys.cpl @1
Sounds Properties
█ rundll32.exe shell32.dll,Control_RunDLL modem.cpl
Modem Properties (General)
█ rundll32.exe shell32.dll,Control_RunDLL netcpl.cpl
Network (Configuration)
█ rundll32.exe shell32.dll,Control_RunDLL password.cpl
Password Properties (Change Passwords)
█ rundll32.exe shell32.dll,Control_RunDLL sysdm.cpl,,0
System Properties (General)
█ rundll32.exe shell32.dll,Control_RunDLL sysdm.cpl,,1
System Properties (Device Manager)
█ rundll32.exe shell32.dll,Control_RunDLL sysdm.cpl,,2
System Properties (Hardware Profiles)
█ rundll32.exe shell32.dll,Control_RunDLL sysdm.cpl,,3
System Properties (Performance)
█ rundll32.exe shell32.dll,Control_RunDLL sysdm.cpl @1
Add New Hardware Wizard
█ rundll32.exe shell32.dll,Control_RunDLL timedate.cpl
Date/Time Properties
█ rundll32.exe shell32.dll,Control_RunDLL timedate.cpl,@0,2
Open third tab (first tab is 0)
█ rundll32.exe shell32.dll,Control_RunDLL wgpocpl.cpl
Microsoft Workgroup Postoffice Admin
█ rundll32.exe shell32.dll,OpenAs_RunDLL "d:\path\filname.ext"
Open With (File Associations)
█ rundll32.exe shell32.dll,SHHelpShortcuts_RunDLL PrintersFolder
View Printers
█ rundll32.exe shell32.dll,SHHelpShortcuts_RunDLL FontsFolder
View Fonts
█ rundll32.exe shell32.dll,Control_RunDLL timedate.cpl,,/f
Pick a Time Zone Dialog
█ rundll32.exe shell32.dll,ShellAboutA Info-Box
Open "About Window Window"
█ rundll32.exe diskcopy.dll,DiskCopyRunDll
Run Diskcopy Dialog
█ rundll32.exe AppWiz.Cpl,NewLinkHere %1
Create new shortcut in the location specified by %1
█ rundll32.exe sysdm.cpl,InstallDevice_RunDLL
Install New Hardware Wizard
█ rundll32.exe rnaui.dll,RnaWizard
Dial-up Networking Wizard
█ rundll32.exe rnaui.dll,RnaDial "MyConnect"
Run "Net Connection" Dialog
█ rundll32.exe shscrap.dll,OpenScrap_RunDLL /r /x %1
Open a scrap document
█ rundll32.exe syncui.dll,Briefcase_Create
Create a Briefcase
█ rundll32.exe mouse.dll,disable
Disable Mouse
█ rundll32.exe keyboard.dll,disable
Lock The Keyboard
█ rundll32.exe user.dll,cascadechildwindows
Cascade All Windows
█ rundll32.exe user.dll,tilechildwindows
Minimize All Child-Windows
█ rundll32.exe user.dll,repaintscreen
Refresh Desktop
█ rundll32.exe user.dll,swapmousebutton
Swap Mouse Buttons
█ rundll32.exe user.dll,setcursorpos
Set Cursor Position To (0,0)
█ rundll32.exe user.dll,wnetconnectdialog
Show "Map Network Drive" Window
█ rundll32.exe user.dll,wnetdisconnectdialog
Show "Disconnect Network Disk" Window
█ rundll32.exe user.dll,disableoemlayer
Display The BSOD (blue screen of death)Window
█ rundll32.exe user.dll,setdoubleclicktime
Set New DblClick Speed (Rate)
█ rundll32.exe user.dll,MessageBeep
Default beep sound
█ rundll32.exe user.dll,setcaretblinktime
Set New Cursor Rate Speed
█ rundll32.exe shell32.dll,SHFormatDrive
Run "Format Disk (A)" Window
█ rundll32.exe shell32.dll,SHExitWindowsEx -1
Cold Restart Of Windows Explorer
█ rundll32.exe shell32.dll,SHExitWindowsEx 1
Shut Down Computer
█ rundll32.exe shell32.dll,SHExitWindowsEx 0
Logoff Current User
█ rundll32.exe shell32.dll,SHExitWindowsEx 2
Windows9x Quick Reboot
█ rundll32.exe shell32.dll.dll,Control_RunDLL appwiz.cpl
Add/remove programs
█ rundll32.exe shell32.dll.dll,Control_RunDLL timedate.cpl,,0
Date/time settings
█ rundll32.exe shell32.dll.dll,Control_RunDLL odbccp32.cpl
ODBC settings
█ rundll32.exe krnl386.exe,exitkernel
Force Windows 9x To Exit (no confirmation)
█ rundll32.exe msprint2.dll,RUNDLL_PrintTestPage
Choose & Print Test Page Of Current Printer
█ rundll32.exe user.exe,#7
To shut Windows down using the Win32 API
█ rundll32.exe user.exe,exitwindows
Polite power off
█ rundll32.exe user32.dll,ExitWindowsEx
Forced immediate logoff
█ rundll32.exe rnaui.dll,RnaRunImport
Open DUN (dial up networking exported file)
█ rundll32.exe rnaui.dll,RnaDial %1
Start a dialup connection by name
█ rundll32.exe msconf.dll,OpenConfLink %l
NetMeeting Speeddial CNF
█ rundll32.exe msconf.dll,NewMediaPhone %l
H.323 -or- Intel IPhone Internet telephony
█ rundll32.exe msconf.dll,CallToProtocolHandler %l
URL Callto
█ rundll32.exe shdocvw.dll,OpenURL %l
URL
█ rundll32.exe url.dll,TelnetProtocolHandler %l
URL Rlogin / Telnet / TN3270
█ rundll32.exe url.dll,FileProtocolHandler %l
URL File
█ rundll32.exe mailnews.dll,Mail_RunDLL
URL Mailto
█ rundll32.exe mailnews.dll,EMLFileHandler
Email rfc822 EML
█ rundll32.exe mailnews.dll,News_RunDLL
News rfc822 NWS
█ rundll32.exe advpack.dll,LaunchINFSection %1,DefaultInstall
INF active install
█ rundll32.exe syncui.dll,Briefcase_Create %1!d! %2
New briefcase
█ rundll32.exe AppWiz.Cpl,NewLinkHere %1
New LNK
█ rundll32.exe cdfview.dll,OpenChannel %L
Open channel file
█ rundll32.exe cdfview.dll,Subscribe %L
Subscribe to channel
█ rundll32.exe mshtml.dll,PrintHTML "%1"
Print HTML (use "%1" or "%1" "%2" "%3" "%4")
█ rundll32.exe amovie.ocx,RunDll /open %1
OPEN aif, auf, avi, midi, mov, mpeg, sound
█ rundll32.exe amovie.ocx,RunDll /play /close %1
PLAY aif, auf, avi, midi, mov, mpeg, sound
█ rundll32.exe shell32.dll,OpenAs_RunDLL %1
Open unknown file
█ rundll32.exe desk.cpl,InstallScreenSaver %l
Install screensaver
[This message has been edited by Patrice Terrier (edited September 29, 2005).]
정말 정말 감사합니다.