java -Ddata=args -Dcommit=no -jar post.jar
In the above script,how are data
and commit
accessed in java?
java -Ddata=args -Dcommit=no -jar post.jar
In the above script,how are data
and commit
accessed in java?
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");
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
For the -D example
String foo = java.lang.System.getProperty("data");