简体   繁体   English

jar包和class包之间有什么区别?

[英]What's the difference between package with jar and package with classes?

I know that in java there's two method of packaging one application. 我知道在Java中有两种包装一个应用程序的方法。 One way is extracting all its dependencies's classes and package them with your classes into a large jar. 一种方法是提取其所有依赖项的类,并将它们与您的类一起打包到一个大的jar中。 Another way is that copy all its dependency to lib folder, and package them with your own classes into a large jar. 另一种方法是将其所有依赖项复制到lib文件夹,并将它们与您自己的类一起打包到一个大jar中。 What's difference between these two packaging method ? 这两种包装方法有什么区别?

Thanks 谢谢

I don't agree that either of these is a standard method. 我不同意这两种方法都是标准方法。 The standard method is to distribute as many .JAR files as necessary, in whatever directory structure is necessary. 标准方法是根据需要的目录结构分发尽可能多的.JAR文件。

book.xml
<book>
<person>
  <first>Kiran</first>
  <last>Pai</last>
  <age>22</age>
</person>
<person>
  <first>Bill</first>
  <last>Gates</last>
  <age>46</age>
</person>
<person>
  <first>Steve</first>
  <last>Jobs</last>
  <age>40</age>
</person>
<person>
  <first>kunal</first>
  <last>kumar</last>
  <age>25</age>
</person>
</book>

create a xml file book.xml
made a jar file book.xml.jar and 
palce it in war/web-inf/lib folder of your project..
 then it will work..

code to parse xml file in same package in java 代码以解析Java中同一包中的xml文件

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.http.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;




import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

@SuppressWarnings("serial")
public class XMLParser extends HttpServlet {
    InputStream istream =getClass().getResourceAsStream("/book.xml");
    public void doGet(HttpServletRequest req, HttpServletResponse resp)throws IOException
    {

        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = null;
        try {
            docBuilder = docBuilderFactory.newDocumentBuilder();
        } catch (ParserConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
      Document doc = null;
    try {
        doc = docBuilder.parse (istream);
    } catch (SAXException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

        // normalize text representation
        doc.getDocumentElement ().normalize ();
        System.out.println ("Root element of the doc is " + 
             doc.getDocumentElement().getNodeName());


        NodeList listOfPersons = doc.getElementsByTagName("person");
        int totalPersons = listOfPersons.getLength();
        System.out.println("Total no of people : " + totalPersons);

        for(int s=0; s<listOfPersons.getLength() ; s++){


            Node firstPersonNode = listOfPersons.item(s);
            if(firstPersonNode.getNodeType() == Node.ELEMENT_NODE){


                Element firstPersonElement = (Element)firstPersonNode;

                //-------
                NodeList firstNameList = firstPersonElement.getElementsByTagName("first");
                Element firstNameElement = (Element)firstNameList.item(0);

                NodeList textFNList = firstNameElement.getChildNodes();
                System.out.println("First Name : " + 
                       ((Node)textFNList.item(0)).getNodeValue().trim());

                //-------
                NodeList lastNameList = firstPersonElement.getElementsByTagName("last");
                Element lastNameElement = (Element)lastNameList.item(0);

                NodeList textLNList = lastNameElement.getChildNodes();
                System.out.println("Last Name : " + 
                       ((Node)textLNList.item(0)).getNodeValue().trim());

                //----
                NodeList ageList = firstPersonElement.getElementsByTagName("age");
                Element ageElement = (Element)ageList.item(0);

                NodeList textAgeList = ageElement.getChildNodes();
                System.out.println("Age : " + 
                       ((Node)textAgeList.item(0)).getNodeValue().trim());

                //------


            }//end of if clause


        }//end of for loop with s var

    //System.exit (0);

}//end of main


    }

Well actually the official Java way to package an executable jar is to have all its other jar dependencies placed outside it, in a folder that is then declared as being in the classpath (either in the manifest of the executable jar, or as command line arguments when running the executable jar). 好的,实际上,打包可执行jar的官方Java方法是将所有其他jar依赖项放在它的外部,放在一个文件夹中,该文件夹然后声明为在类路径中(在可执行jar的清单中,或者作为命令行参数)运行可执行jar时)。

There are tools that try to "stuff in" your classpath dependencies inside your jar file. 有一些工具可以尝试“填充” jar文件中的类路径依赖项。 They do this by either unpackaging all jar files (which are really zip files with a different extension) and then repacking all their contents in one jumbo jar. 他们通过解压缩所有jar文件(实际上是具有不同扩展名的zip文件),然后将其所有内容重新打包到一个巨型jar中来实现此目的。 Or they add the dependencies jar as-is to the interior of the executable jar. 或者他们将依赖关系jar按原样添加到可执行jar的内部。 Maven has a plugin that does both these things, and Eclipse can do them too from the go (when you export your project as a runnable jar). Maven有一个可以完成这两项工作的插件,Eclipse也可以随时随地(当将项目导出为可运行的jar时)完成它们。

I prefer the version in which the actual jars are packaged inside your jar. 我更喜欢将实际罐子包装在罐子中的版本。 First off it's more proper as file organisation goes, and second you don't run the risk of having two classes with the same fully qualified name which were initially placed in different jars now being in conflict (effectively one will overwrite the other). 首先,随着文件组织的发展,它更合适,其次,您不必冒两个具有相同完全限定名称的类的风险,这些类最初放置在不同的jar中,现在却发生冲突(有效地覆盖另一个)。 What's more, from the classpath point of view, having multiple jar files in the classpath, each with their own class files is NOT the same as having all thse classes placed directly in the classpath, so adding your resources inside your executable jar as jars (and not as class files) goes more towards resepcting that. 而且,从类路径的角度来看,在类路径中有多个jar文件,每个文件都有自己的类文件,与将所有这些类直接放置在类路径中不同,因此将您的可执行文件中的资源作为jar添加(而不是作为类文件)更倾向于重新分配。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “包装”和“模块”之间有什么区别? - What's the difference between “package” and “module”? OSGi:Import-Package / Export-Package和Require-Capability / Provide Capability之间有什么区别? - OSGi: What's the difference between Import-Package/Export-Package and Require-Capability/Provide Capability? 库、存储库、包和文件夹之间有什么区别? - What is the difference between a library, respository, package and folder? jaxws-rt.jar中的“ / com / sun / xml / ws /”软件包和rt.jar中的“ / com / sun / xml / internal / ws /”软件包之间有什么区别 - what is the Difference between the “/com/sun/xml/ws/” package in jaxws-rt.jar and “/com/sun/xml/internal/ws/” package inside rt.jar 这两个jar文件有什么区别? - What's difference between these two jar files? Java 6的Rhino内置版本和Mozilla直接使用的Rhino软件包有什么区别? - What's the difference between Java 6's built-in version of Rhino and the Rhino package direct from Mozilla? class 和 package 之间的区别 - Difference between class and package apache.felix.dm包中的init()和start()方法有什么区别? - what's the difference between init() and start() method in apache.felix.dm package? AccessLevel.PACKAGE 和 AccessLevel.MODULE 有什么区别? - What is the difference between AccessLevel.PACKAGE and AccessLevel.MODULE? osgi中bootdelegation和DynamicImport-Package有什么区别 - What is the difference between bootdelegation and DynamicImport-Package in osgi
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM