文件添加到类路径

文件添加到类路径

本文介绍了如何将我的外部 jar 文件添加到类路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 maven 环境的新手,需要一些帮助.在 pom.xml

I am new to maven environment, need some ones help.Added my external jar file (directoryhelper.jar) in lib folder as below in pom.xml

<dependency>
      <groupId>com.test.directoryhelper</groupId>
      <artifactId>DirectoryHelper</artifactId>
      <version>1.0</version>
      <scope>system</scope>
      <systemPath>${basedir}/lib/directoryhelper.jar</systemPath>
</dependency>

编译成功,但在运行时我得到java.lang.NoClassDefFoundError.

compilation is successful, but during run time I am gettingjava.lang.NoClassDefFoundError.

如何将 directoryhelper.jar 添加到类路径.

how to add the directoryhelper.jar to class path.

推荐答案

Maven 开箱即用将提供一个 JAR 文件(默认打包).此 JAR 文件仅包含项目的 (main) 工件.如果您只使用它并运行它,很明显缺少依赖项 - 设计使然.

Maven out of the box will come up with a JAR file (default packaging). This JAR file only contains (main) artifacts of the project. If you take just that and run it, clearly the dependencies are missing -- by design.

通常,Maven 工件会与其 POM 结合重用,以便在使用时知道依赖项是什么.如果您使用 APK 并将它们安装在手机上,则可能有处理依赖项的机制,我仅从 Maven 的角度回答这个问题.

Typically Maven artifacts are reused in combination with their POM so that at the point of use it's know what the dependencies are. if you're using APKs and installing them on a phone, there may be mechanisms to deal with dependencies, I'm answering this merely from a Maven standpoint.

如果你想创建一个带有依赖项的 JAR,你必须告诉 Maven 这样做,这不是默认的.让 Maven 这样做的方法是(可能并不详尽):

If you want to create a JAR with dependencies you have to tell Maven to do so, that's not the default.Ways of having Maven do that are (probably not exhaustive):

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.4</version>
    <configuration>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
    </configuration>
...

  • Maven 阴影插件

    这篇关于如何将我的外部 jar 文件添加到类路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

  • 08-04 23:54