如何动态地将命令行参数传递给main方法 [英] how to pass command line arguments to main method dynamically

查看:345
本文介绍了如何动态地将命令行参数传递给main方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



现在我需要将命令行参数传递给该主类



有没有办法这样做?



这是我做的方式

  VirtualMachineManager manager = Bootstrap.virtualMachineManager(); 
LaunchingConnector connector = manager.defaultConnector();
映射参数= connector.defaultArguments();
((Connector.Argument)arguments.get(options))。setValue(userVMArgs);
((Connector.Argument)arguments.get(main))。setValue(cmdLine);

这里userVMargs是我的主类的classpath以及用于调用的类的类路径我的主类中的类的方法



和cmdLine正在将我的主类与类及其功能
一起使用,我将使用eclipse作为IDE来开发我的项目

解决方案

如果要通过发送参数启动VM,则应发送VM参数,而不是程序参数。



程序参数是传递给您的应用程序的参数,可以通过main方法的argsString数组参数进行访问。 VM参数是传递给JavaSW解释器的参数,例如System属性。上面的调试配置基本上等同于:

  java -DsysProp1 = sp1 -DsysProp2 = sp2 test.ArgsTest pro1 pro2 pro3 

虚拟机参数在调用Java解释器(即java)之后和Java类之前。程序参数在Java类之后。



考虑一个程序ArgsTest.java:

  package test; 

import java.io.IOException;

public class ArgsTest {

public static void main(String [] args)throws IOException {

System.out.println(程序参数:);
for(String arg:args){
System.out.println(\t+ arg);
}

System.out.println(来自VM参数的系统属性);
String sysProp1 =sysProp1;
System.out.println(\tName:+ sysProp1 +,Value:+ System.getProperty(sysProp1));
String sysProp2 =sysProp2;
System.out.println(\tName:+ sysProp2 +,值:+ System.getProperty(sysProp2));

}
}

如果给出输入为



  java -DsysProp1 = sp1 -DsysProp2 = sp2 test.ArgsTest pro1 pro2 pro3 

在命令行中,在项目bin文件夹中将给出以下结果:


 程序参数:
pro1
pro2
pro3
VM参数的系统属性
名称:sysProp1,值:sp1
名称:sysProp2,值:sp2



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?

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);

here userVMargs is classpath of my main class and the also classpath of the class which is being used to invoke the method of class inside my main class

and cmdLine is having my main class along with the class and its function and i am using eclipse as IDE to develop my project

解决方案

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

这篇关于如何动态地将命令行参数传递给main方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