Pages

Tuesday, February 10, 2015

Command Line Arguments in Java

The following is a brief exploration of ways in which command line arguments can be called in a java program, first by setting a 'Runtime' variable, then executing an argument (String) with this variable as a process. A useful list of arguments is also provided right at the end for those who want to experiment.


 public static void main(String[] args) throws IOException {
        
        
            try
      {
         Runtime rt = Runtime.getRuntime();
         String cmdString = "cmd /c label e:ThisDrive";
        

         Process pr = rt.exec(cmdString);
                 
       }
      catch (Exception e)
      {
         System.out.println(e.toString());
         e.printStackTrace();
      }
    }
    
}



This block of code renames whatever drive is connected to E:/ to 'ThisDrive'. The following example shows a method to obtain information about the system in use, & any processes which are currently running, in the style of Windows task manager :


 public static void main(String args[]) throws AWTException, IOException
   {
      try
      {
         Runtime rt = Runtime.getRuntime();
         String cmdString = "cmd /c hostname";
         

         System.out.println("Hostname: ");
         Process pr = rt.exec(cmdString);
       

         BufferedReader input = new BufferedReader(new InputStreamReader(
                                                   pr.getInputStream()));

         String line = null;

         while ((line = input.readLine()) != null)
         {
            System.out.println(line);
            
      
         }

        
      }
      catch (Exception e)
      {
         System.out.println(e.toString());
         e.printStackTrace();
      }
      
      try
      {
         Runtime rt = Runtime.getRuntime();
         String cmdString = "cmd /c whoami";
         

         System.out.println("\nUser: ");
         Process pr = rt.exec(cmdString);
       

         BufferedReader input = new BufferedReader(new InputStreamReader(
                                                   pr.getInputStream()));

         String line = null;

         while ((line = input.readLine()) != null)
         {
            System.out.println(line);
          
         }

        
      }
      catch (Exception e)
      {
         System.out.println(e.toString());
         e.printStackTrace();
      }
      
      
       try
      {
         Runtime rt = Runtime.getRuntime();
         String cmdString = "cmd /c date /t";
         

         System.out.println("\nSystem Date: ");
         Process pr = rt.exec(cmdString);
       

         BufferedReader input = new BufferedReader(new InputStreamReader(
                                                   pr.getInputStream()));

         String line = null;

         while ((line = input.readLine()) != null)
         {
            System.out.println(line);
          
            
         }

       
      }
      catch (Exception e)
      {
         System.out.println(e.toString());
         e.printStackTrace();
      }
       try
      {
         Runtime rt = Runtime.getRuntime();
         String cmdString = "cmd /c tasklist";
         

         System.out.println("\nCurrent Tasks: ");
         Process pr = rt.exec(cmdString);
       

         BufferedReader input = new BufferedReader(new InputStreamReader(
                                                   pr.getInputStream()));

         String line = null;

         while ((line = input.readLine()) != null)
         {
            System.out.println(line);
          
            
         }

       
      }
      catch (Exception e)
      {
         System.out.println(e.toString());
         e.printStackTrace();
      }
     

   }



The first block here runs a command line argument asking for the name of the current System & returns it. Next, the username is called for & returned, then the current date & finally a list of system processes. The output will look akin to this :


run:
Hostname: 
John

User: 
John/ Vitrus

System Date: 
10/02/2015 

Current Tasks: 

