Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add my external jar file to class path

Tags:

maven

apk

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.

like image 688
prGD Avatar asked Aug 24 '13 04:08

prGD


People also ask

How do I include all JARs in a folder CLASSPATH?

In general, to include all of the JARs in a given directory, you can use the wildcard * (not *. jar ). The wildcard only matches JARs, not class files; to get all classes in a directory, just end the classpath entry at the directory name.


1 Answers

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

like image 173
Sander Verhagen Avatar answered Sep 29 '22 16:09

Sander Verhagen