简体   繁体   English

Jar:未找到类定义错误

[英]Jar : No Class Def Found Error

I am trying to build a project using Ant Build and i have referenced several jars to make it work. 我正在尝试使用Ant Build构建一个项目,并且我引用了多个jar使其工作。 Now When i put the jar created by Ant build in some other machine and run it. 现在,当我将由Ant构建的罐子放在其他机器上并运行它时。 I am getting the error saying NoClassDefFoundError org/apache... Not found . 我收到错误消息说NoClassDefFoundError org/apache... Not found Is there anyway to put all the referenced jars in the classpath of the project or in the manifest file? 无论如何,是否将所有引用的jar放在项目的类路径或清单文件中?
Or is there anyway to repackage all the jar in a project? 还是在项目中重新打包所有jar? I know there is one method using jarjar but I don't have any idea of how to use it. 我知道有一种使用jarjar的方法,但是我不知道如何使用它。
Please suggest me some ideas, I've been stuck with this small problem for a long time. 请给我提出一些想法,我已经在这个小问题上困扰了很长时间了。

When building with ANT the external jars you need are being added to the classpath by the ANT tool. 使用ANT进行构建时,您需要的外部jar将通过ANT工具添加到类路径中。 Look in your build script, you most likely have an entry either in the javac task or in a setup task that defines your classpath. 在构建脚本中查看,您很可能在javac任务或定义类路径的安装任务中都有一个条目。

After you build your code, your jar file only has your classes in it, the classes in the 3rd party jar files (like Apache) are not added to your jar file by default. 生成代码后,您的jar文件中仅包含您的类,默认情况下,第3方jar文件(例如Apache)中的类不会添加到您的jar文件中。

What you need to decide is do you want a single jar file with all needed classes or are you willing to deploy multiple jar files? 您需要决定的是要一个具有所有所需类的jar文件,还是愿意部署多个jar文件? If you're comfortable delivering your application as multiple jar files, you will want to provide a batch file or shell script to launch the application for the user that builds the classpath to include the deployed jars. 如果您可以将应用程序作为多个jar文件交付,则可以提供一个批处理文件或shell脚本来为构建类路径以包含已部署jar的用户启动该应用程序。

If you want one jar file, you can do something like below. 如果您需要一个jar文件,则可以执行以下操作。 Assume that all the 3rd party jars you have are in a directory identified by the ANT property lib.dir : 假设您拥有的所有第3方jar都在ANT属性lib.dir标识的目录中:

<jar destfile='${build.dir}/lib-jars.jar'>
  <zipgroupfileset dir="${lib.dir}">
    <include name="**/*.jar" />
  </zipgroupfileset>
</jar>
<sleep seconds='1'/>  <!-- avoid timestamp warnings -->

what this will do is create in your build.dir directory a single jar file named lib-jars.jar that contains all the classes from all of the 3rd party jars. 要做的是在build.dir目录中创建一个名为lib-jars.jar的 jar文件,其中包含来自所有第3方jar的所有类。 Understand that this will result in equivalent files (like MANIFEST.MF files) being overwritten if they exist in multiple jars and only the last one will be present. 了解这将导致等效文件(例如MANIFEST.MF文件)在多个jar中存在并且仅存在最后一个时被覆盖。

Once you have this new all-libs jar, you can then jar up your application classes and the contents of this all-libs jar into one jar: 有了这个新的全库jar之后,就可以将您的应用程序类和该全库jar的内容打包到一个jar中:

<jar destfile='${jar.file}' basedir='${classes.dir}'>
  <!-- using zipfileset we can filter on entries in the one file -->
  <zipfileset src='${build.dir}/lib-jars.jar'>
    <exclude name="META-INF/MANIFEST.MF"/>
  </zipfileset>
  <manifest>
    <attribute name="Built-By" value="${user.name}"/>
    <attribute name="Main-Class" value="${main.class}"/>
    <section name="common">
      <attribute name="Specification-Title" value="${project.title}"/>
      <attribute name="Specification-Version" value="${release.version}"/>
      <attribute name="Specification-Vendor" value="${vendor}"/>
      <attribute name="Implementation-Title" value="${project.title}"/>
      <attribute name="Implementation-Version" value="${release.version}"/> 
      <attribute name="Implementation-Vendor" value="${vendor}"/>
    </section>
  </manifest>      
</jar>

notice that I exclude the MANIFEST.MF file from the all-libs jar and create my own. 请注意,我从所有库jar中排除了MANIFEST.MF文件,并创建了自己的文件。 Then end result of this is a single jar file that includes all of the classes/property files/resources from all of the library jar files and your classes. 最终的结果是一个jar文件,其中包含所有库jar文件和您的类中的所有类/属性文件/资源​​。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM