When running Java jar file my dependency classes aren't found - java

I currently have a maven project with 1 dependency. When I build it using mvn package and run it I get a ClassNotFoundException for the classes in my dependency. When I export it as a normal jar in Intellij I still get the same error. I have tried just about everything including setting -cp in the command line. Note that the main class is being found, just not the classes in my dep
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>me.jaredstef</groupId>
<artifactId>hawkeyecountdownbot</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>hawkeyecountdownbot</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.twitter4j</groupId>
<artifactId>twitter4j-core</artifactId>
<version>[4.0,)</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<mainClass>me.jaredstef.hawkeyecountdownbot.HawkHeart</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
EDIT:
Stacktrace:
Exception in thread "main" java.lang.NoClassDefFoundError: twitter4j/TwitterException
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: twitter4j.TwitterException
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)
... 7 more

It looks like it's failing to find a dependent class (twitter4j.TwitterException) which is used in a method signature of a class you are using from your own code.
How do you get this compiled without error?
If that works though it may be an issue of incompatible versions of the twitter4j library to be present: one at compile time (with the TwitterException) and a different one at runtime (without the TwitterException).
This may be caused by your allowing to use "4.0 or higher" in the dependency. It may help to define an exact version there to track this down.

When you run a JAR file with the -jar switch, class path defined on the command line (or in environment variables) is ignored.
You will have to add a Class-Path manifest attribute that includes the location of your dependencies.
Maven-Jar-Plugin (or more exactly, the Maven Archiver configuration) supports this by using the addClasspath and classpathPrefix options. Note that you still have to ensure that the dependency libs end up in that directory (e. g. by using maven-assembly-plugin or by manually copying the dependencies there). Or build a fat jar with maven-assembly-plugin that includes all dependencies.

I will suggest you to do following:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-4</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>Main_Class</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<finalName>Final_Jar_Name</finalName>

Related

Error: Unable to initialize main class Caused by: java.lang.NoClassDefFoundError: org/bson/conversions/Bson

https://github.com/Gjonathan252/AutoPart-Java-Database-w-MongoDB
Its my first time using maven and I am trying to run the .jar of my program but I keep getting this error in the command prompt when I run.
java -jar autopartsjavamongodb-1.0-SNAPSHOT.jar
result:
Error: Unable to initialize main class com.example.autopartsjavamongodb.main
Caused by: java.lang.NoClassDefFoundError: org/bson/conversions/Bson
The Java program uses mongodb dependencies and they all seem to be working when I run the program on the IDE or using this long command on the command prompt.
"C:\Program Files\Java\jdk-16.0.2\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2021.2\lib\idea_rt.jar=63226:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2021.2\bin" -Dfile.encoding=UTF-8 -classpath "D:\Computer Science Lessons\AutoPart Java Database w MongoDB\target\classes;C:\Users\gjona\.m2\repository\org\mongodb\mongodb-driver-sync\4.3.1\mongodb-driver-sync-4.3.1.jar;C:\Users\gjona\.m2\repository\org\mongodb\bson\4.3.1\bson-4.3.1.jar;C:\Users\gjona\.m2\repository\org\mongodb\mongodb-driver-core\4.3.1\mongodb-driver-core-4.3.1.jar" com.example.autopartsjavamongodb.main
This is what the pom.xml file looks like. I dont think I am missing any depencacies as I am able to run it on the IDE.
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>autopartsjavamongodb</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<maven.compiler.release>11</maven.compiler.release>
<maven.compiler.source>16</maven.compiler.source>
<maven.compiler.target>16</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-sync</artifactId>
<version>4.0.5</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-sync</artifactId>
<version>4.3.1</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>bson</artifactId>
<version>4.0.5</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.example.autopartsjavamongodb.main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
Normally, when we package a project into a jar file, the jar file doesn't contain its dependencies, so the dependency jar files would need to be included in the classpath.
One way to achieve that is by using maven-assembly-plugin
As a result, all the dependency jars would be packaged in one jar.
pom.xml example
`<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>libs/</classpathPrefix>
<mainClass>com.example.autopartsjavamongodb.main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>`
Also please note pluginManagement tag is used to define the plugin version in your parent POM, in your case you shouldn't include pluginManagement tag

