Command Line Parameters

LA home
Computing
 Algorithms
 Bioinformatics
 FP,  λ
 Logic,  π
 MML
 Prog.Langs

Prog'Lang'
 glossary
 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.A., CSSE, Monash, .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, CSSE, Monash University, .au

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.A., CSSE, Monash, .au ---");

     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, CSSE, Monash University, .au
// 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.

window on the wide world:

Computer Science Education Week

Linux
 Ubuntu
free op. sys.
OpenOffice
free office suite,
ver 3.4+

The GIMP
~ free photoshop
Firefox
web browser
FlashBlock
like it says!

Java
 API

© L. Allison   http://www.allisons.org/ll/   (or as otherwise indicated),
Faculty of Information Technology (Clayton), Monash University, Australia 3800 (6/'05 was School of Computer Science and Software Engineering, Fac. Info. Tech., Monash University,
was Department of Computer Science, Fac. Comp. & Info. Tech., '89 was Department of Computer Science, Fac. Sci., '68-'71 was Department of Information Science, Fac. Sci.)
Created with "vi (Linux + Solaris)",  charset=iso-8859-1,  fetched Sunday, 20-Apr-2014 08:07:28 EST.