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```
Related
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.
I've been trying to understand what the issue is exactly, but whatever I do doesn't seem to work.
I have a text file that lists names along with numbers, seperated by a colon.
An example of this is:
Betty Ross:52
Angie Scotts:29
Michael Rosen:72
The list is very long and comprises over 10,000 lines.
public class PeopleIds {
public static int UNDEFINED_ID = -1;
private static HashMap<String, Integer> people;
public static void initialize() {
people = new HashMap<String, Integer>();
System.out.println(new File("res/ids/people_ids.txt").exists());
try {
Files.lines(Paths.get("res/ids/people_ids.txt")).forEach(s -> {
people.put(s.replaceAll(":.*", "").trim(), Integer.parseInt(s.replaceAll(".*:", "")));
});
} catch (IOException e) {
System.out.println("Unable to read specified file.");
e.printStackTrace();
}
}
public static int getId(final String name) {
final Integer id = people.get(name);
return id != null ? id : UNDEFINED_ID;
}
}
I call the initialize method from a GUIController class:
public class GUIController implements Initializable {
#FXML
private TableView<PersonData> personTable;
#FXML
private TableColumn<PersonData, String> name;
#FXML
private TableColumn<PersonData, Integer> limit;
#FXML
private TextField searchInput;
#FXML
private ImageView personIcon;
private Image undefinedIcon;
private PersonIcon icon;
private ObservableList<PersonData> data;
#Override
public void initialize(URL location, ResourceBundle resources) {
PeopleIds.initialize();
undefinedIcon = new Image(getClass().getResourceAsStream("/ids/no.png"));
name.setCellValueFactory(new PropertyValueFactory<PersonData, String>("name"));
limit.setCellValueFactory(new PropertyValueFactory<PersonData, Integer>("limit"));
data = PriceData.getData();
personTable.setPeople(data);
searchInput.textProperty().addListener((ov, oldValue, newValue) -> {
final String input = searchInput.getText();
if (input.length() == 0) return;
searchInput.setText(input.substring(0, 1).toUpperCase() + input.substring(1).toLowerCase());
filterSearch();
});
}
}
When I call it from this class with PeopleIds.initialize(), an exception is thrown, telling me that there was an exception in the application start method.
Here is what was logged in its entirety:
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(Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplication(Unknown Source)
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(Unknown Source)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$156(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: javafx.fxml.LoadException:
/C:/Confidential/bin/base/PersonGUI.fxml
at javafx.fxml.FXMLLoader.constructLoadException(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.load(Unknown Source)
at base.PersonGUI.start(PersonGUI.java:13)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$163(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$174(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$149(Unknown Source)
... 1 more
Caused by: java.io.UncheckedIOException: java.nio.charset.MalformedInputException: Input length = 1
at java.io.BufferedReader$1.hasNext(Unknown Source)
at java.util.Iterator.forEachRemaining(Unknown Source)
at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Unknown Source)
at java.util.stream.ReferencePipeline$Head.forEach(Unknown Source)
at base.PeopleIds.initialize(PeopleIds.java:17)
at base.GUIController.initialize(GUIController.java:36)
... 18 more
Caused by: java.nio.charset.MalformedInputException: Input length = 1
at java.nio.charset.CoderResult.throwException(Unknown Source)
at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at java.io.InputStreamReader.read(Unknown Source)
at java.io.BufferedReader.fill(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
... 24 more
Exception running application base.PersonGUI
I'm not sure what is going on here? I've looked into it and people have said to move the fxml file (the one that is used to format the content and is linked with the GUIController to the same package as the Main class, however it already is.
I've been wrestling with this issue for days to no avail. Do any of you have past experiences with this issue? If so, how did you resolve it? Thanks a lot.
If there is an Exception while the file is being read, not when opening the file, an unchecked exception is thrown for the Files.lines stream operation (Stream.forEach doesn't have a throws clause).
This happens here
Files.lines(Paths.get("res/ids/people_ids.txt")).forEach(s -> {
people.put(s.replaceAll(":.*", "").trim(), Integer.parseInt(s.replaceAll(".*:", "")));
});
, which you can easily see in the stacktrace:
Caused by: java.io.UncheckedIOException: java.nio.charset.MalformedInputException: Input length = 1
(This is caused by the wrong Charset being used, see Files.readAllBytes vs Files.lines getting MalformedInputException )
You don't catch this kind of exception with your catch clause:
} catch (IOException e) {
You need to use
} catch (Exception e) {
to catch the unchecked exceptions too.
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)
I installed Junit and tried to follow one of to tutorial videos on YouTube. I added junit-4.11 to my CLASSPATH correctly but when I run my Junit test, I get initializationError. echo %CLASSPATH% shows the junit-4.11.jar correctly. What could be causing this?
package test;
public class Junit {
public String concatenate(String one, String two)
{
return one + two;
}
public int multiply(int number1, int number2)
{
return number1 * number2;
}
}
package test;
import static org.junit.Assert.*;
import org.junit.Test;
public class MultiplyTest {
#Test
public void testMultiply() {
Junit test = new Junit();
int result = test.multiply(3,4);
assertEquals(12, result);
}
}
java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing
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 org.junit.internal.builders.JUnit4Builder.runnerForClass(JUnit4Builder.java:10)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:26)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:33)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit4TestClassReference.java:25)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:48)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:444)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.ClassNotFoundException: org.hamcrest.SelfDescribing
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)
... 25 more
You need to add hamcrest-all.jar to your classpath also. This question is answered here: java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing
I got java.lang.ClassNotFoundException:
Exception in thread "main" java.lang.NoClassDefFoundError: Consts/SortOfPages
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: Consts.SortOfPages
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
and this is the class of SortOfPages:
package Consts;
public enum SortOfPages {
POPUP("New - "),
SILENT("Silent - ");
public String description;
private SortOfPages(String description) {
this.description = description;
}
public String getDescription() {
return description;
}
}
in the import of the main class:
import Consts.SortOfPages;
But when I search the Const/SortOfPages, I find it, and it exists.
I try to clean, and build again - and the same.
How Can I Solve it?