Java Maven project cannot find dependency [duplicate]

This question already has answers here:
How can I create an executable/runnable JAR with dependencies using Maven?
(33 answers)
Closed 1 year ago.
Whenever I try to run my project as maven install, it creates a completely normal jar file. It is a swing gui application. However, when I click a button in the GUI that uses this websocket client, it ouputs the error below.
Exception in thread "Thread-2" java.lang.NoClassDefFoundError: com/neovisionaries/ws/client/WebSocketException
at bot.BotFunc.run(BotFunc.java:30)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.ClassNotFoundException: com.neovisionaries.ws.client.WebSocketException
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
... 2 more
my pom.xml looks like this:
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>HelixBot</groupId>
<artifactId>HelixBot</artifactId>
<version>1.1</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifest>
<mainClass>gui.GUIMain</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.neovisionaries</groupId>
<artifactId>nv-websocket-client</artifactId>
<version>2.14</version>
</dependency>
</dependencies>
<packaging>jar</packaging>
</project>
As you can see, the correct dependency is added, so I am not sure why this error is appearing. I have tried clean building it and checked the .project and .classpath files. It may be due to the fact that I converted it to maven from a regular java project, but I have not found anything. Please let me know if there is a possible solution.
If you are saying that you run maven install and gets a "normal" jar file, maybe you are missing the plugin to add the dependencies to your jar. So after you run a simple mvn clean package, and run the jar file. I believe that could work.
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>

Maven clean install not including sqlite dependency for executable jar file

After using
mvn clean install
and then
java -jar executable.jar
I get this error:
java.lang.ClassNotFoundException: org.sqlite.JDBC
This 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>****</groupId>
<artifactId>****</artifactId>
<version>0.7-SNAPSHOT</version>
<packaging>jar</packaging>
<name>****</name>
<description>****</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.23.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit/junit5-engine -->
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit5-engine</artifactId>
<version>5.0.0-ALPHA</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.tinyjee.jgraphx/jgraphx -->
<dependency>
<groupId>org.tinyjee.jgraphx</groupId>
<artifactId>jgraphx</artifactId>
<version>3.4.1.3</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>******</sourceDirectory>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>*****</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>******</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<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>
Running the program inside intelliJ works without problems. I included it there from the project structure.
I replaced names and directories with ****. This is a school project and I don't want my prof accusing me of providing solutions to other groups in case they find this stackoverflow entry.
Probably you are getting this only when you are running your jar because the dependencies are not available/packaged inside of it.
Try generating a "fat jar" (also known as uber-jar), it will package all your dependencies inside the jar:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
<configuration>
<finalName>YOUR_JAR_FINAL_NAME</finalName>
</configuration>
</plugin>
</plugins>
</build>
Documentation related to the maven-shade-plugin can be found in here
Since you are using a runnable jar file, you can follow this section of the documentation related to Executable Jars
Some background.
Maven install never installs dependencies;
it only installs the project that is build via the POM.
The installation of dependencies are a task that you must also perform
if you don't use either a "fat jar" (which I can't recommend) or use
the new spring boot executable jar implementation.
The classic "fat jar" is an amazingly terrible
(but often the only option)
solution for a problem like this.
Consider using Spring-Boot;
they have developed a new,
sane,
version of an executable jar file that includes the dependencies within the executable jar and adds them to the classpath at startup.
This functionality is similar to the functionality of a "war" file when it is added to
a JEE container.
Caveat: I don't work for Pivotal,
I just like much of their work (the Spring Framework).
Edit:
Here is a link to the
Executable Jar Section in the Spring Boot Reference.
It contains some details.

Maven Error: Could not find or load main class

