NoClassDefFoundError as I run a Maven packaged JAR file - java

I am using Maven to compile and package a Java application from the command line:
mvn clean
mvn compile
mvn package
These commands run without a problem. But when I try to run the generated JAR file
java -jar target/JarFile.jar
I get the following error:
Error: Unable to initialize main class com.company.Main
Caused by: java.lang.NoClassDefFoundError: com/google/api/client/json/JsonFactory
Which is a class from an external dependency. These dependencies are also managed by Maven (i.e., I am not supplying them locally, but listing them in my POM file.) Since I am able to run the application inside the IDE, I assume I am missing a step to make the dependencies available to the JAR file. Here is my POM file:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<groupId>test-maven-project</groupId>
<artifactId>test</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</dependency>
<dependency>
<groupId>com.google.oauth-client</groupId>
<artifactId>google-oauth-client</artifactId>
<version>1.30.6</version>
</dependency>
<dependency>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client</artifactId>
<version>1.34.2</version>
</dependency>
<dependency>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client</artifactId>
<version>1.30.9</version>
</dependency>
<dependency>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client-jackson2</artifactId>
<version>1.34.2</version>
</dependency>
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-customsearch</artifactId>
<version>v1-rev20200401-1.30.9</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.company.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
Could you point me in the right direction so that the dependencies are accessible at runtime?

To run your jar from commandline, you must either explicitly specify the classpath on the command line or build an executable jar:
How can I create an executable JAR with dependencies using Maven?
BTW: mvn package already executes mvn compile, so you need not call mvn compile explicitly.

Use this in your pom.xml and rebuild your project.
<dependency>
<groupId>com.google.api.client</groupId>
<artifactId>google-api-client-json</artifactId>
<version>1.1.0-alpha</version>
</dependency>

Related

org.json.* is not accessible in JAVA Maven Project as a dependency

I am making a JavaFX project (for the GUI) on Maven and for the moment I want to convert a StringBuffer object to JSON.
I tried to add the dependency org.json from Maven but there is a problem in pom.xml file
Missing Artifact org.json:json:bundle:20220924
I tried adding an older version of org.json dependency, it had the same issue
I tried a different dependency, everything was ok in the pom.xml file but I could not import it into my project (any dependency).
All dependencies return this error: The type org.json.JSONArray is not accessible
Lastly, I tried to download the json-java.jar from github and add it into a libs folder on the base project directory and added it in the pom file as a dependency but it could not be resolved either.
My pom.xml file:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>books</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20220924</version>
<type>bundle</type>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>13</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>13</version>
</dependency>
<dependency>
<groupId>com.sample</groupId>
<artifactId>sample</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/libs/json-20220924.jar</systemPath>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>11</release>
</configuration>
</plugin>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.6</version>
<executions>
<execution>
<!-- Default configuration for running -->
<!-- Usage: mvn clean javafx:run -->
<id>default-cli</id>
<configuration>
<mainClass>com.example.App</mainClass>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
In the end I downloaded Maven again and extracted it into a folder and changed my environment variables on path. Maven is successfully installed on version 3.8.7
Thank you in advance for any help!
UPDATE
I should mention that I tried it on a new java project (without maven) and it works fine, by importing the .jar file from git-hub repository

Intellij mvn install complains "cannot access <package>" during testComplile after adding cucumber-junit-platform-engine dependency in pom.xml

I am planning to use Cucumber Test with Junit 5 in Maven. So I followed cucumber to install different maven dependency. I added a runner class to execute my cucumber tests
package pirate;
import org.junit.platform.suite.api.ConfigurationParameter;
import org.junit.platform.suite.api.IncludeEngines;
import org.junit.platform.suite.api.SelectClasspathResource;
import org.junit.platform.suite.api.Suite;
import static io.cucumber.junit.platform.engine.Constants.GLUE_PROPERTY_NAME;
#Suite
#IncludeEngines("cucumber")
#SelectClasspathResource("pirate")
#ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "pirate")
public class Runner {}
I also created a new folder name pirate under resources folder and move all .feature files into that new folder.
But when I execute mvn clean install, the command fails at testCompile:
It seems like the compiler can't read the package name?
Below is my pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>onetwothree</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>onetwothree</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<testSourceDirectory>src/test/java</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<properties>
<configurationParameters>
cucumber.junit-platform.naming-strategy=long
</configurationParameters>
</properties>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.9.0</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>7.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit-platform-engine</artifactId>
<version>7.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-suite</artifactId>
<version>1.9.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
My project structure is:
but if I comment out cucumber-junit-platform-engine dependency in pom.xml, the error is gone but it won't run the cucumber tests. Did I miss something here?
Not sure if it's related, but one of the error messages is like:
[ERROR] error reading /Users/xx/.m2/repository/org/junit/platform/junit-platform-engine/1.9.1/junit-platform-engine-1.9.1.jar; zip file is empty
[ERROR] /Users/xx/Desktop/zz/src/test/java/pirate/Runner.java:[1,1] cannot access pirate
ZipException opening "junit-platform-engine-1.9.1.jar": zip END header not found
Maven : error in opening zip file when running maven
following the procedure fixes the "cannot access " problem.
did you tried with a different scope than test for the cucumber-junit-platform-engine dependency?

