I am attempting to use Joda-Time with my bukkit plugin, however I am running into ClassNotFoundException and a NoClassDefFoundError.
I am using eclipse and have added it to my Build Path, and modified my Class-Path variable in my MANIFEST in the jar (and then adding the jar to a lib folder outside of my jar file), as well as tried to add the joda-time jar files in my jar file.
So make myself a bit more clear... The ClassNotFoundException shows up, when I add the files manually to my jar the NoClassDefFoundError shows up.
I've tried converting my project to maven (via eclipse) and added joda-time as a dependency to the pom.xml, but I cant seem to get that to work either (Maybe I'm doing something wrong).
If you need anymore information please ask. I'm not sure what else to include.
Thanks for any help you can give me.
java.lang.NoClassDefFoundError: org/joda/time/ReadableInstant
at com.**********.plugin.UMSBase.registerEvents(UMSBase.java:46)
at com.***********.plugin.UMSBase.onEnable(UMSBase.java:22)
at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:217)
at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:457)
at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:381)
at org.bukkit.craftbukkit.v1_5_R2.CraftServer.loadPlugin(CraftServer.java:282)
at org.bukkit.craftbukkit.v1_5_R2.CraftServer.enablePlugins(CraftServer.java:264)
at net.minecraft.server.v1_5_R2.MinecraftServer.j(MinecraftServer.java:303)
at net.minecraft.server.v1_5_R2.MinecraftServer.e(MinecraftServer.java:282)
at net.minecraft.server.v1_5_R2.MinecraftServer.a(MinecraftServer.java:242)
at net.minecraft.server.v1_5_R2.DedicatedServer.init(DedicatedServer.java:150)
at net.minecraft.server.v1_5_R2.MinecraftServer.run(MinecraftServer.java:381)
at net.minecraft.server.v1_5_R2.ThreadServerApplication.run(SourceFile:573)
Caused by: java.lang.ClassNotFoundException: org.joda.time.ReadableInstant
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 org.bukkit.plugin.java.PluginClassLoader.findClass0(PluginClassLoader.java:80)
at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:53)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 13 more
Did you put Joda Time's jar file in the same folder as craftbukkit.jar.
I'm not sure if this will fix the problem.
Just add to build plugins maven-shade-plugin, which inserts Joda Time to plugin jar, so the classes will be found at runtime.
Into your pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>jar-with-dependencies</shadedClassifierName>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Then use name-with-dependencies.jar as your plugin file.
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 use(import) a class added by a dependency of my project.
I added the dependency gson in my pom.xml as follows:
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
And I ran mvn install after that.
Now I can find the jar dependencies in C:\Users\%Name%\.m2\repository\com\google\code\gson\gson\2.8.5, so I assume it is correctly installed.
When I try to import the class Gson in Java, several problems occurs:
The path to import isn't com.google.code.gson but com.google.gson, I don't understand why.
When I compile with mvn package, everything runs smoothly. But if I try to run the code with java -cp .\target\testJSON-1.0-SNAPSHOT.jar json.App I get the following error telling me the Gson class isn't found :
Exception in thread "main" java.lang.NoClassDefFoundError: com/google/gson/Gson
at json.App.main(App.java:13)
Caused by: java.lang.ClassNotFoundException: com.google.gson.Gson
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)
... 1 more
Now from what I understood by looking for solutions:
While maven know the dependencies of the project, java won't. And I need to manually add the jar to the classpath.
So my questions are:
If I bother to tell maven I need gson to run my project, why do I need to manually include all dependencies to run it ? Is there a way to automate this thing in an easy and intuitive manner ?
When I look for ways to add multiples jars to the classpath, the only answers I find assume that all the jars are in the same folder, hence proposing to do java -cp folder/*.jar. The jar generated by Maven is located in the target subfolder of my project, while the gson jar is located in the .m2 folder. How to add multiple jars (located in different locations) to the classpath ?
Thank you,
The path to import isn't com.google.code.gson
Google Code got shut down in 2016, they have changed their group IDs.
the Gson class isn't found
The GSON JAR isn't on the classpath.
If I bother to tell maven I need gson to run my project, why do I need
to manually include all dependencies to run it ? Is there a way to
automate this thing in an easy and intuitive manner ?
Maven is a build tool, and it has built your app successfully. Java requires you to include the JARs on the classpath. This not a deficiency of Maven. If you want to bundle all the dependencies together into one JAR that "just runs" then create a fat JAR.
How to add multiple jars (located in different locations) to the
classpath ?
If you create a fat JAR, you won't have to, but you can separate classpath entries like so (OS-specific):
java -cp folder/*.jar;another/folder/*.jar
You may want to include the dependencies in when creating a jar with maven. You can use maven-assembly plugin,
<build>
<plugins>
<!-- any other plugins -->
<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>
</plugins>
</build>
I tried to implement a simple producer consumer example with kafka and I achieved with the following properties:
Properties configProperties = new Properties();
configProperties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,"localhost:" + portNumber);
configProperties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.ByteArraySerializer");
configProperties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringSerializer");
// Belirtilen property ayarlarına sahip kafka producer oluşturulur
org.apache.kafka.clients.producer.Producer producer = new KafkaProducer(configProperties);
However when I try the exact same configs, and everything else the same, in another project which is a plugin for a data visualization software, I got this error:
.... // Here there is some other stuff but I thing the important one is the below one
Caused by: java.lang.NoClassDefFoundError: org/apache/kafka/clients/producer/Producer
at App.MyControlPanel.<init>(MyControlPanel.java:130)
at App.CytoVisProject.<init>(CytoVisProject.java:29)
... 96 more
Caused by: java.lang.ClassNotFoundException: org.apache.kafka.clients.producer.Producer
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 98 more
In the first one that I said it worked, I was using "mvn clean compile assembly:single", but in the second one I created a jar file for the whole project. Because the visualization software wants a jar file to install the plugin. Since every thing is same (At least I could not find any difference, I used same code) I guess the problem is about the way build the project. What happened here? What is the difference between "mvn clean compile assembly:single" and building a jar file in IntelliJ? Why I got this error and how to fix this? Thanks a lot for help!
As I said in the last comment of the first answer, I have a plugin which has manifest and transform as goal. Here:
<plugin>
<groupId>com.springsource.bundlor</groupId>
<artifactId>com.springsource.bundlor.maven</artifactId>
<version>1.0.0.M2</version>
<configuration>
<outputManifest>C:\Users\USER\AppData\Local\Temp\archetype2tmp/META-INF/MANIFEST.MF</outputManifest>
<failOnWarnings>false</failOnWarnings>
<removeNullHeaders>true</removeNullHeaders>
<manifestHeaders><![CDATA[Bundle-ManifestVersion: 2
Bundle-Name: CytoVisProject
Bundle-SymbolicName: CytoVisProject
Spring-DM-Version: ${pom.version}
]]></manifestHeaders>
</configuration>
<!-- generate the manifest automatically during packaging -->
<executions>
<execution>
<id>bundle-manifest</id>
<phase>package</phase>
<goals>
<goal>manifest</goal>
<goal>transform</goal>
</goals>
</execution>
</executions>
</plugin>
If I use a shade plugin like below:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.0</version>
<configuration>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
It does not works, because I need to use manifest and transform as goals in my plugin. How can I add kafka classes to the jar file that IntelliJ creates to solve this problem (I am not sure if this can solve or not)?
I've found an easier solution. I have changed
kafkaProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringSerializer");
to this
kafkaProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
And afterwards my code run, also as part of a pre-compiled plug-in.
What the problem is
At compile time kafka-clients-0.10.0.0.jar is available, so the code compiles successfully, but at runtime it is missing, hence the error you get.
How to fix it
You have 2 options:
Include the kafka JAR inside your JAR
Add the kafka JAR to your plugin's classpath
Also, make sure you don't have a dependency for kafka-clients marked with provided scope.
I'm making an eclipse plugin that I also want to make it work command line. As normal the usual problem is encountering the eclipse dependencies in command-line:
Exception in thread "main" java.lang.NoClassDefFoundError: org/eclipse/swt/widgets/Layout
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:56)
Caused by: java.lang.ClassNotFoundException: org.eclipse.swt.widgets.Layout
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 3 more
Now I know there is an eclipse FAQ for this problem however here is a bit different. I'm using Maven for build with the maven-dependency-plugin to add jars I need from other project to this jar as dependencies:
<build>
<plugins>
<plugin>
<!-- Copy non-Eclipse plugins to target/dependency so that may be referenced
for runtime use. -->
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
<executions>
<execution>
<id>copy-dependencies</id>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<!-- seems the line below is needed otherwise every second run the
copy-dependencies fails , link to bug : https://bugs.eclipse.org/bugs/show_bug.cgi?id=393978 -->
<includeScope>runtime</includeScope>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
The dependencies in my Manifest file looks like this:
Rsrc-Class-Path: ./
lib/jcommon-1.0.23.jar
lib/jfreechart-1.0.19.jar
lib/jfreechart-1.0.19-swt.jar
lib/jfreechart-1.0.19-experimental.jar
lib/swt.jar
target/dependency/my.first.package.jar
target/dependency/my.second.package.jar
Class-Path: .
Rsrc-Main-Class: my.package.Main
Main-Class: org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader
Bundle-ClassPath: .,
lib/jcommon-1.0.23.jar,
lib/jfreechart-1.0.19-experimental.jar,
lib/jfreechart-1.0.19-swt.jar,
lib/jfreechart-1.0.19.jar,
lib/swt.jar
Require-Bundle: org.eclipse.core.runtime,
org.eclipse.ui,
my.first.package;visibility:=reexport,
my.second.package;visibility:=reexport,
ca.odell.glazedlists,
org.eclipse.nebula.widgets.nattable.core,
org.eclipse.core.resources,
org.eclipse.swt
As you can see I added an swt.jar to a lib inside my project and am referencing it there as to have all my dependencies inside my jar.
Still I get the above error when I try to run it command line as if the swt.jar isn't there.
What am I missing?
If you want to make an Eclipse plugin command-line friendly, the best thing is to have the plugin also define an Eclipse application that you can then invoke from command-line: https://wiki.eclipse.org/FAQ_How_do_I_create_an_application%3F
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>