I have tried to run a simple game using Java and keep getting an error when I try to run this application.
Exception in thread "main" java.lang.NoClassDefFoundError: sun/misc/Unsafe ...
I think my problem is that my JDK (12.0.2) does not support these Slick2D as it is "OutDated". If downgrading my Java Version is a solution can someone please explain how I can find a version of Java that supports Slick2D?
I have already set up my libraries in my IDE and set my path. I tried to run the same application in the newest Intellij and eclipse environments. I currently have JDK 12.0.2 installed and a 2015 version of LWJGL plus Slick2D installed.
package classPackage;
import org.newdawn.slick.*;
public class Game extends BasicGame{
public Game(String gamename) {
super(gamename);
}
#Override
public void init(GameContainer gc) throws SlickException {
}
#Override
public void update(GameContainer gc, int i) throws SlickException {
}
#Override
public void render(GameContainer gc, Graphics g) throws SlickException {
g.drawString("Howdy!", 10, 10);
}
public static void main(String[] args) {
try {
AppGameContainer appgc;
appgc = new AppGameContainer(new Game("Simple Slick Game"));
appgc.setDisplayMode(640, 480, false);
appgc.start();
} catch (SlickException ex) {
}
}
}
Error:
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:375)
at lwjgl/org.lwjgl.MemoryUtil.<clinit>(MemoryUtil.java:63)
at lwjgl/org.lwjgl.opengl.WindowsDisplay.setTitle(WindowsDisplay.java:522)
at lwjgl/org.lwjgl.opengl.Display.setTitle(Display.java:541)
at lwjgl/org.lwjgl.opengl.Display.createWindow(Display.java:312)
at lwjgl/org.lwjgl.opengl.Display.create(Display.java:848)
at lwjgl/org.lwjgl.opengl.Display.create(Display.java:757)
at slick/org.newdawn.slick.AppGameContainer.tryCreateDisplay(AppGameContainer.java:302)
at slick/org.newdawn.slick.AppGameContainer.access$000(AppGameContainer.java:34)
at slick/org.newdawn.slick.AppGameContainer$2.run(AppGameContainer.java:353)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:310)
at slick/org.newdawn.slick.AppGameContainer.setup(AppGameContainer.java:348)
at slick/org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:317)
at testGame/classPackage.Game.main(Game.java:31)
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
I was able to get this issue resolved by downgrading my jdk to 8. Thank you all I hope this will help someone in the future.
Related
I am trying to render a sketch with IntelliJ however when I select the P3D renderer mode, the program throws an error. I assume it has something to do with OpenGL, but I cant find anything about it online. Code is below.
import processing.core.PApplet;
public class GUI extends PApplet {
public GUI() {
}
public void settings() {
System.setProperty("jogl.disable.openglcore", "false");
fullScreen(P3D);
}
public void draw() {
background(0);
ellipse(mouseX, mouseY, 20, 20);
}
public static void main(String... args) {
PApplet.main("GUI");
}
}
The error is
java.lang.NoClassDefFoundError: com/jogamp/opengl/GLException
at processing.opengl.PGraphicsOpenGL.createPGL(PGraphicsOpenGL.java:717)
at processing.opengl.PGraphicsOpenGL.<init>(PGraphicsOpenGL.java:569)
at processing.opengl.PGraphics3D.<init>(PGraphics3D.java:35)
at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:67)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:483)
at processing.core.PApplet.makeGraphics(PApplet.java:1932)
at processing.core.PApplet.createPrimaryGraphics(PApplet.java:2011)
at processing.core.PApplet.initSurface(PApplet.java:10260)
at processing.core.PApplet.runSketch(PApplet.java:10221)
at processing.core.PApplet.main(PApplet.java:9997)
at processing.core.PApplet.main(PApplet.java:9979)
at GUI.main(GUI.java:20)
Caused by: java.lang.ClassNotFoundException: com.jogamp.opengl.GLException
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 13 more
java.lang.RuntimeException: com/jogamp/opengl/GLException
at processing.core.PApplet.makeGraphics(PApplet.java:1965)
at processing.core.PApplet.createPrimaryGraphics(PApplet.java:2011)
at processing.core.PApplet.initSurface(PApplet.java:10260)
at processing.core.PApplet.runSketch(PApplet.java:10221)
at processing.core.PApplet.main(PApplet.java:9997)
at processing.core.PApplet.main(PApplet.java:9979)
at GUI.main(GUI.java:20)
Process finished with exit code 1
Thanks - any help is appreciated!
I'm using the Processing library in a simple Java project and I tried to make 3D but I got errors, this is my code :
package fr.ayfri;
import processing.core.PApplet;
import processing.core.PConstants;
public class Main extends PApplet {
public void settings() {
size(1600, 900, PConstants.P3D); // <--- P3D this tells Processing to work with 3D
}
public void draw() {
background(140, 190, 255);
}
public static void main(String[] args) {
String[] processingArgs = { "Main" };
Main main = new Main();
PApplet.runSketch(processingArgs, main);
}
}
But when I run the code I get this error :
java.lang.NoClassDefFoundError: com/jogamp/opengl/GLCapabilitiesImmutable
at processing.opengl.PGraphicsOpenGL.createPGL(PGraphicsOpenGL.java:712)
at processing.opengl.PGraphicsOpenGL.<init>(PGraphicsOpenGL.java:569)
at processing.opengl.PGraphics3D.<init>(PGraphics3D.java:35)
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.Constructor.newInstance(Constructor.java:481)
at processing.core.PApplet.makeGraphics(PApplet.java:2266)
at processing.core.PApplet.createPrimaryGraphics(PApplet.java:2345)
at processing.core.PApplet.initSurface(PApplet.java:10983)
at processing.core.PApplet.runSketch(PApplet.java:10922)
at fr.ayfri.Main.main(Main.java:20)
Caused by: java.lang.ClassNotFoundException: com.jogamp.opengl.GLCapabilitiesImmutable
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 13 more
java.lang.RuntimeException: com/jogamp/opengl/GLCapabilitiesImmutable
at processing.core.PApplet.makeGraphics(PApplet.java:2299)
at processing.core.PApplet.createPrimaryGraphics(PApplet.java:2345)
at processing.core.PApplet.initSurface(PApplet.java:10983)
at processing.core.PApplet.runSketch(PApplet.java:10922)
at fr.ayfri.Main.main(Main.java:20)
Process finished with exit code 1
So I just don't know how to make this works ?
P3D uses OpenGL behind the scenes and you need to link against the native libraries.
In this case it's the jogl and gluegen-rt libraries present in Processing/Java/core/libraries: simply copy the correct jars for your platform (or all if you want to run the project on multiple platforms) and add them to the build path.
Here's an eclipse OSX example:
It's very interesting to use Class.forName() to load a class and get errors... I write one line code and get an error as above.
class UnsatisfiedLinkErrorTest {
public static void main(String[] args) throws ClassNotFoundException {
System.out.println(Class.forName("java.net.SocketOutputStream"));
}
}
When I use java UnsatisfiedLinkErrorTest, I get an error:
> java UnsatisfiedLinkErrorTest
Exception in thread "main" java.lang.UnsatisfiedLinkError: java.net.SocketOutputStream.init()V
at java.net.SocketOutputStream.init(Native Method)
at java.net.SocketOutputStream.<clinit>(SocketOutputStream.java:44)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:259)
at UnsatisfiedLinkErrorTest.main(UnsatisfiedLinkErrorTest.java:3)
But when I run it in Idea, I get:
> java "-javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=49247:/Applications/IntelliJ IDEA.app/Contents/bin" UnsatisfiedLinkErrorTest
class java.net.SocketOutputStream
I don't know why in Idea I can get the true answer, (maybe jni?) I hope someone can tell me the reason. Thanks.
This question has been solved. I then cancelled the initialization of the class, then the error goes. The error is about the initialization, but not about finding the native library. So:
class UnsatisfiedLinkErrorTest {
public static void main(String[] args) throws ClassNotFoundException {
System.out.println(Class.forName("java.net.SocketOutputStream", false, null)); // will be okay!
}
}
I'm trying to run Processing from Eclipse but whenever I run the application as a Java Application I get java.lang.ClassNotFoundException.
Here's my full code:
import processing.core.PApplet;
public class App extends PApplet {
public void setup() {
size(200,200);
background(0);
}
public void draw() {
stroke(255);
if (mousePressed) {
line(mouseX,mouseY,pmouseX,pmouseY);
}
}
public static void main(String args[]) {
PApplet.main(new String[] { "--present", "App" });
}
}
Exception:
java.lang.ClassNotFoundException: App
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at processing.core.PApplet.main(PApplet.java:6522)
at com.WordClouds.App.main(App.java:17)
I had the same problem with the class name correctly set and also the package. This solved my issue:
PApplet.main(new String[] { "--present", MyProcessingSketch.class.getName() });
You are probably doing this tuturial : http://www.learningprocessing.com/tutorials/processing-in-eclipse/
You probably overlooked this :
public static void main(String args[]) {
PApplet.main(new String[] { "--present", "MyProcessingSketch" });
}
Note that the String “MyProcessingSketch” must match the name of your class (and if it is in a package, should include the package, i.e. packagename.MyProcessingSketch).
My Java is very rusty and I'm having a bit of a headache here.
Here's my server.java:
import java.io.IOException;
import net.tootallnate.websocket.WebSocket;
import net.tootallnate.websocket.WebSocketServer;
public class server extends WebSocketServer
{
public server(int port)
{
super(port, Draft.AUTO);
}
public void onClientOpen(WebSocket conn)
{
System.out.println("onopen");
/*try
{
this.sendToAll(conn+" entered the room!");
}
catch(IOException ex)
{
ex.printStackTrace();
}*/
}
public void onClientClose(WebSocket conn)
{
System.out.println("onclose");
}
public void onClientMessage(WebSocket conn,String message)
{
System.out.println("onmessage"+message);
}
public static void main(String[] args)
{
int port = 8000;
try
{
port=Integer.parseInt(args[0]);
}
catch(Exception ex)
{
}
server s = new server(port);
s.start();
System.out.println("ChatServer started on port: "+s.getPort());
}
}
I need to include a jar file: ./Java-WebSocket/dist/WebSocket.jar (available from http://github.com/TooTallNate/Java-WebSocket/)
It compiles OK:
prompt> javac -classpath ./Java-WebSocket/dist/WebSocket.jar server.java
However, when I go to run:
prompt> java -classpath ./Java-WebSocket/dist/WebSocket.jar server
Exception in thread "main" java.lang.NoClassDefFoundError: server
Caused by: java.lang.ClassNotFoundException: server
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: server. Program will exit.
I hope someone can help.
Thanks in advance,
java -classpath .:./Java-WebSocket/dist/WebSocket.jar server
^^
Or on Windows:
java -classpath .;./Java-WebSocket/dist/WebSocket.jar server
^^
The classpath defaults to the current directory. You need to point to your classes as well as the library.
Isn't server placed into a package ? If it is, specify the full class name.
e.g. prompt> java -classpath ./Java-WebSocket/dist/WebSocket.jar app.core.server
I would also uppercase the class name, though.
Have you tried using the full class name (your.package.server)?