Self-contained JavaFX 12 build with JDK 12 & Maven?

There are a lot of threads about JavaFX not being included with JDK 12/11, and that you need to have your own installation of JavaFX, but I found only this example of a modular installation without external download requirements :
https://github.com/openjfx/samples/tree/master/IDE/IntelliJ/Modular/Maven
Here is my pom.xml :
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>scheduler</groupId>
<artifactId>shop-solver</artifactId>
<version>1.0.0</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>12</maven.compiler.source>
<maven.compiler.target>12</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.3</version>
<configuration>
<mainClass>scheduler.Main</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>12</release>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>12</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>12</version>
</dependency>
<dependency>
<groupId>com.jfoenix</groupId>
<artifactId>jfoenix</artifactId>
<version>9.0.8</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>27.1-jre</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>1.21</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>1.21</version>
</dependency>
</dependencies>
I'm still getting this error when I try to run through the javafx:run plugin :
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.
Unrecognized option: --module-path
When I try to run from IntelliJ I get another error :
Caused by: java.lang.IllegalAccessError: class com.sun.javafx.fxml.FXMLLoaderHelper (in unnamed module #0xd5599b7) cannot access class com.sun.javafx.util.Utils (in module javafx.graphics) because module javafx.graphics does not export com.sun.javafx.util to unnamed module #0xd5599b7
The project worked fine under JDK1.8 and JavaFX 2, here is a video of the error happening on the upgrade : https://puu.sh/E25JW/7ecdca26d7.webm
Again, the issue is that I'm trying to build all of this modularly, I want to be able to publish a repository with maven dependencies and maybe plugin or build directives, but do not want to have people who clone my repo need to install JavaFX externally outside of Maven. Thanks.

Running maven project doesn't work

I am trying to follow the Maven in 5 Minutes tutorial for my project but for some reason I can't seem to actually run my project when it builds. I have the following pom.xml file:
<?xml version="1.0"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.my-group</groupId>
<artifactId>my-artifact</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.sun</groupId>
<artifactId>tools</artifactId>
<version>1.8.0</version>
<scope>system</scope>
<systemPath>${java.home}/../lib/tools.jar</systemPath>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>javafx</artifactId>
<version>2.2.3</version>
<scope>system</scope>
<systemPath>${project.basedir}/jfxrt.jar</systemPath>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<manifest>
<mainClass>com.my.package.MyClass</mainClass>
</manifest>
</configuration>
</plugin>
</plugins>
</build>
</project>
The tutorial seems to imply that I can do this from my project root:
mvn package
java -cp target/my-artifact-0.0.1-SNAPSHOT.jar com.my.package.MyClass
However, if I try to do this, I get the following error message:
Error: Could not find or load main class com.my.package.MyClass
If I run java -cp ../my-artifact-0.0.1-SNAPSHOT.jar com.my.package.MyClass from the target/classes directory I still get the same message, which is weird because I've verified that the .class file is there. Why am I not able to run my project after building it with the above steps?

Maven Eclipse and Dependencies

I have an Maven Eclipse project and it runs fine within Eclipse, however if I attempt to run it from the command line it fails to find the dependencies.
The pom.xml file:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.something.or.rather.myapp</groupId>
<artifactId>MyApp</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>MyApp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.cloudant</groupId>
<artifactId>cloudant-client</artifactId>
<version>1.0.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
From the project directory (MyApp):
mvn eclipse:eclipse // runs fine
mvn package // runs fine
java -cp target/MyApp-1.0-SNAPSHOT.jar com.something.or.rather.myapp.MyMainClass // fails
...
Exception in thread "main" java.lang.NoClassDefFoundError: com/cloudant/client/api/CloudantClient
...
I notice if I put the full path of the Cloudant JAR file in the java command it resolves that exception only to raise another.
java -cp /full/path/to/cloudant-client-1.0.1jar:target/MyApp-1.0-SNAPSHOT.jar com.something.or.rather.myapp.MyMainClass // still fails
...
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/client/methods/HttpRequestBase
...
Any ideas? This is painful.
By default maven builds thin jars without the project dependencies, only the project classes.
What you want is a fat jar. To do this have a look at:Building a fat jar using maven
Hope this helps

Categories