[英]Trying to pass arguments to java application
So I have a program with an updater and I've made a mistake before releasing it. 所以我有一个带有更新程序的程序,在发布之前犯了一个错误。 I totally forgot about by-passing the update so the user can update it later.
我完全忘记了绕过更新,因此用户以后可以更新它。
Now I'm trying to fix it and I thought that creating an argument with "-no_patching" is the best solution to this. 现在,我正在尝试对其进行修复,并且我认为使用“ -no_patching”创建参数是对此的最佳解决方案。
Here's the code: 这是代码:
public static void main(String... args) throws Exception {
if (args.length == 0) {
checkVersion();
System.err.println("patching ON");
} else if(args.toString().matches("-no_patching")) {
System.err.println("patching OFF");
launchApp();
}
}
The thing is when i run the program with argument it runs for 1 second then it stops. 问题是,当我运行带有参数的程序时,它将运行1秒钟,然后停止。 What I'm doing wrong ?
我做错了什么?
Here is the mistake at line args.toString().matches("-no_patching")
. 这是
args.toString().matches("-no_patching")
行的错误。
That should be 那应该是
else if(args[0].equals("-no_patching")){ // make sure args length is 1
System.err.println("patching OFF");
launchApp();
}
toString()
on args
array wont give you contents. args
数组上的toString()
不会给您内容。
You are trying to match the args
array instead of matching first argument from that array. 您正在尝试匹配
args
数组,而不是匹配该数组的第一个参数。
//this is not what you want
args.toString().matches("-no_patching")
You need to get first element from array and then do comparison : 您需要从数组中获取第一个元素,然后进行比较:
args[0].equals("-no_patching")
args is array. args是数组。 You should do it like this:
您应该这样做:
for(String arg : args)
{
if(arg.matches("-no_patching")){
System.err.println("patching OFF");
launchApp();
}
}
Try this, convert args to a List and use the contains method to see if there are any argument matches: 尝试此操作,将args转换为List并使用contains方法查看是否有任何参数匹配:
public static void main(String... args) throws Exception {
if(args.length == 0){
checkVersion();
System.err.println("patching ON");
} else if(Arrays.asList(args).contains("-no_patching")){
System.err.println("patching OFF");
launchApp();
}
}
i totally forgot about array thing... i guess I panicked a bit xD 我完全忘了数组的事...我想我有点慌张了
else if(args[0].equals("-no_patching")){
//do something
}
did the trick, Thank you! 做到了,谢谢!
I think you are trying an incorrect comparison in the ELSE IF 我认为您尝试在ELSE IF中进行不正确的比较
else if(args.toString().matches("-no_patching"))
args.toString()
would give some address value of the argument array args. args.toString()
将给出参数数组args的一些地址值。 If this is compared with your "-no_patching" argument it would definitely return FALSE. 如果将此参数与您的“ -no_patching”参数进行比较,则肯定会返回FALSE。
Rather you can always compare like 相反,您总是可以像
else if(args[0].toString().matches("-no_patching"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.