Why am I getting the error "NoClassDefFoundError: sun/misc/Unsafe" when trying to load sounds using the LWJGL library?
Why am I getting the error "NoClassDefFoundError: sun/misc/Unsafe" when trying to load sounds using the LWJGL library?
这是我的 Eclipse 项目文件的屏幕截图。
当我尝试在第 15 行的 class AudioPlayer
中启动一个 new Sound("Res/MouseClick.ogg")
对象时,出现以下错误。
package com.game;
import java.util.HashMap;
import java.util.Map;
import org.newdawn.slick.Music;
import org.newdawn.slick.Sound;
public class AudioPlayer {
public static Map<String, Sound> soundMap = new HashMap<String, Sound>();
public static Map<String, Music> musicMap = new HashMap<String, Music>();
public static void load() {
try {
soundMap.put("menu_sound", new Sound("Res/MouseClick.ogg")); // <- throws error
musicMap.put("music", new Music("Res/Background.ogg"));
} catch (Exception e) {
e.printStackTrace();
}
}
public static Music getMusic(String key) {
return musicMap.get(key);
}
public static Sound getSound(String key) {
return soundMap.get(key);
}
}
Exception in thread "main" java.lang.NoClassDefFoundError: sun/misc/Unsafe
at lwjgl/org.lwjgl.MemoryUtilSun$AccessorUnsafe.getUnsafeInstance(MemoryUtilSun.java:74)
at lwjgl/org.lwjgl.MemoryUtilSun$AccessorUnsafe.<init>(MemoryUtilSun.java:62)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
at java.base/java.lang.reflect.ReflectAccess.newInstance(ReflectAccess.java:166)
at java.base/jdk.internal.reflect.ReflectionFactory.newInstance(ReflectionFactory.java:404)
at java.base/java.lang.Class.newInstance(Class.java:590)
at lwjgl/org.lwjgl.MemoryUtil.loadAccessor(MemoryUtil.java:324)
at lwjgl/org.lwjgl.MemoryUtil.<clinit>(MemoryUtil.java:66)
at lwjgl/org.lwjgl.openal.ALC10.alcOpenDevice(ALC10.java:202)
at lwjgl/org.lwjgl.openal.AL.init(AL.java:160)
at lwjgl/org.lwjgl.openal.AL.create(AL.java:138)
at lwjgl/org.lwjgl.openal.AL.create(AL.java:102)
at lwjgl/org.lwjgl.openal.AL.create(AL.java:201)
at slick/org.newdawn.slick.openal.SoundStore.run(SoundStore.java:295)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:310)
at slick/org.newdawn.slick.openal.SoundStore.init(SoundStore.java:292)
at slick/org.newdawn.slick.Sound.<init>(Sound.java:54)
at Wave/com.game.AudioPlayer.load(AudioPlayer.java:15)
at Wave/com.game.Game.<init>(Game.java:37)
at Wave/com.game.Game.main(Game.java:157)
Caused by: java.lang.ClassNotFoundException: sun.misc.Unsafe
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 23 more
从您发布的 IDE 屏幕截图我可以看出您正在使用
- Java 12,并且
- Java 模块系统 (
module-info.java
)
请注意,sun/misc/Unsafe
是(遗留的)Java 内部 API 用于在使用 Java 模块系统时,这个原因默认情况下被封装(隐藏)了。
为了让您的应用程序(和您使用的库)访问它,您需要通过添加 requires jdk.unsupported;
将其明确包含在您的 module-info.java
中
或者,您也可以不使用 Java 模块系统——在这种情况下它应该可以正常工作。但是,我建议继续使用模块系统。
这是我的 Eclipse 项目文件的屏幕截图。
当我尝试在第 15 行的 class AudioPlayer
中启动一个 new Sound("Res/MouseClick.ogg")
对象时,出现以下错误。
package com.game;
import java.util.HashMap;
import java.util.Map;
import org.newdawn.slick.Music;
import org.newdawn.slick.Sound;
public class AudioPlayer {
public static Map<String, Sound> soundMap = new HashMap<String, Sound>();
public static Map<String, Music> musicMap = new HashMap<String, Music>();
public static void load() {
try {
soundMap.put("menu_sound", new Sound("Res/MouseClick.ogg")); // <- throws error
musicMap.put("music", new Music("Res/Background.ogg"));
} catch (Exception e) {
e.printStackTrace();
}
}
public static Music getMusic(String key) {
return musicMap.get(key);
}
public static Sound getSound(String key) {
return soundMap.get(key);
}
}
Exception in thread "main" java.lang.NoClassDefFoundError: sun/misc/Unsafe
at lwjgl/org.lwjgl.MemoryUtilSun$AccessorUnsafe.getUnsafeInstance(MemoryUtilSun.java:74)
at lwjgl/org.lwjgl.MemoryUtilSun$AccessorUnsafe.<init>(MemoryUtilSun.java:62)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
at java.base/java.lang.reflect.ReflectAccess.newInstance(ReflectAccess.java:166)
at java.base/jdk.internal.reflect.ReflectionFactory.newInstance(ReflectionFactory.java:404)
at java.base/java.lang.Class.newInstance(Class.java:590)
at lwjgl/org.lwjgl.MemoryUtil.loadAccessor(MemoryUtil.java:324)
at lwjgl/org.lwjgl.MemoryUtil.<clinit>(MemoryUtil.java:66)
at lwjgl/org.lwjgl.openal.ALC10.alcOpenDevice(ALC10.java:202)
at lwjgl/org.lwjgl.openal.AL.init(AL.java:160)
at lwjgl/org.lwjgl.openal.AL.create(AL.java:138)
at lwjgl/org.lwjgl.openal.AL.create(AL.java:102)
at lwjgl/org.lwjgl.openal.AL.create(AL.java:201)
at slick/org.newdawn.slick.openal.SoundStore.run(SoundStore.java:295)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:310)
at slick/org.newdawn.slick.openal.SoundStore.init(SoundStore.java:292)
at slick/org.newdawn.slick.Sound.<init>(Sound.java:54)
at Wave/com.game.AudioPlayer.load(AudioPlayer.java:15)
at Wave/com.game.Game.<init>(Game.java:37)
at Wave/com.game.Game.main(Game.java:157)
Caused by: java.lang.ClassNotFoundException: sun.misc.Unsafe
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 23 more
从您发布的 IDE 屏幕截图我可以看出您正在使用
- Java 12,并且
- Java 模块系统 (
module-info.java
)
请注意,sun/misc/Unsafe
是(遗留的)Java 内部 API 用于在使用 Java 模块系统时,这个原因默认情况下被封装(隐藏)了。
为了让您的应用程序(和您使用的库)访问它,您需要通过添加 requires jdk.unsupported;
module-info.java
中
或者,您也可以不使用 Java 模块系统——在这种情况下它应该可以正常工作。但是,我建议继续使用模块系统。