Java 是否可以从另一个方法调用传递 args[] 的 main 方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4122455/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to call the main method passing args[] from another method?
提问by abcdefg
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. Is there a way to do this?
我试图从另一个传递参数的方法调用一个类的 main 方法,就像从命令行运行这个类一样。有没有办法做到这一点?
回答by juhanic
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
您可以重命名您的主要内容并创建一个新的,使其称为“新”主要内容。至少这是我在单元测试时通常做的
回答by Andrei Fierbinteanu
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,你可以从任何其他类调用它,例如:
Test.main(new String[] { "a", "b" });
and this way you'll pass "a" and "b" as the parameters.
这样你将传递“a”和“b”作为参数。
回答by Bastien Jansen
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. You shouldn't call it directly, but rather create a new instance of your other class which will do all the initialization needed.
但我认为这不是一个好主意,main() 方法应该只包含一些调用构造函数的非常基本的代码。您不应该直接调用它,而应该创建另一个类的新实例,它将完成所有需要的初始化。
回答by Grodriguez
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);
}
}
回答by Buhake Sindi
The answer is yes,
答案是肯定的
Since main
is a static
method and is publicmethod, 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");
}
}
回答by aioobe
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:
作为旁注,您可以像这样声明 main 方法:
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).
生成的字节码是相同的(可变参数被编译为数组),因此它在所有方面都向后兼容(除了它不会在非可变参数的 java 编译器上编译)。