Search code examples
javalwjglnoclassdeffounderror

Why am I getting the error "NoClassDefFoundError: sun/misc/Unsafe" when trying to load sounds using the LWJGL library?


This is the screenshot of my Eclipse project files. Eclipse Screenshot

I get the error below when I try to initiated a new Sound("Res/MouseClick.ogg") object in my class AudioPlayer at line 15.

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$1.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

Solution

  • From your posted IDE screenshot I can see that you are using

    • Java 12, and
    • the Java Module System (module-info.java)

    Note that sun/misc/Unsafe is a (legacy) Java internal API which for that reason and by default is encapsulated (hidden) away when using the Java Module System.

    In order for your application (and your used library) to access it, you need to explicitly include it in your module-info.java by adding requires jdk.unsupported;

    Alternatively, you may also not use the Java Module System – in which case it should simply work. However, I recommend to continue using the module system.