Faqts : Business : Programming : Shopping For You : Java

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

48 of 75 people (64%) answered Yes
Recently 7 of 10 people (70%) answered Yes

Entry

Java: How to pass command line parameter arguments to a Java standalone program?

Sep 7th, 2009 04:44
Knud van Eeden, Joe Bloggs,


----------------------------------------------------------------------
--- Knud van Eeden --- 25 April 2001 - 01:28 am ---------------------

Java: How to pass command line parameter arguments to a Java 
standalone program?

---

A standalone Java program runs outside of a browser or appletviewer.

---

When you run a standalone program, you use the Java interpreter 
(=java.exe)

---

Depending on your program's purpose, there may be times when you will
want the user to include specific information about the command line
when you start the program.

---

For example, assume that you have created a standalone program, named
'RemotePrint' that will print a file on a remote network printer.
When the user runs the program, they specify the name of the file they
want to print, as shown:

c:\> java RemotePrinter SampleFile.java <ENTER>

To access the command line arguments, your programs access a special
array with the fixed unchanging name of 'args', of string variables the
Java interpreter will pass to the 'main' function:

public static void main( String args[] )

---

The following program, 'ShowCommandLineArguments.java', displays your
program's
command-line arguments by looping through the elements of the 'args'
array:

--- cut here ---------------------------------------------------------

class ShowCommandLineArguments {
 public static void main( String args[] ) {
  int I;
  for ( I = 0; I < args.length; I++ ) {
   System.out.println( args[ I ] );
  }
 }
}

--- cut here ---------------------------------------------------------

---

Using the Java compiler to compile this program, and then invoke the
program by using different command-line parameters, as shown:

c:\>  java ShowCommandLineArguments Hello World from Java <ENTER>

will show:

Hello
World
from
Java

---

[book: source: Jamsa, Kris - Java Now! - ISBN 1-884133-30-4 - p. 
137 'Using command line arguments in a standalone Java program']

----------------------------------------------------------------------