I'm using a Java Maven program and I don't know what to enter as the <mainClass>. I've tried all kinds of things based off of numerous stackoverflow questions, but they are not solving the error.
Each time it says:
Maven Error: Could not find or load main class ...
I have this written inside my pom.xml (minus the ???)
<build>
...
<plugins>
...
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.3</version>
<configuration>
<descriptors>
<descriptor>src/main/assembly/jar-with-dependencies.xml</descriptor>
</descriptors>
<archive>
<manifest>
<mainClass> ??? </mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
...
</plugins>
...
</build>
How do I fix these errors?
I got this error using Maven, and I discovered the solution.
Error: Could not find or load main class com.mycompany.testapifactory.Main
I'm using java JDK version 1.7 on Linux, my pom.xml file was the default generated by Netbeans and I was using these commands to compile, which do work fine with a normal hello-world java application:
mvn clean compile
java -jar target/TestAPIFactory-1.0-SNAPSHOT.jar com.mycompany.testapifactory.Main
What happened:
It turns out my problem was that my Main method was extending something Exotic like this:
public class Main extends SomeExoticLibraryClass{
public static void main(String[] args){
//...
}
}
It was this extending of the main class that caused the above error.
TLDR solution:
Make sure your main class isn't extending any 3rd party classes. Refactor those out and away into their own classes. That error message is awful, and requires process of elimination to find out what to do.
Unless you need the 'maven-assembly-plugin' for reasons other than setting the mainClass, you could use the 'maven-jar-plugin' plugin.
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<index>true</index>
<manifest>
<mainClass>your.package.yourprogram.YourMainClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
You can see the plugin in practise in the ATLauncher.
The 'mainClass' element should be set to the class that you have the entry point to your program in eg:
package your.package.yourprogram;
public class YourMainClass {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
For me the problem was nothing to do with Maven but to do with how I was running the .jar. I wrote some code and packaged it as a .jar with Maven. I ran it with
java target/gs-maven-0.1.0.jar
and got the error in the OP. Actually you need the -jar option:
java -jar target/gs-maven-0.1.0.jar
Please follow the below snippet.. it works..
<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.xyz</groupId>
<artifactId>test</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>TestProject</name>
<description>Sample Project</description>
<dependencies>
<!-- mention your dependencies here -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<archive>
<manifest>
<mainClass>com.xyz.ABC.</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Please note, you have to provide the full classified class name (class name including package name without .java or .class) of main class inside <mainClass></mainClass> tag.
I got it too, for me the problem got resolved after deleting the m2 folder (C:\Users\username.m2) and updating the maven project.
I got it too, the key was to change the output folder from bin to target\classes. It seems that in Eclipse, when converting a project to Maven project, this step is not done automatically, but Maven project will not look for main class based on bin, but will on target\classes.
specify the main class location in pom under plugins
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<index>true</index>
<manifest>
<mainClass>com.example.hadoop.wordCount.WordCountApp</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
add this to your pom.xml file:
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>sample.HelloWorldApplication</mainClass>
</transformer>
</transformers>
</configuration>
and add the class name of your project (full path) along with the package name like "com.packageName.className" which consists of the main method having "run" method in it.
And instead of your "???" write ${mainClass} which will automatically get the className which you have mentioned above.
Then try command mvn clean install and mvn -jar "jar_file_name.jar" server "yaml_file_name.yml"
I hope it will work normally and server will start at the specified port.
The first thing i would suggest is to use the correct configuration for predefined descriptors.
<project>
[...]
<build>
[...]
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.3</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
[...]
</project>
To configure the main class you need to know the package and name of the class you would like to use which should be given into <mainClass>...</mainClass> parameter.
Furthermore i recommend to stop using Maven 2 and move to Maven 3 instead.
I got this error(classNotFoundException for main class), I actually changed pom version , so did maven install again and then error vanished.
TLDR : check if packaging element inside the pom.xml file is set to jar.
Like this - <packaging>jar</packaging>. If it set to pom your target folder will not be created even after you Clean and Build your project and Maven executable won't be able to find .class files (because they don't exist), after which you get Error: Could not find or load main class your.package.name.MainClass
After creating a Maven POM project in Netbeans 8.2, the content of the default pom.xml file are as follows -
<?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>com.mycompany</groupId>
<artifactId>myproject</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
Here packaging element is set to pom. Hence the target directory is not created as we are not enabling maven to package our application as a jar file. Change it to jar then Clean and Build your project, you should see target directory created at root location. Now you should be able to run that java file with main method.
When no packaging is declared, Maven assumes the packaging as jar. Other core packaging values are pom, war, maven-plugin, ejb, ear, rar. These define the goals that execute on each corresponsding build life-cycle phase of that package. See more here
this worked for me....
I added the following line to properties in pom.xml
<properties>
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
</properties>
For me, I added
<packaging>jar</packaging>
and removed the default spring-boot-maven-plugin
<!-- remove this plugin
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin> -->
Add the -Xdiag option at the execution. This is extra "diagnostic". This will not solve the issue but add more detailed error messages and root causes that help identifying the issue.
In a rare occasion, an emoji in the access path caused this type of error. Double check your directory names, maybe a non standard character makes all the fun!
I am late to the party in my case it was the image I was using that was causing the trouble
Working config
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>${jib-maven-plugin.version}</version>
<configuration>
<from>
<image>openjdk:11-jre-slim</image>
</from>
<to>
<image>${docker.image.prefix}/${project.artifactId}</image>
</to>
<container>
<mainClass>com.example.configserverApplication</mainClass>
<ports>
<port>8888</port>
</ports>
</container>
</configuration>
</plugin>
Failing one
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>${jib-maven-plugin.version}</version>
<configuration>
<from>
<image>openjdk:alpine</image>
</from>
<to>
<image>${docker.image.prefix}/${project.artifactId}</image>
</to>
<container>
<mainClass>com.example.configserverApplication</mainClass>
<ports>
<port>8888</port>
</ports>
</container>
</configuration>
</plugin>
My understanding is that
"The OpenJDK port for Alpine is not in a supported release by OpenJDK, since it is not in the mainline codebase. It is only available as early access builds of OpenJDK Project Portola. See also this comment. So this image follows what is available from the OpenJDK project's maintainers."
https://hub.docker.com/_/openjdk
it is weird that it fails with a one-line error and changing the image fixed it for me.

