【发布时间】: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,请检查“运行配置”以向您的启动添加一些参数!