Why is my classloader throwing ClassNotFoundException? - java

I have an abc.jar and I'm loading a class using reflection. But I'm getting a ClassNotFoundException. My code is as below:
File file1 = new File(ApplicationContext.getPath()+"lib/abc.jar");
Class noparams[] = {};
URL url = file1.toURI().toURL();
URL[] urls = new URL[]{url};
#SuppressWarnings("resource")
ClassLoader cl = new URLClassLoader(urls);
cls = cl.loadClass("com.abcd.feat.udf.MobileUDF");
Constructor<?> c = cls.getConstructor();
mobileUDFActions = c.newInstance();
I have verified that the jar is present in ApplicationContext.getPath()/lib folder.
Contents of jar are appropriate. It contains classes in the package com/abcd/feat/udf.
Please help me realize what is the mistake. I'm getting below exception.
java.lang.ClassNotFoundException: com.abcd.feat.udf.MobileUDF
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 com.abcd.feat.core.TestScriptExecutor.<init>(TestScriptExecutor.java:85)

Related

Loading Modules when the ModuleLoader is a Dependency

I'm currently working on a minecraft plugin that needs to load different Modules. However, the plugin that loads the modules is also a dependency for each Module. Because of this, when I try to load a module, it throws a NoClassDefFoundError. How can I use the module loader as a dependency for the module aswell?
Code for module loader:
private void loadModules() throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
if(SaturnPrisons.getInstance().getDataFolder() == null || !SaturnPrisons.getInstance().getDataFolder().exists()){
return;
}
File dir = new File(SaturnPrisons.getInstance().getDataFolder() + File.separator + "modules");
if(!dir.exists()){
dir.mkdir();
return;
}
if(dir.listFiles() == null){
return;
}
for(File file: dir.listFiles()) {
if (file.isDirectory()) {
continue;
}
String[] arrayOfFile = file.getName().split("\\.");
if (!arrayOfFile[arrayOfFile.length - 1].equalsIgnoreCase("jar")) {
continue;
}
JarFile jarFile = new JarFile(file);
JarEntry jarEntry = jarFile.getJarEntry("module.yml");
if (jarEntry == null) {
System.out.println(file.getName() + " could not load! module.yml missing!");
continue;
}
InputStream inputStream = jarFile.getInputStream(jarEntry);
Map<String, Map<String, String>> values = new Yaml().load(inputStream);
if (!values.containsKey("main")) {
System.out.println(file.getName() + " could not load! Main Class not in module.yml!");
continue;
}
String main = "" + values.get("main");
URLClassLoader loader = new URLClassLoader(new URL[]{file.toURI().toURL()});
Class<?> jarClass = loader.loadClass(main);
Object instance = jarClass.newInstance();
if (!(instance instanceof SaturnModule)) {
System.out.println(file.getName() + " could not load! Main Class was not a SaturnModule class!");
continue;
}
SaturnModule saturnModule = (SaturnModule) instance;
saturnModule.load(SaturnPrisons.getInstance());
System.out.println("Module Loaded: " + file.getName());
}
Error
[00:15:16 ERROR]: Error occurred while enabling SaturnPrisons v3.00-SNAPSHOT (Is it up to date?)
java.lang.NoClassDefFoundError: com/saturnplugins/saturnprisons/modules/SaturnModule
at java.lang.ClassLoader.defineClass1(Native Method) ~[?:1.8.0_291]
at java.lang.ClassLoader.defineClass(Unknown Source) ~[?:1.8.0_291]
at java.security.SecureClassLoader.defineClass(Unknown Source) ~[?:1.8.0_291]
at java.net.URLClassLoader.defineClass(Unknown Source) ~[?:1.8.0_291]
at java.net.URLClassLoader.access$100(Unknown Source) ~[?:1.8.0_291]
at java.net.URLClassLoader$1.run(Unknown Source) ~[?:1.8.0_291]
at java.net.URLClassLoader$1.run(Unknown Source) ~[?:1.8.0_291]
at java.security.AccessController.doPrivileged(Native Method) ~[?:1.8.0_291]
at java.net.URLClassLoader.findClass(Unknown Source) ~[?:1.8.0_291]
at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_291]
at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_291]
at com.saturnplugins.saturnprisons.modules.ModuleManager.loadModules(ModuleManager.java:77) ~[?:?]
at com.saturnplugins.saturnprisons.modules.ModuleManager.<init>(ModuleManager.java:21) ~[?:?]
at com.saturnplugins.saturnprisons.SaturnPrisons.onEnable(SaturnPrisons.java:16) ~[?:?]
at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:264) ~[spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:337) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:403) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugin(CraftServer.java:381) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugins(CraftServer.java:330) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
at net.minecraft.server.v1_12_R1.MinecraftServer.t(MinecraftServer.java:422) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
at net.minecraft.server.v1_12_R1.MinecraftServer.l(MinecraftServer.java:383) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
at net.minecraft.server.v1_12_R1.MinecraftServer.a(MinecraftServer.java:338) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
at net.minecraft.server.v1_12_R1.DedicatedServer.init(DedicatedServer.java:272) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:545) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
at java.lang.Thread.run(Unknown Source) [?:1.8.0_291]
Caused by: java.lang.ClassNotFoundException: com.saturnplugins.saturnprisons.modules.SaturnModule
at java.net.URLClassLoader.findClass(Unknown Source) ~[?:1.8.0_291]
at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_291]
at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_291]
... 25 more```

java.lang.ClassNotFoundException when trying to load derived class

I have two different Modules: MultiRealm and SurvivalRealm. MultiRealm provides an abstract class (LoadedRealm) which the class SurvivalRealm (in the Module SurvivalRealm) extends. Now, MultiRealm tries to load the SurvivalRealm class from the jar. But when trying to load, i get the Following error: java.lang.NoClassDefFoundError: net/lightbluefoxlabs/dev/multirealm/core/multirealmcore/Realms/LoadedRealm where net/lightbluefoxlabs/dev/multirealm/core/multirealmcore/Realms/LoadedRealm is the correct location of the LoadedRealm class. Here is the Stacktrace:
[23:03:52] [Thread-10/WARN]: at java.lang.ClassLoader.defineClass1(Native Method)
[23:03:52] [Thread-10/WARN]: at java.lang.ClassLoader.defineClass(Unknown Source)
[23:03:52] [Thread-10/WARN]: at java.security.SecureClassLoader.defineClass(Unknown Source)
[23:03:52] [Thread-10/WARN]: at java.net.URLClassLoader.defineClass(Unknown Source)
[23:03:52] [Thread-10/WARN]: at java.net.URLClassLoader.access$100(Unknown Source)
[23:03:52] [Thread-10/WARN]: at java.net.URLClassLoader$1.run(Unknown Source)
[23:03:52] [Thread-10/WARN]: at java.net.URLClassLoader$1.run(Unknown Source)
[23:03:52] [Thread-10/WARN]: at java.security.AccessController.doPrivileged(Native Method)
[23:03:52] [Thread-10/WARN]: at java.net.URLClassLoader.findClass(Unknown Source)
[23:03:52] [Thread-10/WARN]: at java.lang.ClassLoader.loadClass(Unknown Source)
[23:03:52] [Thread-10/WARN]: at java.lang.ClassLoader.loadClass(Unknown Source)
[23:03:52] [Thread-10/WARN]: at net.lightbluefoxlabs.dev.multirealm.core.multirealmcore.Realms.LoadedRealm.RealmFactory(LoadedRealm.java:54)
[23:03:52] [Thread-10/WARN]: at net.lightbluefoxlabs.dev.multirealm.core.multirealmcore.Realms.LoadedRealm.RealmFactory(LoadedRealm.java:20)
[23:03:52] [Thread-10/WARN]: at net.lightbluefoxlabs.dev.multirealm.core.multirealmcore.Realms.RealmManager.Initialize(RealmManager.java:22)
[23:03:52] [Thread-10/WARN]: at net.lightbluefoxlabs.dev.multirealm.core.multirealmcore.Realms.RealmManager.lambda$InitializeAsync$0(RealmManager.java:39)
[23:03:52] [Thread-10/WARN]: at java.lang.Thread.run(Unknown Source)
[23:03:52] [Thread-10/WARN]: Caused by: java.lang.ClassNotFoundException: net.lightbluefoxlabs.dev.multirealm.core.multirealmcore.Realms.LoadedRealm
[23:03:52] [Thread-10/WARN]: at java.net.URLClassLoader.findClass(Unknown Source)
[23:03:52] [Thread-10/WARN]: at java.lang.ClassLoader.loadClass(Unknown Source)
[23:03:52] [Thread-10/WARN]: at java.lang.ClassLoader.loadClass(Unknown Source)
[23:03:52] [Thread-10/WARN]: ... 16 more
Here is the SurvivalRealm class:
import net.lightbluefoxlabs.dev.multirealm.core.multirealmcore.Realms.LoadedRealm;
import net.lightbluefoxlabs.dev.multirealm.core.multirealmcore.Realms.Worlds.RealmWorld;
public class SurvivalRealm extends LoadedRealm {
public SurvivalRealm(String RealmPath) {
super(RealmPath);
Load();
}
#Override
public void Load() {
//LoadStuff
}
#Override
public void Unload() {
}
}
... and this static function in LoadedRealm tries to import this class:
public static LoadedRealm RealmFactory(String jarPath, String RealmPath) throws RealmLoadException{
try {
File file = new File(jarPath);
URLClassLoader c = new URLClassLoader(new URL[]{file.getAbsoluteFile().toURI().toURL()});
Class<?> realmClass = c.loadClass(RealmClass);
return (LoadedRealm)(realmClass.getDeclaredConstructor(String.class).newInstance(RealmPath));
}catch (Exception e){
throw new RealmLoadException(e);
}
}
Here is a picture of the Structure:
The MultiRealm Module does not import/"Know about" (excuse my terminology) the SurvivalRealm module, only the other way around.
Am I just trying to achieve something impossible? If you need the .iml file, just ask. Any help would be really apreciated, I have been trying to fix this issue for basically the whole day now.
Thanks for reading through this beginner issue.
You should try using your pom files from maven for achieve your goal. A multi-module project is defined by a parent POM referencing one or more submodules.

java.lang.NoClassDefFoundError: org/openscience/cdk/Atom

I am having a problem with use of this open source development kit - https://github.com/cdk/cdk
Here is the java code for BasicProgram:
import org.openscience.cdk.interfaces.IAtom;
import org.openscience.cdk.Atom;
public class BasicProgram {
public static void main(String args[]) throws Exception {
IAtom atom = new Atom("C");
System.out.println(atom);
}
}
When I go to compile the class file is generated without errors, but doesn't seem to find the references in the jar file:
E:\Cheminformatics>javac -cp cdk-2.0.jar BasicProgram.java
E:\Cheminformatics>java -cp . BasicProgram
Exception in thread "main" java.lang.NoClassDefFoundError: org/openscience/cdk/Atom
at BasicProgram.main(BasicProgram.java:6)
Caused by: java.lang.ClassNotFoundException: org.openscience.cdk.Atom
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

Error Invoking Maven

I am trying to invoke maven from a Java class.
My code:
import java.io.File;
import java.util.Arrays;
import javax.xml.ws.spi.Invoker;
import org.apache.maven.shared.invoker.DefaultInvocationRequest;
import org.apache.maven.shared.invoker.DefaultInvoker;
import org.apache.maven.shared.invoker.InvocationRequest;
import org.apache.maven.shared.invoker.MavenInvocationException;
public class MavenProjectBuilder implements ProjectBuilder{
public void BuildProject(String buildPath, String bugID) {
InvocationRequest request = new DefaultInvocationRequest();
request.setPomFile(new File(buildPath));
request.setGoals(Arrays.asList( "clean", "install" ));
DefaultInvoker invoker = new DefaultInvoker();
try {
invoker.execute(request);
} catch (MavenInvocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Which returns the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/codehaus/plexus/util/cli/StreamConsumer
at autoEvoSuite.MavenProjectBuilder.BuildProject(MavenProjectBuilder.java:21)
at autoEvoSuite.Main.<init>(Main.java:110)
at tests.GeneralTest.main(GeneralTest.java:18)
Caused by: java.lang.ClassNotFoundException: org.codehaus.plexus.util.cli.StreamConsumer
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)
... 3 more
I did some research and found out it could be a missing dependency, but shouldn't Maven automatically download all missing ones?
Please check if the below dependency is present in your POM.xml, until the dependency is defined in you pom file that jar will not be downloaded to your local repository.
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
<version>3.0.17</version>
</dependency>

Wso2 ESB - Generated Java Client Error

When I generated a Java Client through the web page of wso2esb for a deployed service "Hello". In addition to the HelloServiceCallBackHandler.java and HelloServiceStub.java I created the class entitled HelloClient.java:
package org.wso2.cs.helloservices;
import java.rmi.RemoteException;
import org.wso2.cs.helloservices.HelloServiceStub.SayHello;
import org.wso2.cs.helloservices.HelloServiceStub.SayHelloResponse;
public class HelloClient {
public static void main(String[] args) {
try {
String name= new String("Test");
HelloServiceStub stub= new HelloServiceStub();
SayHello sh= new SayHello();
sh.setName(name);
SayHelloResponse resp=stub.sayHello(sh);
System.out.println("Response : "+resp);
}catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
I have the following error:
<font color="red"> Exception in thread "main" java.lang.NoClassDefFoundError: javax/wsdl/extensions/soap/SOAPAddress
at org.wso2.cs.helloservices.HelloServiceStub.populateAxisService(HelloServiceStub.java:41)
at org.wso2.cs.helloservices.HelloServiceStub.<init>(HelloServiceStub.java:88)
at org.wso2.cs.helloservices.HelloServiceStub.<init>(HelloServiceStub.java:77)
at org.wso2.cs.helloservices.HelloServiceStub.<init>(HelloServiceStub.java:126)
at org.wso2.cs.helloservices.HelloServiceStub.<init>(HelloServiceStub.java:118)
at org.wso2.cs.helloservices.HelloClient.main(HelloClient.java:13)
Caused by: java.lang.ClassNotFoundException: javax.wsdl.extensions.soap.SOAPAddress
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</font>
This seems to be required jars not found (This complains about wsld4j). Please add AXIS2_HOME/lib folder to your program's classpath .

Categories