Command Line Parameters

LA home
Computing
Prog.Langs
 Java

It's just like C -- see String[] argv below:

import java.lang.*;
import java.io.*;

public class CmdLine
 {
   public static void  main(String[] argv)
   { System.out.println("#--- CmdLine.java, L.Allison, .au ---");

     for(int i=0; i < argv.length; i++) // command line params if any
        System.out.print("argv[" + i + "]=" + argv[i] + "\n");

     System.out.println("#--- end ---");
   }//main

 }//CmdLine class

// October 2002, L.Allison.

CmdLine.java lists its command-line parameters.

E.g. Cat someFile:

import java.io.*;

public class Cat
// Cat after cat as in conCATenate a file, try Unix: man cat
 {
   public static void  main(String[] argv)
   { System.out.println("#--- Cat.java, L.Allison ---");

     for(int i=0; i < argv.length; i++) // command line params if any
        System.out.print("# argv[" + i + "]=" + argv[i] + "\n");


     if( argv.length <= 0 )
        { System.out.println( "failed: no file given" );
          System.exit(1);
        }


     try{ File inputFile = new File( argv[0] );
          FileReader in  = new FileReader(inputFile);
          int c;
          while ((c = in.read()) != -1)       // read a character and
             System.out.print( (char) c );    // print it
        }
     catch(Exception e)
        { System.out.println( "failed: " + e.getMessage());
          System.exit(1);
        }


     System.out.println("#--- end ---");
   }//main

 }//Cat class

// October 2002, L.Allison.
// Released under GNU General Public Licence (GPL) ver.2, 1991.
// http://www.gnu.org/copyleft/gpl.html

java Cat someFile   copies the named file to standard output.

www #ad:

Java
 APIs

↑ © L. Allison, www.allisons.org/ll/   (or as otherwise indicated).
Created with "vi (Linux)",  charset=iso-8859-1,   fetched Thursday, 25-Apr-2024 00:48:23 UTC.

Free: Linux, Ubuntu operating-sys, OpenOffice office-suite, The GIMP ~photoshop, Firefox web-browser, FlashBlock flash on/off.