I am passing my main class as a command line argument to launch VM
Now i need to pass command line arguments to that main class
Is there any way to do this?<
Run ---> Debug Configuration ---> YourConfiguration ---> Arguments tab
go to Run Configuration and in argument tab you can write your argument
We can pass string value to main method as argument without using commandline argument concept in java through Netbean
package MainClass;
import java.util.Scanner;
public class CmdLineArgDemo {
static{
Scanner readData = new Scanner(System.in);
System.out.println("Enter any string :");
String str = readData.nextLine();
String [] str1 = str.split(" ");
// System.out.println(str1.length);
CmdLineArgDemo.main(str1);
}
public static void main(String [] args){
for(int i = 0 ; i<args.length;i++) {
System.out.print(args[i]+" ");
}
}
}
Enter any string :
Coders invent Digital World
Coders invent Digital World
If you want to launch VM by sending arguments, you should send VM arguments and not Program arguments.
Program arguments are arguments that are passed to your application, which are accessible via the "args" String array parameter of your main method. VM arguments are arguments such as System properties that are passed to the JavaSW interpreter. The Debug configuration above is essentially equivalent to:
java -DsysProp1=sp1 -DsysProp2=sp2 test.ArgsTest pro1 pro2 pro3
The VM arguments go after the call to your Java interpreter (ie, 'java') and before the Java class. Program arguments go after your Java class.
Consider a program ArgsTest.java:
package test;
import java.io.IOException;
public class ArgsTest {
public static void main(String[] args) throws IOException {
System.out.println("Program Arguments:");
for (String arg : args) {
System.out.println("\t" + arg);
}
System.out.println("System Properties from VM Arguments");
String sysProp1 = "sysProp1";
System.out.println("\tName:" + sysProp1 + ", Value:" + System.getProperty(sysProp1));
String sysProp2 = "sysProp2";
System.out.println("\tName:" + sysProp2 + ", Value:" + System.getProperty(sysProp2));
}
}
If given input as,
java -DsysProp1=sp1 -DsysProp2=sp2 test.ArgsTest pro1 pro2 pro3
in the commandline, in project bin folder would give the following result:
Program Arguments: pro1 pro2 pro3 System Properties from VM Arguments Name:sysProp1, Value:sp1 Name:sysProp2, Value:sp2