Maven: NoClassDefFoundError of system scoped dependency - java

I am developing an Android project with Maven. I have a third-party jar file that I have included in a lib folder on my project root directory:
<dependency>
<groupId>com.parse</groupId>
<artifactId>parse</artifactId>
<version>1.9.2</version>
<scope>system</scope>
<systemPath>${project.basedir}\lib\Parse-1.9.2.jar</systemPath>
</dependency>
But when I install the apk in my phone, I get NoClassDefFoundError. Obviously, that class exists inside the jar.
How can I do?
Thanks.

I would suggest using one of your directories as a repository, add your jar in it, and load it the proper Maven way.
You can do this adding this to your pom.xml and putting your jar in /lib:
<repositories>
<repository>
<id>mylibid</id>
<url>file://${project.basedir}/lib</url>
</repository>
</repositories>
...
<dependencies>
...
<dependency>
<groupId>com.parse</groupId>
<artifactId>parse</artifactId>
<version>1.9.2</version>
</dependency>
...
</dependencies>

Add my two cents here, you can install the third-party into your local repository firstly and try add dependency directly.
Take your scenario as an example. Change to the folder where contains the third jar and type:
mvn install:install-file -DgroupId=com.parse -DartifactId=parse -Dversion=1.9.2 -Dpackaging=jar -Dfile=Parse-1.9.2.jar
Modify your pom.xml to add the dependency.
<dependencies>
...
<dependency>
<groupId>com.parse</groupId>
<artifactId>parse</artifactId>
<version>1.9.2</version>
</dependency>
...
</dependencies>

Related

How to resolve a Maven dependency of type pom?

