【问题标题】:What Are My args[]?我的参数 [] 是什么?
【发布时间】:2014-12-30 09:43:08
【问题描述】:

我已经超越了自己,并且正在上一个学期,这要求我具备超出我所学知识的 Java 知识。我不知道如何将命令之类的参数传递给主参数,除了通过命令行的“echo Java foo bar” 我正在尝试通过 java 解析 JSON;希望下面的代码就够了

这是我的方法:

public static void parseCrewWithListMapping(String filePath) {
    ObjectMapper mapper = new ObjectMapper();

    try {
        List<Crew> crews = mapper.readValue(new File(filePath), 
                mapper.getTypeFactory().constructCollectionType(List.class, Crew.class));

        for(Crew crew : crews) {
            System.out.println(crew);
        }
    } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
    }
}

这是我尝试运行此方法的主要方法。

public class CrewParsing {
    public static void main(String[] args) 
            throws ParserConfigurationException, SAXException, IOException, XMLStreamException {

        if(args.length < 1)
             throw new RuntimeException("No argument exception");

        System.out.println("Parsing Crew Names");
        CrewParser.parseCrewWithListMapping(args[2]);
        System.out.println("Finished\n\n");

    }
}

我所有的文件都在正确的位置,我正在尝试重新创建:

public static void parseJSONWithListMapping(String filePath) {
    ObjectMapper mapper = new ObjectMapper();

    try {
        List<Employee> employees = mapper.readValue(new File(filePath), 
                mapper.getTypeFactory().constructCollectionType(List.class, Employee.class));

        for(Employee employee : employees){
            System.out.println(employee);
        }

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

方法:

public static void parseJSONWithListMapping(String filePath) {
    ObjectMapper mapper = new ObjectMapper();

    try {
        List<Employee> employees = mapper.readValue(new File(filePath), 
                mapper.getTypeFactory().constructCollectionType(List.class, Employee.class));

        for(Employee employee : employees){
            System.out.println(employee);
        }

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

主要方法(有效):

public class Practice1Test {
    public static void main(String[] args) 
            throws ParserConfigurationException, SAXException, IOException, XMLStreamException {

        if(args.length < 1)
             throw new RuntimeException("No argument exception");

        System.out.println("Parsing Crew Names");
        CrewParser.parseJSONWithListMapping(args[2]);
        System.out.println("Finished\n\n");

    }
}

在有效的代码中将args[2] 作为parseJSONWithListMapping 的参数传递时,我的json 结果很好。 但是当我尝试我的代码时,运行了“无参数异常”,我假设它告诉我没有传递任何参数。

可能是什么问题?我真的希望这是足够的细节;_;

【问题讨论】:

  • 您能否展示一个用于运行程序的示例命令行参数列表?
  • args 是您可以调用 main 方法的参数!如果您使用 Eclipse,请检查“运行配置”以向您的启动添加一些参数!

标签: java methods main


【解决方案1】:

当你从命令行运行程序时,你可以传入参数列表,例如:

java CrewParsing jsonPath1 jsonPath2 jsonPath3

如果您从 Eclipse 等 IDE 运行,您可能只是单击 RunDebug 而忘记输入任何参数。在 Eclipse 中 - 右键单击​​您的主类,当您将鼠标悬停在 Run(or Debug) as... 上时,选择 run(debug) configuration。有一个参数选项卡,您可以在那里输入它们。您从中复制的程序可能已经设置了它们。


如果您想了解有关参数的更多信息,我发现 Oracle command line tutorial 解释得很好:(全部取自链接)

Java 应用程序可以从命令行接受任意数量的参数。这允许用户在启动应用程序时指定配置信息。

用户在调用应用程序时输入命令行参数,并在要运行的类名之后指定它们。例如,假设名为 Sort 的 Java 应用程序对文件中的行进行排序。要对名为 friends.txt 的文件中的数据进行排序,用户需要输入:

java Sort friends.txt

启动应用程序时,运行时系统通过字符串数组将命令行参数传递给应用程序的 main 方法。在前面的示例中,命令行参数以包含单个字符串的数组形式传递给排序应用程序:“friends.txt”。

Echo 示例将其每个命令行参数单独显示在一行中:

public class Echo {
    public static void main (String[] args) {
        for (String s: args) {
            System.out.println(s);
        }
    }
}

以下示例显示了用户如何运行 Echo。用户输入以斜体显示。

java Echo Drink Hot Java

Drink
Hot
Java
//3 separate arguments are all printed out - args[0], args[1], args[2]

如果您仍然不确定自己实际有哪些论点,我会尝试按照教程向您展示的那样打印出您的论点。

【讨论】:

  • 如果您从 Eclipse 等 IDE 运行,您可能只是单击了运行或调试而忘记输入任何参数。在 Eclipse 中 - 右键单击​​您的主类,当您将鼠标悬停在 Run(或 Debug) as... 上时,选择 run(debug) 配置。有一个参数选项卡,您可以在那里输入它们。您从中复制的程序可能已经设置了它们。
【解决方案2】:

您正在检查args.length &lt; 1 并抛出“无参数异常”,但随后您尝试使用args[2],这是第三个命令行参数。

如果你想要第一个,那就是args[0],第二个是args[1],以此类推。如果你需要一个参数,你的args.length &lt; 1 是正确的,你会使用args[0]。如果您需要两个参数,您可以使用args.length &lt; 2 来表示错误,然后使用args[0]args[1]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-18
    • 1970-01-01
    • 2012-02-27
    • 2021-06-09
    • 2010-09-26
    • 2011-04-14
    • 2011-04-13
    • 2014-04-24
    相关资源
    最近更新 更多