ITeye博客

怎么打jar包 jar怎么运行

java_beginner_liyun · 2010-08-13 15:30 · java综合 · 编程语言 · Markdown

一、怎么打jar包

第一步:选中要打jar包的工程

第二步:鼠标右击,选择Export...

第三步:选择java中的Runnable JAR file(如图)


点击“Next” 

第四步:(1)在Launch configuration:选择要打jar包的mian所在的类名;(如图)


 

            (2)在Export destination:选择要存放jar的名称和地址(如图)

 

      (3)如果要打的jar包需要调用别的jar包 请选择Library handling:中copy required libraries into a sub-folder next to the generated JAR(如图)


 第五步:点击 “Finish”

二、运行 jar 方法

(1)cmd

(2)java -jar 盘:\文件名\XXXX.jar

评论(11)

  1. #11 楼 · zhoulangqin · 2014-11-07 11:17

    请问,如果我里面写成循环执行,执行jar包,那要怎么结束

  2. #10 楼 · liubiaocai · 2012-03-02 23:13

    感谢LZ,终于搞定了eclipse导出jar,并且在dos里面可以运行了

  3. #9 楼 · lk617238688 · 2011-01-25 16:14

    :wink:

  4. #8 楼 · lk617238688 · 2011-01-25 16:14

    /** * 遍历目录 * * @param directoryPath / public static void read(String txtPath, String xmlPath) { File dir = new File(txtPath); File files = dir.listFiles(); if (files == null) { return; } else { for (int i = 0; i < files.length; i++) { if (files.isDirectory()) { read(files*.getAbsolutePath(), xmlPath); } else { // read file readfile2Xml(files*.getAbsolutePath(), xmlPath); } } } }

    /**
     * start()
     * 
     * @param txtPath
     * @param xmlPath
     * @throws FileNotFoundException
     * @throws IOException
     */
    private static void start(String txtPath, String xmlPath)
    		throws FileNotFoundException, IOException {
    	// 创建XML
    	bulidXmlDoc(xmlPath);
    	// 遍历
    	read(txtPath, xmlPath);
    }
    
    public static void main(String args) throws FileNotFoundException,
    		IOException {
    	// start(args, args);
    	start("F:\\works\\updown\\functions",
    			"F:\\works\\Java\\workspace\\txtfmtxml\\txtfmtxml.xml");
    }
    

    }

  5. #7 楼 · lk617238688 · 2011-01-25 16:13

    /** * 读取文件 每行生成一个Element * * @param file */ private static void readfile2Xml(String file, String xmlPath) { String fileName = file.substring(file.lastIndexOf("\") + 1, file .lastIndexOf(".")); String func = null; String funcName = null; String funcSize = null; InputStreamReader read = null; BufferedReader reader = null; counts = 0; sizes = 0; try { // 添加summary addSummary(fileName, xmlPath); // 添加detail addElement(fileName, null, null, xmlPath);

    		read = new InputStreamReader(new FileInputStream(file), "GBK");
    		reader = new BufferedReader(read);
    		String line = null;
    		while ((line = reader.readLine()) != null) {
    			if ("".equals(line)) {
    				continue;
    			}
    			counts++;
    			func = line.split(" ");
    			funcName = null;
    			funcSize = null;
    			for (String str : func) {
    				if (str.length() != 0) {
    					if (funcName == null) {
    						funcName = str;
    					} else {
    						funcSize = str;
    						break;
    					}
    				}
    			}
    			if (funcSize == null) {
    				funcSize = "0";
    			}
    			sizes += Integer.parseInt(funcSize);
    			// 添加func
    			addElement(fileName, funcName, funcSize, xmlPath);
    		}
    		// 修改count
    		editCount("//summary/module/count", null,
    				counts, xmlPath);
    		editCount("//summary/module/size", null,
    				sizes, xmlPath);
    		editCount("//detail/module", "count",
    				counts, xmlPath);
    		editCount("//detail/module", "size",
    				sizes, xmlPath);
    		reader.close();
    		read.close();
    	} catch (Exception e) {
    		e.printStackTrace();
    		try {
    			reader.close();
    			read.close();
    		} catch (IOException e1) {
    		}
    	}
    
    }
    
  6. #6 楼 · lk617238688 · 2011-01-25 16:13

    /** * 添加Summary * * @param moduleName * @param countTotal * @throws IOException * @throws JDOMException */ private static void addSummary(String moduleName, String xmlPath) throws JDOMException, IOException {

    	SAXBuilder build = new SAXBuilder();
    	// 获得文档对象
    	Document doc = build.build(xmlPath);
    
    	XPath xpath = XPath.newInstance("//summary");
    	List list = xpath.selectNodes(doc);
    
    	Element summary = (Element) list.get(0);
    	Element module = new Element("module");
    
    	module.setAttribute("name", moduleName);
    	Element count = new Element("count");
    	count.setText(counts.toString());
    	Element size = new Element("size");
    	size.setText(sizes.toString());
    
    	module.addContent(count);
    	module.addContent(size);
    	summary.addContent(module);
    
    	// 文件处理
    	output(doc, xmlPath);
    
    }
    
    /**
     * 修改counts、sizes
     * @param Xpath
     * @param attribute
     * @param total
     * @param xmlPath
     * @throws FileNotFoundException
     * @throws IOException
     * @throws JDOMException
     */
    private static void editCount(String Xpath, String attribute,
    		Integer total, String xmlPath) throws FileNotFoundException,
    		IOException, JDOMException {
    
    	SAXBuilder build = new SAXBuilder();
    	// 获得文档对象
    	Document doc = build.build(xmlPath);
    	// 获得根节点
    	Element element = null;
    	// 添加子元素func
    	XPath xpath = XPath.newInstance(Xpath);
    	List list = xpath.selectNodes(doc);
    	if (list.size() == 1) {
    		element = (Element) list.get(0);
    		if (Xpath.contains("summary")) {
    			element.setText(total.toString());
    		} else {
    			element.setAttribute(attribute, total.toString());
    		}
    	}
    	// 文件处理
    	output(doc, xmlPath);
    }
    
  7. #5 楼 · lk617238688 · 2011-01-25 16:11

    /** * 文件处理 * * @param doc * @throws IOException * @throws FileNotFoundException */ private static void output(Document doc, String xmlPath) throws FileNotFoundException, IOException { // 格式化并输出 Format format = Format.getPrettyFormat(); XMLOutputter out = new XMLOutputter(format); out.output(doc, new FileOutputStream(xmlPath)); }

    /**
     * 添加detail
     * 
     * @param line
     * @throws IOException
     * @throws JDOMException
     */
    public static void addElement(String moduleName, String funcName,
    		String funcSize, String xmlPath) throws JDOMException, IOException {
    	SAXBuilder build = new SAXBuilder();
    	// 获得文档对象
    	Document doc = build.build(xmlPath);
    	XPath xpdetail = XPath.newInstance("//detail");
    	List listDetail = xpdetail.selectNodes(doc);
    	Element detail = (Element) listDetail.get(0);
    	Element module = null;
    	if (funcName == null) {
    		// 添加父元素 module
    		module = new Element("module");
    		module.setAttribute("name", moduleName);
    		module.setAttribute("count", counts.toString());
    		module.setAttribute("size", sizes.toString());
    		detail.addContent(module);
    	} else {
    		// 添加子元素func
    		XPath xpmodule = XPath.newInstance("//detail/module");
    		List list = xpmodule.selectNodes(doc);
    		if (list.size() == 1) {
    			module = (Element) list.get(0);
    
    			Element func = new Element("func");
    			func.setAttribute("name", funcName);
    			func.setAttribute("size", funcSize);
    			module.addContent(func);
    		}
    	}
    	// 文件处理
    	output(doc, xmlPath);
    }
    
  8. #4 楼 · lk617238688 · 2011-01-25 16:10

    public class TxtFmtXml { // 模块包含的函数个数 private static Integer counts = 0; // 模块所包含函数的大小 private static Integer sizes = 0;

    /**
     * 创建XML
     * 
     * @throws FileNotFoundException
     * @throws IOException
     */
    public static void bulidXmlDoc(String xmlPath)
    		throws FileNotFoundException, IOException {
    	// 创建根节点
    	Element root = new Element("root");
    	Element elementSummary = new Element("summary");
    	Element elementDetail = new Element("detail");
    
    	root.addContent(elementSummary);
    	root.addContent(elementDetail);
    	// 根节点添加到文档中
    	Document doc = new Document(root);
    
    	// 文件处理
    	output(doc, xmlPath);
    }
    
  9. #3 楼 · lk617238688 · 2011-01-25 16:10

    package com.huawei.txtfmtxml;

    import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.List;

    import org.jdom.Document; import org.jdom.Element; import org.jdom.JDOMException; import org.jdom.input.SAXBuilder; import org.jdom.output.Format; import org.jdom.output.XMLOutputter; import org.jdom.xpath.XPath; /** * *请提供jaxen.jar(XPath要用到)、jdom.jar **/

  10. #2 楼 · lk617238688 · 2011-01-25 16:09

    :wink:

  11. #1 楼 · lk617238688 · 2011-01-14 18:45

    把jar包复制到另一路径下再运行,看行不行?

评论功能维护中,当前仅展示历史评论。