Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
986 views
in Technique[技术] by (71.8m points)

maven - how to add my external jar file to class path

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>

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

how to add the directoryhelper.jar to class path.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

Typically Maven artifacts are reused in combination with their POM so that at the point of use it's know what the dependencies are. Edit: 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.

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):

  • Maven Assembly plugin, jar-with-dependencies predefined descriptor:

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...