0

I'm trying to install and use 3rd Party Jars in my Maven project.
The first Jar is:

anthill3-client.jar
And this is his structure:

├───com
│   └───urbancode
│       ├───anthill3
│       │   ├───command
│       │   ├───custom
│       │   ├───dashboard
│       │   ├───domain
│       │   └───wsviewer
│       ├───codestation2
│       │   ├───domain
│       │   └───server
│       ├───commons
│       │   ├───db
│       │   ├───logfile
│       │   └───util
│       ├───license
│       ├───persistence
│       │   └───collections
│       └───scripting
└───META-INF

I installed it by running this command:

call mvn install:install-file -Dfile=anthill3-client.jar -DgroupId=com.urbancode -DartifactId=anthill3-client -Dversion=1.0 -Dpackaging=jar

Configured it in the pom.xml:

<dependency>
  <groupId>com.urbancode</groupId>
  <artifactId>anthill3-client</artifactId>
  <version>1.0</version>
</dependency>

And was able to use it in my code by importing some classes:

import com.urbancode.anthill3.domain.persistent.PersistenceException;
import com.urbancode.anthill3.domain.security.AuthorizationException;
import com.urbancode.anthill3.main.client.AnthillClient;
import com.urbancode.anthill3.persistence.UnitOfWork;
import com.urbancode.anthill3.domain.project.*;

It didn't quite work though, cause I get .ClassNotFoundException:

Exception in thread "main" java.lang.NoClassDefFoundError: com/urbancode/devilfish/services/method/MethodCall
    at Valor.CM.Tools.App.main(App.java:26)
Caused by: java.lang.ClassNotFoundException: com.urbancode.devilfish.services.method.MethodCall
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 1 more

I tried to fix it by installing the missing Jar:

devilfish.jar
His structure:

├───com
│   └───urbancode
│       ├───command
│       │   ├───shell
│       │   └───var
│       ├───commons
│       │   └───util
│       ├───devilfish
│       │   ├───client
│       │   ├───common
│       │   ├───server
│       │   └───services
│       └───plugin
└───META-INF

Installed it by running this command:

call mvn install:install-file -Dfile=devilfish.jar -DgroupId=com.urbancode -DartifactId=devilfish -Dversion=1.0 -Dpackaging=jar

And added to my pom:

<dependency>
  <groupId>com.urbancode</groupId>
  <artifactId>devilfish</artifactId>
  <version>1.0</version>
</dependency>

But importing classes from the Package simply doesn't work, and I keep getting the same exception. I guess it happens cause I didn't properly install the Jars. I filled the -DgroupId & -DartifactId based on what made sense to me, but I'm not sure I filled the right values.

Alex Weitz
  • 3,199
  • 4
  • 34
  • 57
  • So you are getting a run time exception, not a compile time error ? This is probably sue to the jar not being on the runtime classpath – jr593 Jun 14 '17 at 11:02
  • Yes, it's a run time exception. It fails exactly on the line where I'm using a class from the `com.urbancode.anthill3.main.client.AnthillClient` class. – Alex Weitz Jun 14 '17 at 11:07

2 Answers2

0

Not sure how you are running your project, but it's ok on compilation time, and fails of run time so I guess you are not building you jar right.

In order you pack your jar with dependencies, follow this: How can I create an executable JAR with dependencies using Maven?

<build>
 <plugins>
  <plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
      <archive>
        <manifest>
          <mainClass>fully.qualified.MainClass</mainClass>
        </manifest>
      </archive>
      <descriptorRefs>
        <descriptorRef>jar-with-dependencies</descriptorRef>
      </descriptorRefs>
    </configuration>
  </plugin>
 </plugins>
</build>
Yoniseg
  • 1
  • 1
  • yes, that's OK, if you can use it's API it mean that you have it. the problem is on run time when the app looks for the 3rd party jar but can't find it. Try to build it differently – Yoniseg Jun 14 '17 at 11:14
  • It's better if you can add all the necessary information in the answer itself. Adding link is not recommended since they can be broken or content can be changed later. – Dinidu Hewage Jun 14 '17 at 11:25
0

Open the devilfish.jar (with a zipping tool) and move to META-INF/maven/[several-packages]/pom.xml. Open its pom.xml and look up the right group, artifact name and version. Then install it again with appropriate settings. If urbancode is your company, install an Artifactory or Nexus server for you company, post the files there and add this server to your pom.xml.

Stefan
  • 12,108
  • 5
  • 47
  • 66
  • The META-INF doesn't contain a pom.xml. only MANIFEST.MF with this content: `Manifest-Version: 1.0` `Ant-Version: Apache Ant 1.9.7` `Created-By: pxa6460sr16-20140418_01 (SR16) (IBM Corporation)` I don't think the Jars were build with Maven – Alex Weitz Jun 14 '17 at 11:14
  • Is there no "maven" folder in it? Should be inside this folder structurue. If not its not a Maven artifact. – Stefan Jun 14 '17 at 11:17
  • It's not a Maven artifact. But somehow I was able to call a method from one of them – Alex Weitz Jun 14 '17 at 11:18
  • Is it even possible to use a Jar in maven that wasn't built with Maven? – Alex Weitz Jun 14 '17 at 11:50
  • Please see here: https://stackoverflow.com/questions/364114/can-i-add-jars-to-maven-2-build-classpath-without-installing-them – Stefan Jun 20 '17 at 07:22