简体   繁体   中英

how to pass command line arguments to main method dynamically

I am passing my main class as a command line argument to launch VM

    VirtualMachineManager manager = Bootstrap.virtualMachineManager();
    LaunchingConnector connector = manager.defaultConnector();
    Map arguments = connector.defaultArguments();
    ((Connector.Argument)arguments.get("options")).setValue(userVMArgs);
    ((Connector.Argument)arguments.get("main")).setValue(cmdLine);

If you want to launch VM by sending arguments, you should send VM arguments and not Program arguments.

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
Run ---> Debug Configuration ---> YourConfiguration ---> Arguments tab

We can pass string value to main method as argument without using commandline argument concept in java through Netbean<\/em>

 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]+" ");
      }
    }
  }

转到运行配置并在参数选项卡中,您可以编写参数

"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM