java 如何使用java从另一个类获取命令行参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5061367/
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
how to get the command line arguments from another class with java
提问by kamikaze_pilot
so suppose I have a java package....
所以假设我有一个 java 包....
it's got the main class with the main method
它有带有 main 方法的主类
and then it's got a whole bunch of other classes.....
然后它有一大堆其他类.....
my question is, is it possible to get the args that was passed into the main method from these other classes that are not part of the main class but in the same package...
我的问题是,是否有可能从这些不属于主类但在同一个包中的其他类中获取传递到主方法中的参数...
采纳答案by paxdiablo
No, not portably, there may be some trickery based on the JVM implementation but I've never seen it, and it would be a very bad idea to rely on it even if it existed.
不,不可移植,可能有一些基于 JVM 实现的技巧,但我从未见过它,即使它存在,依赖它也是一个非常糟糕的主意。
If you want those values elsewhere, the main
function needs to make them available somehow.
如果你想在别处使用这些值,main
函数需要以某种方式使它们可用。
An easy way to do this (not necessarily the bestway) is to simply store away the strings as the first thing in main
and provide a means for getting at them:
一个简单的方法(不一定是最好的方法)是简单地将字符串作为第一件事存储起来,main
并提供一种获取它们的方法:
Scratch2.java:
Scratch2.java:
public class Scratch2 {
// Arguments and accessor for them.
private static String[] savedArgs;
public static String[] getArgs() {
return savedArgs;
}
public static void main(String[] args) {
// Save them away for later.
savedArgs = args;
// Test that other classes can get them.
CmdLineArgs cla = new CmdLineArgs();
cla.printArgs();
}
}
CmdLineArgs.java:
CmdLineArgs.java:
public class CmdLineArgs {
public void printArgs() {
String[] args = Scratch2.getArgs();
System.out.println ("Arg count is [" + args.length + "]");
for (int i = 0; i < args.length; i++) {
System.out.println ("Arg[" + i + "] is [" + args[i] + "]");
}
}
}
And, when run with the arguments a b c
, this outputs:
并且,当使用参数运行时a b c
,输出:
Arg count is [3]
Arg[0] is [a]
Arg[1] is [b]
Arg[2] is [c]
回答by Paul
The system-properties on some (?) JRE-implementations provide the system-property "sun.java.command" to get the programm-name and parameters that were used to start the program. Like "myjar.jar param1 param2 ...".
某些 (?) JRE 实现上的系统属性提供系统属性“sun.java.command”以获取用于启动程序的程序名称和参数。像“myjar.jar param1 param2 ...”。
While this value doesn't even belong to the set of properties that are mentioned in the documentation, it is present in both Oracle-JRE v1.8 and OpenJRE v1.8 (tested).
虽然此值甚至不属于文档中提到的属性集,但它存在于 Oracle-JRE v1.8 和 OpenJRE v1.8(已测试)中。
I couldn't find any documentation whether this value is supported by default though (best I could find was the list in the System#getProperties()
docs). Any clarification on this would be welcome. Handle with care!!!
我找不到任何文档是否默认支持此值(我能找到的最好的是System#getProperties()
文档中的列表)。欢迎对此做出任何澄清。小心轻放!!!
回答by Mike Samuel
If you don't care about OS portability, read /proc/self/cmdline
or the equivalent for your OS.
如果您不关心操作系统的可移植性,请阅读/proc/self/cmdline
您的操作系统的阅读或等效内容。
回答by Pa?lo Ebermann
As paxdiablo said, your main class would have to store these parameters and then either distribute or make available to needed ones. Often a good idea would be to let another class do the parsing of these parameters, and provide an object of this class instead of the raw command line to whoever needs it.
正如 paxdiablo 所说,您的主类必须存储这些参数,然后分发或提供给需要的参数。通常一个好主意是让另一个类来解析这些参数,并向需要它的人提供此类的对象而不是原始命令行。
回答by damurdock
I'm kind of a newb at this, but you should be able to store the string[] args to a private instance variable, then make an accessor method for it. E.g.,
我在这方面是个新手,但是您应该能够将 string[] args 存储到私有实例变量中,然后为它创建一个访问器方法。例如,
public class test {
private String[] myArgs = new String[10];
public static void main(String[] args) {
myArgs = args;
}
public String[] getArgs() {
return myArgs;
}
}
Not sure if it will work, but that's the idea.
不确定它是否会起作用,但这就是想法。