Eclipse. ClassNotFoundException but I do know that class exists - java

I added myLibrary.jar in project BuildPath and try to call static method from myLibrary.jar myClass but have exception:
Caused by: java.lang.NoClassDefFoundError: application/Application
at rcp.handlers.RunGenerationHandler.execute(RunGenerationHandler.java:29)
at org.eclipse.ui.internal.handlers.HandlerProxy.execute(HandlerProxy.java:294)
at org.eclipse.ui.internal.handlers.E4HandlerProxy.execute(E4HandlerProxy.java:90)
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.e4.core.internal.di.MethodRequestor.execute(MethodRequestor.java:55)
... 38 more
Caused by: java.lang.ClassNotFoundException: application.Application cannot be found by rcp_1.0.0.qualifier
at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:432)
at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:345)
at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:337)
at org.eclipse.osgi.internal.loader.ModuleClassLoader.loadClass(ModuleClassLoader.java:160)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 46 more
I'm sure that class exists in Library.
I try to rebuild myLibrary.jar and add in project again but problem still stays.
Below is the code where I try to call static method from my class:
import application.Application;
...
if (generationString != null){
Application.main(generationString);
}
Application - class in myLibrary.jar
ldl = myLibrary
UPDATE
I'm really such a slowpoke=(
In RCP applications libraries attaching in MANIFEST.MF
Example:Bundle-ClassPath: .,/directory/myLibrary.jar

If your Library is also a RCP-Plugin you have to add it in the dependencies of your plugin.xml in your project.
In the Library plugin.xml you have to export the package where the class is in.

Related

Executable .jar file doesn't run

