Eclipse 中的 jar file 和 runnable jar file的区别。怎么打jar包 jar怎么运行

    java最难学的就是角落的东东了,不愧叫做java平台。搜索了一把总算明白了。 

     直接运行 .class的方法     java  -cp .  com.network.Chat  , 不用加.class后缀 

    从eclipse中export 的jar file,仅仅是把.class打包了。所以执行这种jar file需要用 java -cp .;ch04.jar com.thnkjava.ch04, 如果你还应用到另外的lib库,你必须在cp里说明,也就是  java -cp .;ch04.jar;lib01.jar;lib02.jar com.thnkjava.ch04 来执行。事实上可以发现ch04.jar也在cp里面,说明ch04.jar就是lib库,最后的参数指明了要被执行的类名。

    如果从eclipse里export出的是 runnable jar file,那么个执行这个jar包的时候是不需要指明哪个类的,直接这样执行 java -jar ch04.jar。原因就是jar包中的MANIFEST.MF内容不同。 runnable jar包中指明哪个类先执行,所以你可以用 java -jar ch04.jar来执行你想要执行的代码,而不必指明具体哪个类。这个你可以打开 jar包查看MANIFEST.MF的区别,一目了然。

     生成runnable jar file时,有两个选项,Extract required libraries into generated JAR 和 package equired libraries into generated JAR。 前者是把你用到的.class 文件提取出来,后者则是把你所需要的所有jar包都打进一个包里。两者的MANIFEST.MF文件内容也有所不同,这应该是eclipse造成的,IDE 做了自己的事情,具体就不研究了。

http://docs.oracle.com/javase/tutorial/deployment/jar/appman.html

 

怎么打jar包 jar怎么运行

 

一、怎么打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

 

分享到:    
评论
10 楼   liubiaocai   2012-03-02  
感谢LZ,终于搞定了eclipse导出jar,并且在dos里面可以运行了
9 楼   lk617238688   2011-01-25  
8 楼   lk617238688   2011-01-25  
/**  
* 遍历目录  
*  
* @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[i].isDirectory()) {  
read(files[i].getAbsolutePath(), xmlPath);  
} else {  
// read file  
readfile2Xml(files[i].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[0], args[1]);  
start("F:\\works\\updown\\functions",  
"F:\\works\\Java\\workspace\\txtfmtxml\\txtfmtxml.xml");  
}  
}
7 楼   lk617238688   2011-01-25  
/**  
* 读取文件 每行生成一个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[@name='" + fileName + "']/count", null,  
counts, xmlPath);  
editCount("//summary/module[@name='" + fileName + "']/size", null,  
sizes, xmlPath);  
editCount("//detail/module[@name='" + fileName + "']", "count",  
counts, xmlPath);  
editCount("//detail/module[@name='" + fileName + "']", "size",  
sizes, xmlPath);  
reader.close();  
read.close();  
} catch (Exception e) {  
e.printStackTrace();  
try {  
reader.close();  
read.close();  
} catch (IOException e1) {  
}  
}  

}
6 楼   lk617238688   2011-01-25  
/**  
* 添加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);  
}
5 楼   lk617238688   2011-01-25  
/**  
* 文件处理  
*  
* @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[@name='"  
+ moduleName + "']");  
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);  
}
4 楼   lk617238688   2011-01-25  
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);  
}
3 楼   lk617238688   2011-01-25  
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  
**/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值