A simple Cassandra client app using netflix astyanax driver is built in Eclipse. Here is 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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>CCL1</groupId>
<artifactId>CCL10</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>CassandraCL1</name>
<description>Cassandra Client 1</description>
<dependencies>
<dependency>
<groupId>com.netflix.astyanax</groupId>
<artifactId>astyanax</artifactId>
<version>1.56.44</version>
</dependency>
</dependencies>
</project>
The app works fine when started from Eclipse. But when the jar file is copied to an external machine it fails to start with the following exception:
ubuntu#ip-172-31-31-41:~/tmp$ java -cp ./CCL10-0.0.1-SNAPSHOT.jar AppCCL1
Exception in thread "main" java.lang.NoClassDefFoundError: com/netflix/astyanax/connectionpool/exceptions/ConnectionException
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.getMethod0(Unknown Source)
at java.lang.Class.getMethod(Unknown Source)
at sun.launcher.LauncherHelper.getMainMethod(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: com.netflix.astyanax.connectionpool.exceptions.ConnectionException
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 6 more
The external machine is Amazon t1.micro Ubuntu-12 instance. The only software installed there is Oracle’s Java Runtime Environment 1.7.
It looks like some dependency is missing, but I cannot even see the name of the missing class.
Why did Maven fail to insert some dependency in jar? How do I troubleshoot and resolve such a problem?
It turns out Maven does not include all the dependencies in jar. (I assumed Maven does this automatically). Including all dependencies in jar is explained here:
How can I create an executable JAR with dependencies using Maven?
So I solved the problem by adding the following fragment in my xml file:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>AppCCL1</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Related
I want to add 3rd party external jar into my maven repository. My project is in Spring Boot.
Error :
`Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner
.java:48)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:51)
at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:52)
Caused by: java.lang.NoClassDefFoundError: com/omnesys/omne/om/OMNM
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Unknown Source)
at java.lang.Class.getConstructor0(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at com.omnesys.nestq.classes.ClassNestQ12.<init>(ClassNestQ12.java:28)
at com.omnesys.nestq.classes.NestControl.<init>(NestControl.java:39)
at orion.ibclient.OmnesysJAPI.<init>(OmnesysJAPI.java:69)
at orion.TradingProgram.connectToNest(TradingProgram.java:452)
at orion.TradingProgram.main(TradingProgram.java:90)
at com.orion.main.OrionController.loadPrperty(OrionController.java:39)
at com.orion.main.NestOrionApplication.main(NestOrionApplication.java:21
)
... 8 more
Caused by: java.lang.ClassNotFoundException: com.omnesys.omne.om.OMNM
at java.lang.ClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 19 more`
I had done following things but still I am getting the same error.
NOTE : It working for other jar but not worked only for this jar. Like for DealerSample.jar its working but not working for this jar. You can see the difference in below picture. When I added the same jar in Simple java project through Build path its working but not in Spring Boot project.
Add external jar : https://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html
: Added my jar to .m2 folder and add dependancy into maven.
Maven Command : mvn install:install-file -Dfile=D:\Vijay\Jars\OmneVerse.15.0.0.0.jar -DgroupId=com.orion -DartifactId=OmneVerse -Dversion=1.0.0 -Dpackaging=jar -DgeneratePom=true
Pom.xml:
<dependencies>
<dependency>
<groupId>com.orion</groupId>
<artifactId>OmneVerse</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
</plugins>
</build>
My Project Libs structure :
Why not try Nexus Repository Manager?
Though this approach may be long and complex. But I think it is the best and lasting solution to your problem. I have used it many times and find it very helpful.
I'm trying to create a jar file of my simple java gui application. I'm using Intellij IDEA and maven.
I have imported Mig Layout as a maven dependency, when I run the program inside Intellij IDEA everything works fine, but when I create a jar by doing mvn clean install or mvn clean package, although maven says BUILD SUCCESSFUL, when I try to open the jar file I get the following Stacktrace (I believe it's a stack trace). The image Stacktrace
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: net/miginfocom/swing/MigLayout at BotGui.<init>(BotGui.java:29) at java.awt.event.InvocationEvent.dispatch(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$500(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source) Caused by: java.lang.ClassNotFoundException: net.miginfocom.swing.MigLayout at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) ... 15 more
this is my dependencies in maven.
<?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.ptcontact.testdiscord</groupId>
<artifactId>Discord_Bot</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.miglayout</groupId>
<artifactId>miglayout-swing</artifactId>
<version>5.0</version>
</dependency>
<dependency>
<groupId>net.dv8tion</groupId>
<artifactId>JDA</artifactId>
<version>3.8.0_436</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>jcenter</id>
<name>jcenter-bintray</name>
<url>https://jcenter.bintray.com</url>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<target>8</target>
<source>8</source>
</configuration>
</plugin>
<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>BotGui</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
</plugins>
</build>
</project>
Yes I have tried out version 5.0, 4.2 and 5.2.
Additionally, I have tried to add miglayout-core as a dependency as well, but that didn't change anything.
I Resolved the problem!
Like #CrazyCoder said, I needed to create a Fat Jar, in other words I needed to actually include the dependencies inside my jar file. Because I'm a beginner I thought that maven did that for you automatically, oh well.
To create a fat jar, I followed this guide:
http://tutorials.jenkov.com/maven/maven-build-fat-jar.html
Hi I want run my project outside intelij. I build my project by means of maven. I get information that builded succes. However when i try run my jar file view error. My jdk version is 11, to project it is attached javafx library. Previously, I had version 1.8 but for sure I compile with use jdk 11. Project using also database MySql. Project in Intelij works without problems.
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.UnsupportedClassVersionError: com/controllerCP/Main has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
<?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>ProjectCPApril</groupId>
<artifactId>ProjectCPApril</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<javafx.version>11.0.1</javafx.version>
</properties>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-graphics</artifactId>
<version>${javafx.version}</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>${javafx.version}</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>${javafx.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
</dependencies>
<build>
<defaultGoal>install</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.0</version>
<executions>
<!-- Attach the shade into the package phase -->
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.controllerCP.Main</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Hi I have downloaded jdk 8 and jdk 11 on my computer. In cmd windows I write java -version and show me 1.8 but I checked and I have in environment variables set Path to bin jdk 11.
a Project of mine using Commons Chain works fine, when i run it in the IDE.
Now i tried to package it with maven.
I used the following pom.xml:
<build>
<finalName>NAME</finalName>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>main.Main</mainClass>
</manifest>
</archive>
</configuration>
<version>2.6</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
<version>3.3</version>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>commons-chain</groupId>
<artifactId>commons-chain</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
</dependencies>
As you can see i added the Main Class to my manifest and specified the Dependencies to use.
The package process runs throught, but when i start the jar it says:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/ch
ain/impl/ChainBase
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at main.Main.main(Main.java:24)
Where does this error come from/what do i need to change in my config file?
Greetings,
Nicolas
You need to specify a classpath that has the jar files for your dependencies, something like java -cp=libs -jar main.jar where libs contains the dependency jar files. Maven will set that up for you with the maven-exec-plugin: mvn exec:java -DmainClass=main.Main. Maybe what you want is to have Maven create an "uber jar" that includes all of the class files for your dependencies so that java -jar main.jar just works. Use the maven-assembly-plugin or the maven-shade-plugin to create one. See What is an uber jar?.
Maven doesn't add the classpath to a jar by default.
You need to tell maven-jar-plugin to do that by adding an addClasspath entry to its configuration/archive block.
<addClasspath>true</addClasspath>
(this should be immediately before or after the mainClass line)
I work on Eclipse (Juno with JDK7), and the program runs (on Eclipse) fine.
My problematic lines are:
URL imageURL = new URL("http://www.idautomation.com/ocr-a-and-ocr-b-fonts/new_sizes_ocr.png");
RenderedImage img = ImageIO.read(imageURL);
File outputfile = new File("saved.png");
ImageIO.write(img, "png", outputfile);
But when i export the project to a jar file and try to run it via windows (7- 64 bit) command line, the following error appears:
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: java.util.ServiceConfigurationError: javax.imageio.spi.ImageReaderSpi: Providercom.sun.media.imageioimpl.plugins.jpeg.CLibJPEGImageReaderSpi could not be instantiated: java.lang.IllegalArgumentException: vendorName == null!
at java.util.ServiceLoader.fail(Unknown Source)
at java.util.ServiceLoader.access$100(Unknown Source)
at java.util.ServiceLoader$LazyIterator.next(Unknown Source)
at java.util.ServiceLoader$1.next(Unknown Source)
at javax.imageio.spi.IIORegistry.registerApplicationClasspathSpis(Unknow
n Source)
at javax.imageio.spi.IIORegistry.<init>(Unknown Source)
at javax.imageio.spi.IIORegistry.getDefaultInstance(Unknown Source)
at javax.imageio.ImageIO.<clinit>(Unknown Source)
at SimpleQueueServiceSample.testOCR(SimpleQueueServiceSample.java:75)
at SimpleQueueServiceSample.main(SimpleQueueServiceSample.java:69)
... 5 more
Caused by: java.lang.IllegalArgumentException: vendorName == null!
at javax.imageio.spi.IIOServiceProvider.<init>(Unknown Source)
at javax.imageio.spi.ImageReaderWriterSpi.<init>(Unknown Source)
at javax.imageio.spi.ImageReaderSpi.<init>(Unknown Source)
at com.sun.media.imageioimpl.plugins.jpeg.CLibJPEGImageReaderSpi.<init>(CLibJPEGImageReaderSpi.java:80)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at java.lang.Class.newInstance0(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
... 13 more
I also using that imports:
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
Please, someone know the problem?
Thanks in advance!
If you export using "Runnable JAR file" then Eclipse will add a custom ClassLoader and a custom main class into the jar file.
On the same time it seems you have installed some Image-IO extensions into the JDK - something providing the class CLibJPEGImageReaderSpi. On my system (Ubuntu, JDK 1.7) there is no such class but JPEGImageReaderSpi. The CLib part makes me think, that you have installed a native library doing the JPEG reading.
These two parts together seem to make the trouble. Solution - Try to export as a simple jar, start by hand providing the classpath on the commandline. If that works, provide a shell wrapper providing the classpath for easier use.
EDIT Googling around I have found an article with exactly that problem:
https://www.java.net//node/695773
I guess I can provide another solution as to this question since I got this error a few days ago and finally solve it.
You can check this article first, here explained the reason: Exception when trying to save images
==> To sum up, the required META-INF the jar need to use is missing, so it can't find the "vender-Name" in the MANIFEST.MF.
As a result, I use MAVEN to generate the required runnable jar instead of using Eclipse to generate it. How? You can write a pom.xml to achieve it and remember to use "maven-assembly-plugin" to generate the required MANIFEST.MF in the jar file. This is the key step. And I can also give you a sample(pom.xml) for it:
<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>xxxProject</groupId>
<artifactId>xxxProject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<repositories>
<repository>
<id>oss.sonatype.org</id>
<name>Sonatype Snapshot Repository</name>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.demo.Main</mainClass>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
</manifest>
<manifestEntries>
<Specification-Vendor>Sun Microsystems, Inc.</Specification-Vendor>
<Implementation-Vendor>Sun Microsystems, Inc.</Implementation-Vendor>
</manifestEntries>
</archive>
</configuration>
<executions>
<execution>
<id>create-my-bundle</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20151123</version>
</dependency>
</dependencies>
So, the most important part is:
<manifestEntries>
<Specification-Vendor>Sun Microsystems, Inc.</Specification-Vendor>
<Implementation-Vendor>Sun Microsystems, Inc.</Implementation-Vendor>
</manifestEntries>
That means maven will add the required META-INFO for you in the jar so that you can solve this issue.
That's it. Hope these info can help you. =)