CMD命令行运行带参数的jar

一.打包(此处用的是eclipse)

代码如下

  1 package com.example.Open;
  2 import java.io.File;
  3 import java.util.List;
  4 import org.dom4j.Document;
  5 import org.dom4j.DocumentException;
  6 import org.dom4j.Element;
  7 import org.dom4j.io.SAXReader;
  8 
  9 import net.sf.json.JSONArray;
 10 import net.sf.json.JSONObject;
 11 public class XmlToJson {
 12     
 13     /**
 14      * 
 15      * @author: jinzy
 16      * @date: 2018年7月18日下午5:39:20  
 17      * @description: 获取xml文件 
 18      * @param xmlName
 19      * @return    设定文件  
 20      * Document    返回类型
 21      */
 22     public static Document getXml(String xmlName) {
 23         String fileDir = System.getProperty("user.dir");
 24         String xmlFile = fileDir + "/reports/"+xmlName+".xml";
 25         System.out.println("xmlFile:"+xmlFile);
 26         File file = new File(xmlFile);
 27         SAXReader reader = new SAXReader();
 28         Document doc = null;
 29         try {
 30             doc = reader.read(file);
 31         } catch (DocumentException e) {
 32             // TODO Auto-generated catch block
 33             e.printStackTrace();
 34         }
 35         return doc;
 36     }
 37 
 38     /**
 39      * 
 40      * @author: jinzy
 41      * @date: 2018年7月18日下午5:40:01  
 42      * @description: 将xml转换为JSON对象
 43      * @param xmlName
 44      * @return
 45      * @throws Exception    设定文件  
 46      * JSONObject    返回类型
 47      */
 48     public static JSONObject xmltoJson(String xmlName) throws Exception {
 49         JSONObject jsonObject = new JSONObject();
 50         //获取根节点元素对象  
 51         Document document = getXml(xmlName);
 52         Element root = document.getRootElement();
 53         iterateNodes(root, jsonObject);
 54         return jsonObject;  
 55      }
 56     
 57     /**
 58      * 遍历元素
 59      * @param node 元素
 60      * @param json 将元素遍历完成之后放的JSON对象
 61      */
 62     public static void iterateNodes(Element node,JSONObject json){ 
 63         //获取当前元素的名称
 64         String nodeName = node.getName();
 65         //判断已遍历的JSON中是否已经有了该元素的名称
 66         if(json.containsKey(nodeName)){
 67             //该元素在同级下有多个
 68             Object Object = json.get(nodeName);
 69             JSONArray array = null;
 70             if(Object instanceof JSONArray){
 71                 array = (JSONArray) Object;
 72             }else {
 73                 array = new JSONArray();
 74                 array.add(Object);
 75             }
 76             //获取该元素下所有子元素 
 77             List<Element> listElement = node.elements();
 78             if(listElement.isEmpty()){
 79                 //该元素无子元素,获取元素的值
 80                 String nodeValue = node.getTextTrim();
 81                 array.add(nodeValue);
 82                 json.put(nodeName, array);
 83                 return ;
 84             }
 85             //有子元素
 86             JSONObject newJson = new JSONObject();
 87             //遍历所有子元素
 88             for(Element e:listElement){
 89                 //递归  
 90                 iterateNodes(e,newJson);
 91             }
 92             array.add(newJson);
 93             json.put(nodeName, array);
 94             return ;
 95         }
 96         //该元素同级下第一次遍历
 97         //获取该元素下所有子元素
 98         List<Element> listElement = node.elements();
 99         if(listElement.isEmpty()){
100             //该元素无子元素,获取元素的值
101             String nodeValue = node.getTextTrim();
102             json.put(nodeName, nodeValue);
103             return ;
104         }
105         //有子节点,新建一个JSONObject来存储该节点下子节点的值
106         JSONObject object = new JSONObject();
107         //遍历所有一级子节点  
108         for(Element e:listElement){
109             //递归  
110             iterateNodes(e,object);
111         }
112         json.put(nodeName, object);
113     
114     }
115     
116     /**
117      * 测试的main方法
118      */
119     public static void main(String[] args) throws Exception {
120             JSONObject jsonObject = xmltoJson(args[0]);
121             System.out.println(jsonObject);
122     }
123 }

 

(1)工程名右击,选择“Export”

(2)选择“Runnable JAR file”

(3)选择“Launch configuration”和jar要存放的路径

(4)点击“Finish”

二:运行

uacProdResult为参数,多个参数用空格隔开

 

posted @ 2018-07-18 17:37  mrjade  阅读(1227)  评论(0编辑  收藏  举报