简体   繁体   English

是否可以调用从另一个方法传递args []的main方法?

[英]Is it possible to call the main method passing args[] from another method?

I'm trying to call the main method of a class from another method passing arguments like when running the class from the command line. 我试图从另一个方法调用类的main方法,如从命令行运行类时传递参数。 Is there a way to do this? 有没有办法做到这一点?

You can call the main method as you would call any other (static) method: 您可以像调用任何其他(静态)方法一样调用main方法:

MyClass.main(new String[] {"arg1", "arg2", "arg3"});

Example: 例:

class MyClass {
    public static void test() {
        MyClass.main(new String[] {"arg1", "arg2", "arg3"});
    }

    public static void main(String args[]) {
        for (String s : args)
            System.out.println(s);
    }
}

Yes, the main method can be called like any other method, so if you have a class Test with a main method, you can call it from any other class like: 是的,main方法可以像任何其他方法一样调用,因此如果你有一个带有main方法的类,你可以从任何其他类调用它,如:

Test.main(new String[] { "a", "b" });

and this way you'll pass "a" and "b" as the parameters. 这样你就可以传递“a”和“b”作为参数。

Have you tried something like : 你尝试过类似的东西:

// In your method
String[] yourArgs = new String[] {"foo", "baz", "bar"};
YourClassWithMain.main(yourArgs);

But I think this is not a good idea, the main() method should only contain some very basic code which calls the constructor. 但我认为这不是一个好主意,main()方法应该只包含一些调用构造函数的非常基本的代码。 You shouldn't call it directly, but rather create a new instance of your other class which will do all the initialization needed. 您不应该直接调用它,而是创建另一个类的新实例,它将执行所需的所有初始化。

The answer is yes, 答案是肯定的,

Since main is a static method and is public method, you can do this (and it compiled on my case): 由于main是一个static方法并且是公共方法,你可以这样做(并在我的情况下编译):

/**
 * @author The Elite Gentleman
 *
 */
public class Test {

    /**
     * 
     */
    public Test() {
        super();
        // TODO Auto-generated constructor stub
        Test.main(new String[] {"main"}); //Yes, it works and compiles....
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("Hello");

    }
}

Sure, you can call the main -method just as an ordinary (static) method like this: 当然,您可以将main -method称为普通(静态)方法,如下所示:

TheClass.main(new String[] { "lorem", "ipsum" });

As a side note, you could declare the main method like this: 作为旁注,您可以声明主方法,如下所示:

public static void main(String... args) { ... }

and call it like 并称之为

TheClass.main("lorem", "ipsum");

The bytecode produced is the same (varargs are compiled to arrays), so it is backward compatible in all ways (except that it won't compile on non-vararg aware java-compilers). 生成的字节码是相同的(varargs被编译为数组),因此它以各种方式向后兼容(除了它不会在非vararg感知的java编译器上编译)。

You could just rename your main and make a new one, making it call the "new" main. 你可以重命名你的主人并制作一个新主人,让它称为“新”主。 At least that's what I generally do when unit testing 至少这是我在进行单元测试时通常所做的事情

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

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