Eclipse / Maven: "Could not find the main class" Errors While Building Executable JAR

Updated: "Could not find the main class" turned out to be a red herring: In the line immediately before public static void main(String[] args) my class attempts to load a resource bundle from a file that was not included in the JAR. Failure to load this resource produced reporting that lead me in the wrong direction:
Caused by: java.util.MissingResourceException: Can't find bundle for base name propFile, locale en_US
at java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:1427)
at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1250)
at java.util.ResourceBundle.getBundle(ResourceBundle.java:705)
at com.foo.bar.MyApp(MyApp.java:103)
Could not find the main class: com.foo.bar.MyApp. Program will exit.
I'm developing on Win 7 64-bit with Eclipse Juno and JDK 1.6_45, both 32-bit. I'm generating an executable JAR with a mvn clean install invocation.
When I attempt to launch the app I've received "Could not find the main class" or "Failed to load Main-Class manifest attribute"
Similar ground has been covered here and here. I've read through these answers and the basic maven examples, but I'm still failing to end up
with an executable JAR.
I've attempted to execute the app with both java -jar MyApp-jar-with-dependencies and java -cp MyApp-jar-with-dependencies com.foo.bar.MyApp invocations
I find it particularly confusing that the MANIFEST.MF (contined within META-INF within the JAR) specifically lists my target main class, and the MyApp.class file is present in the directory tree at the correct location (com\foo\bar\MyApp.class).
I have refactored my package name and class name a few times over the course of development. Might this action have caused some referencing/classpath hiccup? Where else could the main class lookup failing?
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>
<parent>
[...]
</parent>
<groupId>com.foo.bar</groupId>
<artifactId>MyApp</artifactId>
<packaging>jar</packaging>
<version>2.0.0.0-SNAPSHOT</version>
<name>MY APP</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<issues-product>APP</issues-product>
<issues-component>MY_APP</issues-component>
</properties>
<dependencies>
[...]
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>${maven.checkstyle.version}</version>
<configuration>
[...]
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>${maven.pmd.version}</version>
<configuration>
[...]
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>${maven.javadoc.version}</version>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2.2</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.foo.bar.MyApp</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<pluginManagement>
[...]
</pluginManagement>
</build>
<reporting>
[...]
</reporting>
<scm>
[...]
</scm>
</project>
My MANIFEST.MF within the the output JAR:
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: myname
Build-Jdk: 1.6.0_45
Main-Class: com.foo.bar.MyApp
I'm not sure why yours isn't working, here is a cut-n-paste from my working maven project. You should only have to replace com.pictureu.mains.MainGui with your main class to test
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.pictureu.mains.MainGui</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>

Categories