首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从命令行传递array的参数,而不是java中的scanner

从命令行传递array的参数,而不是java中的scanner
EN

Stack Overflow用户
提问于 2015-05-06 23:16:18
回答 1查看 1.2K关注 0票数 1

我用Java编写代码,以便通过scanner获取参数。我有几个类: ChartToHtml.java、ExecutionProperties.java、ExecutionV2.java、TestCase.java、TestCaseList.java、Section.java,所有这些类都将从ImplTest.java调用。

当我从eclipse或命令行扫描程序执行时,它们都工作得很好。问题是当我想要通过程序参数执行它们,并在一行中传递参数时。它将输入视为单个String,但我必须使用String[]作为Section类的输入。

下面是我的Section类和ImplTest类

代码语言:javascript
运行
复制
public class Section {
     Ini.Section root;
     ArrayList<String> StringList = new ArrayList<String>();
     ArrayList<TestCase> TCList = new ArrayList<TestCase>();
     String sectionSearched;
     String section;
     String filenameSearched;

     public Section (){

     }

     public Section (String filenameSearched, String sectionSearched) {
         this.sectionSearched = sectionSearched;
         this.filenameSearched = filenameSearched;
     }

      public ArrayList<String> findSection(String filename, String... wanted) throws IOException, IOException {
             Ini myIni = new Ini(new File (filename));
            for (String d : wanted) {
                root = myIni.get(d);

                    for (String name : root.keySet()){
                        StringList.add(root.get(name));
                    }
              }
              return StringList;
      }     

      public ArrayList<TestCase> convertStringToTestCase(ArrayList<String>StringList){
          for ( int i = 0; i < StringList.size(); i++) {
              String name = StringList.get(i) ;
              TestCase tc = new TestCase (name);
              TCList.add(tc);
          }
          return TCList;
      }
      public String[] getSection(int NumOfSec){
          Scanner scanner = new Scanner(System.in);
          while (true) {
              System.out.println("Input section name:");
              section = scanner.nextLine();
              for (int i =0; i<NumOfSec; i++){
                  String token[]= section.split(" ");
                  System.out.println(Arrays.toString(token));
                    return token;
              }

          }
      }     
}

我的主类

代码语言:javascript
运行
复制
public class ImplTest {

public static void main(String[] args) throws IOException, ConfigurationException, TemplateException {
    ExecutionV2 ex = new ExecutionV2();
    TestCaseList tc = new TestCaseList();
    Section s = new Section();
    ChartToHtml chr= new ChartToHtml();
    ExecutionProperties ep = new ExecutionProperties();
    ImplTest imp = new ImplTest();

    String filename = ex.getcfg();
    String []sec = ex.getSection();
    int it = ex.getIterationMax();
    String runTCpath =ex.getRunTCdir();
    String dir = chr.getChartDir();

    ArrayList<TestCase> TClist = s.convertStringToTestCase(s.findSection(filename, sec));
    ex.executeTestCaseList(TClist, it , runTCpath);
    ex.getTCAttribute(TClist);

    ep.setConfigProperties(tc.getTCPassed(), tc.getTCFailed());
    chr.generateHistoryTable();
    chr.generateChartAndTableTemplate(tc.getTCPassed(), tc.getTCFailed(),ex.getNameList(), ex.getPassedList().toString(), ex.getItList().toString(),dir);
    }
}

然后我修改了主类,通过run configuration传递参数,并传递这一行:

代码语言:javascript
运行
复制
ArrayList<TestCase> TClist = s.convertStringToTestCase(s.findSection(**args[0]**, **args[1]**));
    ex.executeTestCaseList(TClist, Integer.parseInt(**args[2]**) ,  **args[3]**);
    ex.getTCAttribute(TClist);

    ep.setConfigProperties(tc.getTCPassed(), tc.getTCFailed());
    chr.generateHistoryTable();
    chr.generateChartAndTableTemplate(tc.getTCPassed(), tc.getTCFailed(),ex.getNameList(), ex.getPassedList().toString(), ex.getItList().toString(), **args[4]**);

并将这一行传递给程序参数

C:\\Users\\syuniyar\\.EasyTest\\4\\ccl\\config\\test2.cfg 12346 5 C:\\EasyTest\\4\\bin\\runTC.exe C:\\Users\\syuniyar\\EclipseWS\Test3\\chart.html

它工作得很好。但是,当我将输入从...12346...修改为...12346 12345...时,我得到这样的错误:

代码语言:javascript
运行
复制
Exception in thread "main" java.io.IOException: Cannot run program "5": CreateProcess error=2, The system cannot find the file specified

我也尝试使用VM参数,但System.getProperty()的选项仅适用于单个字符串。我知道为什么会出现这个错误,因为它将12345读作' it‘,这是不正确的。我想问的是:

在main方法中可以有一个数组作为单个参数吗?

EN

回答 1

Stack Overflow用户

发布于 2015-05-07 00:07:37

为了直接回答您的问题,“是否可以在main方法中使用数组作为单个参数?”,答案是否定的。main方法接受String对象数组(通常称为"args")形式的参数。这些字符串中的每一个都被视为一个参数。

从命令行执行main方法时,这些参数位于程序名之后,并以空格分隔。它们被加载到一个数组中,并传递给main方法。

如评论中所述(特别是@Ismail Kuruca),如果将多个字符串作为一个参数传递对您来说很重要,您可以连接这些字符串,使您的参数从技术上讲是一个字符串,从而被视为一个参数。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30080870

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档