如何将我的外部jar文件添加到类路径 [英] how to add my external jar file to class path

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

问题描述

我是Maven环境的新手,需要一些帮助. 在lib文件夹中添加了我的外部jar文件(directoryhelper.jar),如下所示: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 getting java.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 Shade插件

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

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