简体   繁体   English

如何动态地将命令行参数传递给主方法

[英]how to pass command line arguments to main method dynamically

I am passing my main class as a command line argument to launch VM我将我的主类作为命令行参数传递给启动 VM

Now i need to pass command line arguments to that main class现在我需要将命令行参数传递给那个主类

Is there any way to do this?有没有办法做到这一点?

this is the way i am doing it我就是这样做的

    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,你应该发送 VM 参数而不是程序参数。

Program arguments are arguments that are passed to your application, which are accessible via the "args" String array parameter of your main method.程序参数是传递给应用程序的参数,可通过 main 方法的“args”字符串数组参数访问。 VM arguments are arguments such as System properties that are passed to the JavaSW interpreter. VM 参数是传递给 JavaSW 解释器的参数,例如系统属性。 The Debug configuration above is essentially equivalent to:上面的 Debug 配置本质上等价于:

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>我们可以通过Netbean<\/em>将字符串值作为参数传递给 main 方法,而无需在 java 中使用命令行参数概念

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

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

"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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