我已经领先于自己了,正在上一个学期,这要求我掌握超出我所了解的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结果就好。 但是当我尝试我的代码时,运行了“无参数异常”,我认为这是告诉我没有参数传递。
可能是什么问题?我真的希望这是足够的细节; _;
你能展示一个你用来运行你的程序的示例命令行参数列表吗? – Eran 2014-11-03 17:11:41
args是您可以调用主要方法的参数!如果你使用的是eclipse,请选择“运行配置”来为你的启动添加一些参数! – Michael 2014-11-03 17:12:33