tags:

views:

38

answers:

3
java -Ddata=args -Dcommit=no -jar post.jar

In the above script,how are data and commit accessed in java?

+2  A: 

In your main function, your passed a string array which contains arguments.

The Java tutorial has a program that shows you how this works:

public class Echo {
    public static void main (String[] args) {
        for (String s: args) {
            System.out.println(s);
        }
    }
}

But this is only for arguments given to your code. Those particular ones you've shown are given to the Java interpreter itself and you can use System.getProperty() to access them:

String data = System.getProperty ("data");
String commit = System.getProperty ("commit");
paxdiablo
-D actually sets system properties, they're not command-line arguments
Michael Mrozek
+5  A: 

You use System.getProperty:

System.getProperty("data");
System.getProperty("commit");

As the name suggests, these are system properties and not command-line arguments as your title suggests. Command-line arguments would be java -jar post.jar arg1 arg2

Michael Mrozek
And command line arguments are accessed via the args parameter to `public static void main(String... args)`.
Software Monkey
A: 

For the -D example

   String foo = java.lang.System.getProperty("data");

See http://download.oracle.com/docs/cd/E17476_01/javase/1.4.2/docs/api/java/lang/System.html#getProperty(java.lang.String)

Charlie Martin