I have a problem with my .jar file.
It work's perfect when I run it in IDEA, but when I build it in jar file, application ignore double click.
I tried run it in cmd line and received next exceptions:
Exception in Application start method
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 com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
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 sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
at java.lang.Thread.run(Unknown Source)
Caused by: javafx.fxml.LoadException:
file:/C:/Users/__it/Desktop/Ilab%20API%20UI%20+%20CODE%20v%202.0/ApplicationExampleForClients%20V_1.0%20(CODE)/out/artifacts/ApplicationExampleForClients_V_1_0_jar/ApplicationExampleForClients%20V_1.0.jar!/Interface.fxml
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2543)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at Main.start(Main.java:27)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
... 1 more
Caused by: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[56,1]
Message: Stream closed
at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(Unknown Source)
at javax.xml.stream.util.StreamReaderDelegate.next(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2513)
... 17 more
Exception running application Main
I import to the project one library, when it worked in java 1.8 only, jar application hes been worked, but when I updated library for all java version, import to project, and run it, received this Stack Trace.
[Edit]
Changes in lib:
private static void LoadPlatformPacketTypesFromAssembly() {
Reflections reflections = new Reflections("com.intenselab.jlabframework");
Set < Class < ? extends Object>> allProtocolMessages =reflections.getTypesAnnotatedWith(IntenseLabPacket.class);
allProtocolMessages.forEach(aClass -> {
crc32.reset();
crc32.update(aClass.getSimpleName().getBytes());
messageTypesMap.put(aClass, (int) crc32.getValue());
});
}
Was without "com.intenselab.jlabframework"
It won't work as you cannot access files in JAR from filesystem (which is what File API do)
File:/C:/Users/__it/Desktop/Ilab%20API%20UI%20+%20CODE%20v%202.0/ApplicationExampleForClients%20V_1.0%20(CODE)/out/artifacts/ApplicationExampleForClients_V_1_0_jar/ApplicationExampleForClients%20V_1.0.jar!/Interface.fxml
ApplicationExampleForClients%20V_1.0.jar! says that you want to access file that is inside JAR, and that will not work as your filesystem has no direct access to it. You should use resources to access project specific material from JAR.
https://docs.oracle.com/javase/8/docs/technotes/guides/lang/resources.html#class
so you will end up with something similar to
InputStream in=SomeClass.class.getResourceAsStream("Interface.fxml")
Make sure to add all the required jar files to the exported jar file, when building/exporting your application. There is a description on who to do this: Correct way to add external jars (lib/*.jar) to an IntelliJ IDEA project

Adding Jar Plugins Dynamically, but issuing NoClassDefFoundError

I am recently making a PluginClassLoader to load java plugins to my programs dynamically. So that the plugins can interacts with my program.
However, things won't go nicely though. So I looked up with bukkit's implementation of loading plugins. [Bukkit's source code] I followed with it, Java still throws me a NoClassDefFoundError.
Here's my implementation:
All the plugins that to be added are extended to com.mob41.sakura.plugin.Plugin. This abstract class contains onCallPlugin(), onEndPlugin(), etc. functions for the main program to interact to them. [Abstract class source code]
All the plugins will be placed in a /plugins folder on the working directory (System.getProperty("user.dir") + "\\plugins"). My program will find the folder (/plugins) on the working directory. If it doesn't exist, it creates a new folder. If exists, it will find all the jar files. And create a PluginClassLoader [Source Code] to add the plugins.
I expected the plugins will be added to the my program and the classloader will create their instances. Through this testing code:
public static void main(String[] args) {
System.out.println("Loading...");
try {
PluginManager.getPluginManager().loadAllPlugins();
} catch (Exception e){
e.printStackTrace();
return;
}
System.out.println("Loaded.");
JSONObject json = new JSONObject();
json.put("ext", false);
System.out.println((JSONObject) PluginManager.getPluginManager().runPluginLifeCycle("Sakura-Plugin-HKOWeather", json));
}
If the plugin invalid/causes error when loading it, this exception will be thrown.
I don't know why my program cannot find the plugin that extended itself...
com.mob41.sakura.plugin.exception.InvalidPluginException: java.lang.NoClassDefFoundError: com/mob41/sakura/plugin/Plugin
at com.mob41.sakura.plugin.PluginManager.loadPlugin(PluginManager.java:180)
at com.mob41.sakura.plugin.PluginManager.loadAllPlugins(PluginManager.java:145)
at com.mob41.sakura.Main.main(Main.java:12)
Caused by: java.lang.NoClassDefFoundError: com/mob41/sakura/plugin/Plugin
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 java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at com.mob41.sakura.plugin.PluginClassLoader.<init>(PluginClassLoader.java:25)
at com.mob41.sakura.plugin.PluginManager.loadPlugin(PluginManager.java:176)
... 2 more
Caused by: java.lang.ClassNotFoundException: com.mob41.sakura.plugin.Plugin
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 17 more
Through hours of debugging, I finally found the answer.
I was specifying a wrong ClassLoader to my implementation of PluginClassLoader.

How to add external jar

I have added an external jar(for jar Visit jarlink) on my java project but I do not have all features of that library.
I added jar following this way click left on my project and properties->Java Build Path->Libraries tabs->Add External Jar.
Is this the right way ? or I am doing it wrong?
for example link Visit mkyong.com/java/java-find-location-using-ip-address I am trying to do this example .I have added that jar on my project but some imports do not work
I have added geoip2-0.7.0.jar (import com.maxmind.*) this import is working but (import com.maxmind.geoip.regionName;) this import is not working
errors : Exception in thread "main" java.lang.NoClassDefFoundError: ServerLocation
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: ServerLocationenter code here
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 most natural way to do this, is to create a lib folder, put your libs in there, then add the JARs to the build path.
If you're using Maven, you just need to specify the dependency groupId and artifactId, and nothing else.
To add external jar,
if you use eclipse find out here.
If you generate a project using maven, Add a dependency in pom.xml. Find sample here.

ClassNotFoundException In a Bukkit java Plugin

I'm trying to revive a plugin and im geting thir error:
[03:01:18 ERROR]: Could not load 'plugins\Reporter.jar' in folder 'plugins'
org.bukkit.plugin.InvalidPluginException: java.lang.ClassNotFoundException: me.f
levasgr.rainbow.Reporter
at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.j
ava:184) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.
java:308) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager
.java:231) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
at org.bukkit.craftbukkit.v1_7_R1.CraftServer.loadPlugins(CraftServer.ja
va:255) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
at org.bukkit.craftbukkit.v1_7_R1.CraftServer.<init>(CraftServer.java:23
3) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
at net.minecraft.server.v1_7_R1.PlayerList.<init>(PlayerList.java:63) [c
raftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
at net.minecraft.server.v1_7_R1.DedicatedPlayerList.<init>(SourceFile:14
) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
at net.minecraft.server.v1_7_R1.DedicatedServer.init(DedicatedServer.jav
a:126) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java
:424) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:6
17) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
Caused by: java.lang.ClassNotFoundException: me.flevasgr.rainbow.Reporter
at java.net.URLClassLoader$1.run(Unknown Source) ~[?:1.7.0_25]
at java.net.URLClassLoader$1.run(Unknown Source) ~[?:1.7.0_25]
at java.security.AccessController.doPrivileged(Native Method) ~[?:1.7.0_
25]
at java.net.URLClassLoader.findClass(Unknown Source) ~[?:1.7.0_25]
at org.bukkit.plugin.java.PluginClassLoader.findClass0(PluginClassLoader
.java:80) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.
java:53) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.7.0_25]
at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.7.0_25]
at java.lang.Class.forName0(Native Method) ~[?:1.7.0_25]
at java.lang.Class.forName(Unknown Source) ~[?:1.7.0_25]
at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.j
ava:173) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
... 9 more
I've done everything right! The plugin.yml the external jar and the export but still doesnt work. Any idea why?
As the error states,
java.lang.ClassNotFoundException: me.f
levasgr.rainbow.Reporter
it cannot find the main class of your plugin. Perhaps one of your package names is capitalized, or your main class is lowercase. Bukkit's plugin loader is case sensitive when it comes to loading the main class.
There are different reasons for a java.lang.ClassNotFoundException in bukkit, the most common reason is that the plugin.yml doesn't point to the correct location.
If you have the following class:
package me.flevasgr.rainbow.reporter;
//...
public class Reporter {
Then you should copy the full package, place a dot and then place the name of the class, like this:
main: me.flevasgr.rainbow.reporter.Reporter
This makes sure bukkit will find your class on the correct location.

Gate setup: official TestGate.java cannot run

new to Gate, actually also not old to Java and Eclipse ;) and want to run the first test of setup.
what I have done:
download gate-7.0-build4195-ALL and unzip it to C:\
change enviroment variable CLASSPATH to ".;%JAVA_HOME%\lib;%JAVA_HOME%\lib\tools.jar;C:\gate-7.0-build4195-ALL\bin\gate.jar;C:\gate-7.0-build4195-ALL\lib"
created a new java project in eclipse, and navigate to gate.jar\gate\TestGate.class and run it.
then I get the following error:
Failed to invoke suite(): java.lang.NoClassDefFoundError: org/apache/log4j/Logger
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.junit.runner.junit3.JUnit3TestLoader.getTest(JUnit3TestLoader.java:108)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestLoader.loadTests(JUnit3TestLoader.java:59)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.NoClassDefFoundError: org/apache/log4j/Logger
at gate.Gate.<clinit>(Gate.java:83)
at gate.TestGate.suite(TestGate.java:126)
... 10 more
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger
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)
... 12 more
did I miss something?
thank you
You shouldn't set your CLASSPATH environment variable. If you want to use JAR files in an Eclipse project you should add them to your project's build path.
For GATE specifically, you need bin/gate.jar and all the JAR files from lib, but you don't need the lib directory itself on your build path.
I am using GATE API in java with netbeans and when I have integrated a test with junit I have had that error. The trick is to build the project without doint the test, once time built you can run the test as normally using GATE.
For example i have got this at the beginning in my test func:
#Test
public void test()
{
if(false)
{
assertEquals(true, true);
return;
}
...
Testing GATE
To build the jar without test, you only need to set to true overthere. It seems something about classpath of the jar used by GATE.

Categories