将cmd行参数传递给特定方法 [英] Passing cmd line arguments to specific method
本文介绍了将cmd行参数传递给特定方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Java中,您可以在程序启动期间将cmd行参数传递给 main()
方法.
In java, you can pass cmd line arguments to the main()
method during program startup.
我想将cmd行参数传递给我直接从脚本调用的特定方法.我没有像进入 main()
那样输入它们的运气.
I'd like to pass cmd line arguments to a specific method that I call from a script directly. I'd had no luck just entering them as I would for main()
.
示例:
public class Test {
public static void main(String[] args) { // args is cmd line input
// do stuff with args
}
}
您会这样做:〜]#java Test cmdlineinput
我需要做:
public class Test {
public void someMethod(String input) {
// do stuff with input
}
}
我想做:〜]#java Test.someMethod cmdlineinput
这有可能吗?
推荐答案
您的程序需要一个主方法才能运行,并且您必须将命令行参数直接或间接地从该主方法传递到所需的位置.我没有其他解决办法.
Your program needs a main method to run, and you have to pass the command line arguments directly or indirectly from the main method to wherever you desire it to go. I know of no other solution.
即
public static void main(String[] args) {
Test test = new Test();
test.someMethod(args);
}
这篇关于将cmd行参数传递给特定方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文