Java executes the Windows CMD command method

  • 2020-04-01 03:34:16
  • OfStack

This article illustrates how Java executes the CMD command under Windows. Share with you for your reference.

The specific implementation code is as follows:

//Gets the runtime 
Runtime rt = Runtime.getRuntime();
//Gets the process
Process p = rt.exec(String[] cmdarray);//Or Process p = rt.exec(String CMD); < br / > //If p is not empty, clear
if(null!=p){destory
  p.destory();
  p=null;
}

Such as:

Runtime rt = Runtime.getRuntime();
Process p = rt.exec("cmd.exe /c shutdown -a");
System.out.println(p.toString());

CMD /c dir is to close the command window after executing the dir command.

CMD /k dir is to execute the dir command without closing the command window.

CMD /c start dir opens a new window and executes the dir command. The original window closes.

CMD /k start dir will open a new window and execute the dir command. The original window will not close.

You can use CMD /? See help information.

Supplement:

Taken the CMD command to end
1. Gpedit. MSC -- group policy
2. Sndrec32 -- recorder
3. Nslookup-- IP address detector
4. Explorer -- open explorer
5. Logoff -- logoff order
Tsshutdn --60 seconds countdown shutdown command
7. Lusrmgr.msc -- native users and groups
8. Services. MSC -- local service Settings
9. Oobe /msoobe /a-- check whether XP is active
10. Notepad -- open notepad
11. Cleanmgr -- garbage disposal
12. Net start messenger-- start a messenger service
13. Compmgmt. MSC -- computer management
14. Net stop messenger-- stop messenger service
Conf -- start netmeeting
16. Dvdplay --DVD player
Charmap -- start the charmap table
18. Diskmgmt.msc -- disk management utility
19. Calc -- start the calculator
20. DFRG. MSC -- disk defragmenter
21. CHKDSK. Exe -- CHKDSK disk check
22. Devmgmt. MSC -- device manager
23. Regsvr32 /u *.dll- stops the DLL file from running
24. Drwtsn32 -- system doctor
25. Rononce-p -- power off 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 -- displays memory usage
31. Regedit. Exe -- registry
32. Winchat --XP comes with its own LAN
33. Progman -- program manager
Winmsd -- system information
35. Perfmon.msc -- computer performance monitoring program
36. Winver -- check the Windows version
37. SFC /scannow-- scan for errors and recover
38. Taskmgr -- task manager (2000 / xp / 2003
39. Winver -- check the Windows version
40. Wmimgmt.msc -- open Windows management architecture (WMI)
41. Wupdmgr -- Windows update program
42. Wscript -- Windows script host Settings
Write -- write-- write-- write-- write-- write-- write-- write-- write--
44. Winmsd -- system information
45. Wiaacmgr -- scanner and camera wizard
46. Winchat --XP comes with its own LAN
47. Mem. Exe -- displays memory usage
48. Msconfig.exe-- system configuration utility
49. Mplayer2 -- simple widnows media player
Mspaint -- draw a board
51. MSTSC -- remote desktop connection
52. Mplayer2 -- media player
Magnify - magnify utility
54. MMC -- open the console
55. Mobsync -- sync commands
Dxdiag -- check DirectX information
57. Drwtsn32 -- system doctor
58. Devmgmt. MSC -- device manager
DFRG. MSC -- disk defragmenter
60. Diskmgmt.msc -- disk management utility
Dcomcnfg -- open system component service
62. Ddeshare -- turn on DDE sharing Settings
63. Dvdplay --DVD player
64. Net stop messenger-- stop messenger service
65. Net start messenger- to start a messenger service
66. Notepad -- open notepad
67. Nslookup -- tool wizard for network management
Ntbackup -- system backup and restore
69. Denise -- screen narrator
70. Ntmsmgr.msc -- mobile storage manager
Ntmsoprq.msc -- mobile storage administrator action request
72. Netstat -an-- (TC) command checks 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
Syskey - system encryption, once the encryption can not be unlocked, protect Windows xp system double password
80. Services. MSC -- local service Settings
81. Sndvol32-- volume control program
Sfc. exe-- system file inspector
83. SFC /scannow-- Windows file protection
Tsshutdn --60 seconds countdown shutdown command
85. Tourstart -- introduction to xp (roaming xp program after installation)
86. Taskmgr -- task manager
87. Eventvwr -- event viewer
Eudcedit -- word program
89. Explorer -- open explorer
90. Packager -- object wrapper
91. Perfmon.msc -- computer performance monitoring program
92. Progman -- program manager
93. Regedit. Exe -- registry
94. Rsop. MSC -- group policy result set
Regedt32 -- registry editor
96. Rononce-p -- power off in 15 seconds
97. Regsvr32 /u *.dll- stops the DLL file from running
98. Regsvr32 /u zipfldr.dll-- unzip support
99. Cmd.exe --CMD command prompt
CHKDSK. Exe -- CHKDSK disk check
101.cermgr. MSC -- certificate management utility
102. Calc -- start the calculator
103. Charmap -- start the charmap table
Cliconfg --SQL SERVER client network utility
Clipbrd-- clipboard viewer
106. Conf -- launch netmeeting
107. Compmgmt. MSC -- computer management
108.cleanmgr -- garbage disposal
109. Ciadv. MSC -- index service program
110. Osk -- open the on-screen keyboard
111. Odbcad32 --ODBC data source manager
112. oobe/msoobe/a -- check if XP is active
113. Lusrmgr.msc -- native users and groups
114. Logoff -- log out order
115. Iexpress -- trojans bundled tools, the system comes with
116. Nslookup-- IP address detector
117. Fsmgmt.msc -- Shared folder manager
118. Utilman -- utility manager
Gpedit.msc -- group policy
120. Explorer -- open explorer
Javac actually executes but it doesn't print the execution because you don't print the execution
If you execute

Runtime.getRuntime.exec("cmd");

I don't see the results
Because it is not executed in a Java program but on the operating system
You can do this
Runtime.getRuntime.exec("cmd /c start javac ");

You can see a command line window.

I hope this article has been helpful to your Java programming.


Related articles: