Java 如何向main添加命令参数?爪哇
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20438140/
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 add command arguments to main? Java
提问by user977154
How can I setup my main to accept command switches?
如何设置我的 main 以接受命令开关?
a source port (-s source_port)
and the destination hostname (-h hostname)
and port (-p port)
of the receiving application as well as the audit log file (-l log_file)
. One IDS misuse keyword (–m misuse_keyword)
and a misuse threshold (-t misuse_threshold)
are also specified on the command line.
接收应用程序的源端口(-s source_port)
和目标主机名(-h hostname)
和端口(-p port)
以及审计日志文件(-l log_file)
。命令行中还指定(–m misuse_keyword)
了一个 IDS 误用关键字和一个误用阈值(-t misuse_threshold)
。
Basically, I want to have -h localhost set a field named hostname = localhost and -l bob.txt set to a field named inputfile = "bob.txt"
. How do I do this in Java? I can do this in C and C++ but not sure how to do this in Java.
基本上,我想让 -h localhost 设置一个名为 hostname = localhost 的字段,并将 -l bob.txt 设置为一个名为inputfile = "bob.txt"
. 我如何在 Java 中做到这一点?我可以在 C 和 C++ 中做到这一点,但不确定如何在 Java 中做到这一点。
public static void main(String[] args) {
}
采纳答案by AJ.
Do something like this in your main()
method
在你的main()
方法中 做这样的事情
args = new String[12];
args[0]="-s";
args[1]="source_port";// something integer value "1324"
args[2]="-h";
args[3]="hostname";
args[4]="-p";
args[5]="port";
args[6]="-l";
args[7]="log_file";
args[8]="-m";
args[9]="misuse_keyword";
args[10]="-t";
args[11]="misuse_threshold";
while storing in another variable
存储在另一个变量中时
int source_port = Integer.parseInt(args[1]);
回答by Keerthivasan
Just put the argument after your program name. For example
只需将参数放在程序名称之后即可。例如
java addProgram 1 2
In your case, it should be
在你的情况下,它应该是
java YourProgramName "-s source_port_value" "-h hostname_value" "-p port_value"
These values will be received by main method in args (String array), then you can iterate the array and get the flag options and their corresponding values to proceed with your operation. I have put the double quotes to make them as a single input value. we have three arguments passing into the main method
这些值将由 args(字符串数组)中的 main 方法接收,然后您可以迭代该数组并获取标志选项及其对应的值以继续您的操作。我已经把双引号作为一个单一的输入值。我们有三个参数传递到 main 方法中
Logic to parse the value
解析值的逻辑
public static void main(String[] args){
HashMap<String,String> properties = new HashMap<String,String>();
for(int i=0;i<args.length;i++){
if(args[i].trim().startsWith("-s"))
properties.put("source_port",args[i].split(" ")[1]);
if(args[i].trim().startsWith("-h"))
properties.put("hostname",args[i].split(" ")[1]);
if(args[i].trim().startsWith("-p"))
properties.put("port",args[i].split(" ")[1]);
}
//this will give you all the property values mapped with their keys after this looping
//you can get the values from properties map from here onwards
}
回答by Alexey A.
In java if you pass -h hostname -p port
you will receive array with 4 elements {"-h", "hostname", "-p", "port"}
. So you will have to manage the keys parsing manually.
在 java 中,如果您通过,-h hostname -p port
您将收到包含 4 个元素的数组{"-h", "hostname", "-p", "port"}
。因此,您必须手动管理密钥解析。
Here is an example:
下面是一个例子:
private static final String HOST_KEY = "-h";
private static final String PORT_KEY = "-p";
public static void main(String[] args)
{
String host = "";
String port = "";
for(int i=0; i<args.length; i+=2)
{
String key = args[i];
String value = args[i+1];
switch (key)
{
case HOST_KEY : host = value; break;
case PORT_KEY : port = value; break;
}
}
}