This may be simple to you people but as i am new to java, so i want know actually what is going on in the following part?
if (args.length > 0) {
file =
Those are called command line arguments , which you get as a String array in your program. Here is the Oracle tutorial
A Java application can accept any number of arguments from the command line. This allows the user to specify configuration information when the application is launched.
The user enters command-line arguments when invoking the application and specifies them after the name of the class to be run.
Hence the below code :
String file = "test1.xml";
if (args.length > 0) {
file = args[0];
}
Checks to see if the length of the String[] args
is greater than 0
, which means it checks if any command line argument was entered or is the array empty. If command line arguments were entered , then assign file
the first element of that array , or else default file
to test1.xml
. You can run your class as :
java DomTest1 someFileName.someExtension
When an application is launched, the runtime system passes the command-line arguments to the application's main method via an array of Strings. In the previous example, the command-line arguments passed to the DomTest1 application in an array that contains a single String: "someFileName.someExtension".