The example in this article describes how to execute the cmd command under Windows using Java. Share it with everyone for your reference.
The specific implementation code is as follows:
Copy the code code as follows://get runtime
Runtime rt = Runtime.getRuntime();
//Get the process
Process p = rt.exec(String[] cmdarray);//or Process p = rt.exec(String cmd);
//If p is not empty, then clear it
if(null!=p){destroy
p.destory();
p=null;
}
For example:
Copy the code as follows: Runtime rt = Runtime.getRuntime();
Process p = rt.exec("cmd.exe /c shutdown -a");
System.out.println(p.toString());
cmd /c dir closes the command window after executing the dir command.
cmd /k dir does not close the command window after executing the dir command.
cmd /c start dir will open a new window and then execute the dir command, and the original window will close.
cmd /k start dir will open a new window and then execute the dir command, but the original window will not be closed.
You can use cmd /? to view help information.
Replenish:
★CMD command★
1. gpedit.msc-----Group Policy
2. sndrec32-------recorder
3. Nslookup-------IP address detector
4. explorer-------Open the resource manager
5. logoff---------logout command
6. tsshutdn-------60 second countdown shutdown command
7. lusrmgr.msc----local users and groups
8. services.msc---local service settings
9. oobe/msoobe /a----Check whether XP is activated
10. notepad--------Open Notepad
11. cleanmgr------garbage sorting
12. net start messenger----start messenger service
13. compmgmt.msc---Computer Management
14. net stop messenger-----stop the messenger service
15. conf-----------Start netmeeting
16. dvdplay--------DVD player
17. charmap--------Start character mapping table
18. diskmgmt.msc---disk management utility
19. calc----------Start the calculator
20. dfrg.msc-------disk defragmentation program
21. chkdsk.exe-----Chkdsk disk check
22. devmgmt.msc--- Device Manager
23. regsvr32 /u *.dll----stop dll file running
24. drwtsn32------System Doctor
25. rononce -p ----Shut down in 15 seconds
26. dxdiag---------Check DirectX information
27. regedt32-------registry editor
28. Msconfig.exe---system configuration utility
29. rsop.msc-------Group Policy Result Set
30. mem.exe--------Display memory usage
31. regedit.exe----registry
32. winchat--------XP comes with LAN chat
33. progman--------Program Manager
34. winmsd---------system information
35. perfmon.msc----computer performance monitoring program
36. winver---------Check Windows version
37. sfc /scannow-----scan errors and recover
38. taskmgr-----Task Manager (2000/xp/2003
39. winver---------Check Windows version
40. wmimgmt.msc----Open Windows Management Architecture (WMI)
41. wupdmgr--------windows update program
42. wscript--------windows script host settings
43. write----------writing board
44. winmsd---------system information
45. wiaacmgr-------Scanner and Camera Wizard
46. winchat--------XP comes with LAN chat
47. mem.exe--------Display memory usage
48. Msconfig.exe---system configuration utility
49. mplayer2-------simple windows media player
50. mspaint--------drawing board
51. mstsc----------Remote Desktop Connection
52. mplayer2-------media player
53. magnify--------magnifying glass utility
54. mmc------------Open the console
55. mobsync--------synchronization command
56. dxdiag---------Check DirectX information
57. drwtsn32------ System Doctor
58. devmgmt.msc--- Device Manager
59. dfrg.msc-------disk defragmentation program
60. diskmgmt.msc---disk management utility
61. dcomcnfg-------Open system component service
62. ddeshare-------Open DDE sharing settings
63. dvdplay--------DVD player
64. net stop messenger-----stop the messenger service
65. net start messenger----start messenger service
66. notepad--------open notepad
67. nslookup-------Network management tool wizard
68. ntbackup-------system backup and restore
69. narrator------screen "narrator"
70. ntmsmgr.msc----Mobile Storage Manager
71. ntmsoprq.msc---Mobile storage administrator operation request
72. netstat -an----(TC) command to check the interface
73. syncapp--------Create a briefcase
74. sysedit--------system configuration editor
75. sigverif-------File signature verification program
76. sndrec32-------recorder
77. shrpubw--------Create a shared folder
78. secpol.msc-----Local security policy
79. syskey---------system encryption. Once encrypted, it cannot be decrypted. It protects the double password of Windows XP system.
80. services.msc---Local service settings
81. Sndvol32-------volume control program
82. sfc.exe--------System File Checker
83. sfc /scannow---windows file protection
84. tsshutdn-------60 second countdown shutdown command
85. tourstart------xp introduction (the tour xp program that appears after the installation is completed)
86. taskmgr--------task manager
87. eventvwr-------event viewer
88. eudcedit------Word creation program
89. explorer-------open the resource manager
90. packager-------object packager
91. perfmon.msc----computer performance monitoring program
92. progman--------Program Manager
93. regedit.exe----registry
94. rsop.msc-------Group Policy Result Set
95. regedt32-------registry editor
96. rononce -p ----Shut down in 15 seconds
97. regsvr32 /u *.dll----stop dll file running
98. regsvr32 /u zipfldr.dll------Cancel ZIP support
99. cmd.exe--------CMD command prompt
100. chkdsk.exe-----Chkdsk disk check
101. certmgr.msc----Certificate management utility
102. calc----------Start the calculator
103. charmap--------Start character mapping table
104. cliconfg-------SQL SERVER client network utility
105. Clipbrd--------Clipboard viewer
106. conf-----------Start netmeeting
107. compmgmt.msc---Computer Management
108. cleanmgr-------garbage sorting
109. ciadv.msc------index service program
110. osk-------------Open the on-screen keyboard
111. odbcad32-------ODBC data source manager
112. oobe/msoobe /a----Check whether XP is activated
113. lusrmgr.msc----local users and groups
114. logoff---------logout command
115. iexpress-------Trojan horse bundling tool, comes with the system
116. Nslookup-------IP address detector
117. fsmgmt.msc-----Shared Folder Manager
118. utilman--------auxiliary tool manager
119. gpedit.msc-----Group Policy
120. explorer-------open the resource manager
javac is actually executed but does not output the execution result because you do not output the execution result part. If you execute the copied code, the code is as follows: Runtime.getRuntime.exec("cmd");
You can't see the result because it is not executed in the java program but on the operating system. You can copy the code as follows: Runtime.getRuntime.exec("cmd /c start javac ");
You will see a command line window.
I hope this article will be helpful to everyone’s Java programming.