I am trying to import a library in Maven which is Kotlin Multiplatform.
This is it's Github repo (does not actually matter much of course)
Point is, it says it can be imported in with this dependency for Gradle:
dependencies {
implementation("com.github.ajalt.colormath:colormath:2.0.0")
}
Obviously, just converting it to Maven does not work:
<dependencies>
...
<dependency>
<groupId>com.github.ajalt.colormath</groupId>
<artifactId>colormath</artifactId>
<version>2.0.0</version>
<type>jar</type>
</dependency>
</dependencies>
So I added it's pom to the project as this answer explains it:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.github.ajalt.colormath</groupId>
<artifactId>colormath</artifactId>
<version>2.0.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
This dependency is resolved (it can be only resolved as pom type. Then I add it as dependency:
<dependencies>
...
<dependency>
<groupId>com.github.ajalt.colormath</groupId>
<artifactId>colormath</artifactId>
<version>2.0.0</version>
<type>jar</type>
</dependency>
</dependencies>
It still cannot find it.
I also added the repo1, because I did not see where Maven was looking for this artifact:
<repositories>
<repository>
<id>central</id>
<name>Central Repository</name>
<url>https://repo1.maven.org/maven2</url>
</repository>
</repositories>
But still no success. I don't understand why it's not working.
The code can be found in the repo1, but Maven does not resolve it.
This must be a very simple thing I am not understanding here, please help.
The link to the Maven Central from GitHub points to a bare POM artifact, without any dependencies and/or attached JARs.
It looks like Kotlin MP uploads JVM-specific artifacts with a different artifactId - colormath-jvm in this case. Please check corresponding directory in the Maven Central.
I suggest using following dependency declaration in the POM:
<groupId>com.github.ajalt.colormath</groupId>
<artifactId>colormath-jvm</artifactId>
<version>2.0.0</version>

Missing package for org.apache.http, JAR can't be found

I need the org.apache.http package, where i have tried to use the find JAR on web function built inside intelliJ, but no matter, what jar i try to get, it all returns 404 (is this because the package is depricated?)
how is the error i get when i try:
Is there any way to get this jar package?
Try downloading it from maven repositories
https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore/4.4.12
If it's a maven project, specify in your pom.xml the repository. And add the dependencies to apache.
<repositories>
<repository>
<id>my-repo</id>
<url>https://my repo.com</url>
</repository>
</repositories>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.12</version>
</dependency>

Unable to add IBM db2jcc4 jar file through Maven

I am trying to access DB2 tables in a java project. I am able to access the tables when I manually added the jar files - db2jcc4.jar and db2jcc_license_cisuz.jar. No issues in accessing the tables.
But when I try to add these jar files through Maven, they won't add to the project.
<dependency>
<groupId>com.ibm.db2</groupId>
<artifactId>db2jcc4</artifactId>
<version>9.7.0.4</version>
</dependency>
Error Message - Missing artifact id.
Also, the latest db2jcc4.jar files (Version 11.1) are not present in Maven repository. Is there any other place I can access it from?
You have to download the right driver from IBM.
http://www-01.ibm.com/support/docview.wss?uid=swg21363866
Then install it to your local maven repository
http://maven.apache.org/plugins/maven-install-plugin/install-file-mojo.html
As written in maven central repository the artifact is in another repo.
Add it to your pom and it will work.
<repositories>
<repository>
<id>Alfresco</id>
<name>Alfresco</name>
<url>https://artifacts.alfresco.com/nexus/content/repositories/public/</url>
</repository>
</repositories>
Assuming that , using share drive is the option to go.
<dependency>
<groupId>com.ibm.db2.jcc</groupId>
<artifactId>licences</artifactId>
<version>0.7</version> <!-- Adjust this properly -->
<scope>system</scope>
<systemPath>R:\JDBC drivers\IBM DB2_db2_2.2.0.v20130525_0720\db2jcc_license_cisuz.jar</systemPath>
</dependency>
According to maven central repository the artifact is in another repository. Include these two in your pom.xml and it should work:
<dependency>
<groupId>com.ibm.db2.jcc</groupId>
<artifactId>db2jcc4</artifactId>
<version>10.1</version>
</dependency>
<repositories>
<repository>
<id>com.ibm.db2.jcc</id>
<url>https://artifacts.alfresco.com/nexus/content/repositories/public/</url>
</repository>
</repositories>

Optional system dependency causes "invalid, transitive dependencies (if any) will not be available"

I have a main project and a commons project which has a dependency that is not in any repository and needs to be included using <scope>system</scope>.
Because the system dependency is defined using a relative url using a maven property ${project.basedir}, I made it <optional>true</optional> so that it doesn't bother other projects and so depending projects need to redefine this dependency with the correct path.
Commons pom.xml:
<dependency>
<groupId>thirdparty-group</groupId>
<artifactId>artifact</artifactId>
<version>1.4.2</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/dependency.jar</systemPath>
<optional>true</optional>
</dependency>
In main project's pom:
<dependency>
<groupId>my-group</groupId>
<artifactId>commons</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>thirdparty-group</groupId>
<artifactId>artifact</artifactId>
<version>1.4.2</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/dependency.jar</systemPath>
</dependency>
Obviously dependency.jar is in both the commons and main project.
The commons jar installs properly without a hitch. But when used in the main project however, the result is:
[WARNING] The POM for my-group:commons:jar:1.0-SNAPSHOT is invalid, transitive dependencies (if any) will not be available: 1 problem was encountered while building the effective model for my-group:commons:1.0-SNAPSHOT
[ERROR] 'dependencies.dependency.systemPath' for thirdparty-group:artifact:jar must specify an absolute path but is ${project.basedir}/lib/dependency.jar #
The build continues, but transitive runtime dependencies are now excluded breaking the application.
Why is maven complaining about a dependency from commons that is not even relevant to the main project (as it is optional, why is it even included as transitive dependency)?
How to work around this problem?
Putting the system dependency in the repo is not an option unfortunately.
Going by #PascalThivent's excellent answer, I worked around the issue by defining a local repo inside the commons project, and then changing the scope to compile with <optional>true</optional>.
In commons pom.xml:
<dependencies>
<dependency>
<groupId>thirdparty-group</groupId>
<artifactId>artifact</artifactId>
<version>0.0.1</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
</dependencies>
<repositories>
<repository>
<id>local-repo</id>
<url>file://${basedir}/lib/local-repo</url>
</repository>
</repositories>
To install the library:
mvn org.apache.maven.plugins:maven-install-plugin:2.3.1:install-file \
-Dfile=./lib/dependency.jar \
-DgroupId=thirdparty-group \
-DartifactId=artifact \
-Dversion=0.0.1 \
-Dpackaging=jar \
-DlocalRepositoryPath=.\lib\local-repo
I can just commit this to the VCS directly so other developers don't have to do this every time on checkout.
In the main project's pom.xml nothing changes. It can still use its own system dependency, or go this approach as well. Both approaches work for the WAR file that comes out there...

Why is Maven not finding this jar

I have the following JAR that my project needs so I added it to my pom.xml...
<dependency>
<groupId>org.reficio</groupId>
<artifactId>soap-builder</artifactId>
<version>1.0.0-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>org.apache.ws.commons.schema</groupId>
<artifactId>XmlSchema</artifactId>
</exclusion>
</exclusions>
</dependency>
But when I run Maven it tries to download from various repos and finally fails. It only creates a "lastUpdated" file whose contents are below..
http\://repository.jboss.org/nexus/content/groups/public-jboss/.lastUpdated=1392941827625
http\://www.terracotta.org/download/reflector/releases/.lastUpdated=1392941828195
Does anyone have any suggestions? Is this JAR simply not there anymore?
Thanks.
That artefact isn't in Maven Central (or other default repo) and so Maven won't find it automatically. The reficio/soap-ws page on Github explains:
soap-ws is not yet located in the central maven repo, thus you also have to add an additional repository to your config.
<repositories>
<repository>
<id>reficio</id>
<url>http://repo.reficio.org/maven/</url>
</repository>
</repositories>

Categories