Image Name                     PID Session Name        Session#    Mem Usage
========================= ======== ================ =========== ============
System Idle Process              0 Services                   0          4 K
System                           4 Services                   0     17,096 K
smss.exe                       320 Services                   0        472 K
csrss.exe                      556 Services                   0      2,860 K
wininit.exe                    636 Services                   0      2,248 K
services.exe                   744 Services                   0      4,276 K
lsass.exe                      764 Services                   0      8,956 K
svchost.exe                    832 Services                   0      9,472 K
svchost.exe                    876 Services                   0      6,136 K
svchost.exe                    988 Services                   0     15,412 K
svchost.exe                    240 Services                   0     38,728 K
svchost.exe                    364 Services                   0     11,720 K
igfxCUIService.exe             584 Services                   0      2,140 K
svchost.exe                    660 Services                   0     76,292 K
svchost.exe                    768 Services                   0     12,072 K
spoolsv.exe                   1220 Services                   0     10,308 K
svchost.exe                   1264 Services                   0     18,152 K
dasHost.exe                   1384 Services                   0      6,568 K
HeciServer.exe                1424 Services                   0      1,028 K
Service.exe                   1508 Services                   0      1,612 K
mfevtps.exe                   1532 Services                   0      3,336 K
NitroPDFDriverService9x64     1564 Services                   0        568 K
NLSSRV32.EXE                  1596 Services                   0         60 K
RichVideo64.exe               1660 Services                   0      1,104 K
svchost.exe                   1708 Services                   0      7,256 K
McAPExe.exe                   1780 Services                   0        652 K
mcshield.exe                  1820 Services                   0    152,424 K
mfefire.exe                   1900 Services                   0      3,428 K
McSvHost.exe                  1956 Services                   0     28,020 K
svchost.exe                   2184 Services                   0      2,264 K
WUDFHost.exe                  2432 Services                   0      2,756 K
svchost.exe                   2932 Services                   0      5,232 K
CltMngSvc.exe                 2964 Services                   0      2,904 K
McCSPServiceHost.exe          2544 Services                   0      4,828 K
SearchIndexer.exe             1244 Services                   0     28,884 K
PresentationFontCache.exe     2440 Services                   0      2,140 K
GoogleCrashHandler.exe        1516 Services                   0        868 K
GoogleCrashHandler64.exe      3392 Services                   0        132 K
WmiPrvSE.exe                  5624 Services                   0     12,712 K
csrss.exe                     5748 Console                    7     35,048 K
winlogon.exe                  6024 Console                    7      4,736 K
dwm.exe                       4192 Console                    7     23,448 K
cltmng.exe                    5212 Console                    7     29,220 K
cltmngui.exe                  5980 Console                    7     10,980 K
taskhostex.exe                2312 Console                    7      9,712 K
McUICnt.exe                   5372 Console                    7     33,696 K
explorer.exe                  3196 Console                    7     68,696 K
igfxHK.exe                    5204 Console                    7      8,236 K
igfxTray.exe                  5336 Console                    7      9,484 K
igfxEM.exe                    6112 Console                    7     10,664 K
taskhost.exe                  1148 Console                    7      5,908 K
SkyDrive.exe                  3356 Console                    7     13,636 K
chrome.exe                    5420 Console                    7    142,224 K
chrome.exe                    5596 Console                    7     73,912 K
RAVCpl64.exe                  2260 Console                    7      9,508 K
chrome.exe                    5384 Console                        7     76,328 K
hotkey.exe                    2568 Console                         7      5,264 K
SettingSyncHost.exe           5704 Console                 7      3,616 K
JME_LOAD.exe                  5716 Console                    7      5,704 K
PDVD10Serv.exe                4684 Console                    7      5,524 K
CLMLSvc.exe                   1948 Console                    7      7,464 K
CCleaner64.exe                5744 Console                    7        848 K
livecomm.exe                  1892 Console                    7     13,964 K
RuntimeBroker.exe             5932 Console                    7     14,304 K
WWAHost.exe                    752 Console                    7     57,660 K
taskhostex.exe                4676 Services                   0      4,640 K
taskhost.exe                  3560 Console                    7     13,308 K
audiodg.exe                   4272 Services                   0     14,384 K
chrome.exe                    3120 Console                    7    226,728 K
chrome.exe                    4496 Console                    7    220,624 K
chrome.exe                    3620 Console                    7     94,916 K
netbeans64.exe                3292 Console                    7    565,888 K
taskeng.exe                   3640 Services                   0      4,384 K
java.exe                      4752 Console                    7     14,056 K
conhost.exe                   3936 Console                    7      3,240 K
cmd.exe                       4356 Console                    7      2,028 K
conhost.exe                   3664 Console                    7      3,228 K
tasklist.exe                  4280 Console                    7      5,372 K



Hopefully this was of some use to those wishing to understand cmd arguments in java a little better. The following link is a useful resource on such commands, though a list is also available from cmd itself by typing 'help' or 'help' + whatever command you need information on :

An A-Z Index of the Windows CMD command line