试图将参数传递给java应用程序

时间:2016-10-03 11:24:35

标签: java arguments

所以我有一个带有更新程序的程序,在发布之前我犯了一个错误。我完全忘记绕过更新,以便用户以后可以更新。

现在我正在尝试修复它,我认为用" -no_patching"创建一个参数。是最好的解决方案。

以下是代码:

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();
    }
}

问题是,当我运行带有参数的程序时,它运行1秒然后停止。我做错了什么?

6 个答案:

答案 0 :(得分:1)

这是args.toString().matches("-no_patching")行的错误。

那应该是

else if(args[0].equals("-no_patching")){ // make sure args length is 1
            System.err.println("patching OFF");
            launchApp();
        }
<{1}}数组上的

toString()不会给你内容。

答案 1 :(得分:1)

您正在尝试匹配args数组,而不是匹配该数组中的第一个参数。

//this is not what you want
args.toString().matches("-no_patching") 

您需要从数组中获取第一个元素,然后进行比较:

args[0].equals("-no_patching")

答案 2 :(得分:1)

args是数组。你应该这样做:

<div id="fullpage">
  <div class="section" data-anchor="one">Section One</div>
  <div class="section" data-anchor="two">Section Two</div>
  <div class="section">Section Two sub page one</div>
  <div class="section">Section Two sub page two</div>
  <div class="section" data-anchor="three">Section Three</div>
  <div class="section" data-anchor="four">Section Four</div>
</div>

<div class="nav-wrapper">
  <hr>
  <ul id="myMenu">
    <li data-menuanchor="firstPage" class="active"><a href="#one">First section</a></li>
    <li data-menuanchor="secondPage"><a href="#two">Second section</a></li>
    <li data-menuanchor="thirdPage"><a href="#three">Third section</a></li>
    <li data-menuanchor="fourthPage"><a href="#four">Fourth section</a></li>
  </ul>
</div>



.section {
  text-align:center;
  font-size: 3em;
}
.content{
  margin:50px
}
#myMenu{position:absolute; background-color:#eee; top:0; width:100%; margin:0px !important; padding:0px !important;}

.active{font-size:15px; background-color:purple; }
.nav-wrapper{position:absolute; height:20px; bottom:0;width:100%; z-index:999999999; background:blue;}
.nav-wrapper > ul li {list-style:none; display:inline-block; padding:0px !important; margin:0px ; margin-left:-4px; text-align:center;}
.nav-wrapper ul li{width:calc(100% / 4);}
hr {
  background: #f00  none repeat scroll 0 0;
  height: 5px;
  position: relative;
  width: 100%;
  z-index: 999999999;
  margin:-5px
}

答案 3 :(得分:0)

试试这个,将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();
    }
}

答案 4 :(得分:0)

我完全忘记了数组的事情...我想我有点惊慌xD

else if(args[0].equals("-no_patching")){
   //do something
}

做了诀窍,谢谢!

答案 5 :(得分:0)

我认为您在ELSE IF

中尝试进行错误的比较
         else if(args.toString().matches("-no_patching"))

args.toString()会给出参数数组args的一些地址值。如果将其与“-no_patching”参数进行比较,则肯定会返回FALSE。

相反,你可以像

一样进行比较
         else if(args[0].toString().matches("-no_patching"))