Why am I getting this error for my LWJGL program? - java

I am trying to make a window with OpenGL (Using LWJGL 2) with Java. When I tried to run, a ClassNotFoundException error came up from the Eclipse BuiltInClassLoader.
I have tried looking for the correct jar file but I couldn't open the jar files to see which packages were inside them. Here is my code:
DisplayManager.java
package renderEngine;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.PixelFormat;
import org.lwjgl.opengl.ContextAttribs;
public class DisplayManager {
private static final int WIDTH = 1280;
private static final int HEIGHT = 720;
private static final int FPS_CAP = 120;
public static void createDisplay(){
ContextAttribs attribs = new ContextAttribs(3, 2);
attribs.withForwardCompatible(true);
attribs.withProfileCore(true);
try{
Display.setDisplayMode(new DisplayMode(WIDTH,
HEIGHT))
Display.create(new PixelFormat(), attribs);
}catch(LWJGLException e){
e.printStackTrace();
}
GL11.glViewport(0, 0, WIDTH, HEIGHT);
}
public static void updateDisplay(){
Display.sync(FPS_CAP);
Display.update();
}
public static void closeDisplay(){
Display.destroy();
}
}
MainGameLoop.java
package engineTester;
import org.lwjgl.opengl.Display;
import renderEngine.DisplayManager;
public class MainGameLoop {
public static void main(String[] args) {
DisplayManager.createDisplay();
while(!Display.isCloseRequested()){
DisplayManager.updateDisplay();
}
DisplayManager.closeDisplay();
}
}
I expected the output to show a window, this is the real output:
Exception in thread "main" java.lang.NoClassDefFoundError:
org/lwjgl/LWJGLException at
engineTester.MainGameLoop.main(MainGameLoop.java:11) Caused by:
java.lang.ClassNotFoundException: org.lwjgl.LWJGLException at
java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:606)
at
java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:168)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
... 1 more

You probably put the jar files for lwjgl in the module path
i had the same error before so to fix this error, Make sure to move all 3 jar files into Classpath which is the lwjgl_util.jar, lwjgl.jar,and slick-util!
To do this, Right-Click the Java Project where the MainGameLoop and DisplayManager is, Then Hover your mouse into build path, and then click Configure Build Path, and go to Libraries, here you will see the 3 jar files. if the jar files are in the Module Path! move it into Classpath, Click Apply and close and then you should be good!
if the problem is still not fixed! then you probably have Java Runtime Environment(JRE) 1.7 or below! if so you can download JRE 1.8 Here: https://www.oracle.com/java/technologies/javase-jre8-downloads.html
hope this helps! and also have a nice day.

Related

java.lang.ClassNotFoundException - Eclipse with OpenCV

When I run my below code I get the below error, but it worked before and I am not sure what Eclipse got and is not good anymore
Error: Unable to initialize main class src.convolution
Caused by: java.lang.NoClassDefFoundError: org/opencv/core/Mat
package src;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
public class convolution {
public static void main( String[] args ) {
try {
int kernelSize = 9;
System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
Mat source = Imgcodecs.imread("C:/Users/B & B/Desktop/ProcIMG/grayscale.jpg", Imgcodecs.IMREAD_GRAYSCALE);
Mat destination = new Mat(source.rows(),source.cols(),source.type());
Mat kernel = new Mat(kernelSize,kernelSize, CvType.CV_32F){
{
put(0,0,-3);
put(0,1,-3);
put(0,2,-3);
put(1,0-3);
put(1,1,0);
put(1,2,-3);
put(2,0,5);
put(2,1,5);
put(2,2,5);
}
};
Imgproc.filter2D(source, destination, -1, kernel);
Imgcodecs.imwrite("C:/Users/B & B/Desktop/ProcIMG/output.jpg", destination);
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}
It could be .classpath issue. Try following steps
Delete Project from Eclipse don't delete content from disk.
remove .classpath and .project files
re-import project in Eclipse
I have the same problem, maybe you have already found your answer but as it is the first result of my web research I want to offer a solution to the others :
Just add the opencv_xxx.jar to the project classpath as external class. and it will work fine. (in Menu project -> properties -> java build path -> libraries tab -> classpath-> add external class folder button)
Hope it help many of you

LWJGL Package is sealed

I'm following along with a youtube tutorial on programming in Java using Slick2D and lwjgl. When I run this code:
package game;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
public class Game extends StateBasedGame{
public static final String gamename = "Ham Blaster!";
public static final int menu = 0;
public static final int play = 1;
public Game(String name) {
super(name);
this.addState(new Menu(menu));
this.addState(new Play(play));
}
public static void main(String[] args){
try{
AppGameContainer appgc;
appgc = new AppGameContainer(new Game(gamename));
appgc.setDisplayMode(640, 360, false);
}catch(SlickException e){
e.printStackTrace();
}
}
#Override
public void initStatesList(GameContainer gc) throws SlickException {
this.getState(menu).init(gc, this);
this.getState(play).init(gc, this);
this.enterState(menu);
}
}
It throws a security exception saying that the package org.lwjgl is sealed. I've added the lwjgl jar file and the two jar files associated with slick (slick2d and lwjgl . jar) to the build path. Is there a solution?
I had the same problem ones. And I Found that going into the Jar file(s) and opening up the Manifest file, and then removing the
Sealed: true
line entirely fixed my problem. And just so you know there would be no need to re package or something if you open the jar file with e.g. Winrar and then editing the file while it is in the jar.
You probably only need to go into the lwjgl jar files.
Hope it works out for you!

Java program runs fine in Eclipse but not as a .jar file

I've got an issue with my .jar file. It runs fine in Eclipse but as soon as I export it, it won't open. I've checked the manifest file and it looks like it's okay.
I've tried exporting it as a runnable jar, as well as just using the jar builder. Nothing worked.
I've tried to run it in command prompt and it says it can't access the jar file... I've searched around a while on here and haven't found an answer yet.
I'm not sure what I'm doing wrong. The only thing I can think of is I'm not getting my images correctly.
I'm using .png files for the program's sprites and here's an example of how I get them for my program.
This code begins the building of a level from a .png file.
public class SpawnLevel extends Level{
public SpawnLevel(String path) {
super(path);
}
protected void loadLevel(String path){
try{
System.out.println("classpath is: " + System.getProperty("java.class.path"));
BufferedImage image = ImageIO.read(SpawnLevel.class.getResource(path));
int w = width = image.getWidth();
int h = height= image.getHeight();
tiles = new int[w*h];
image.getRGB(0,0,w,h,tiles,0,w);
}catch(IOException e){
e.printStackTrace();
System.out.println("EXEPTION FOUND!!! Could not load the level file!");
}
}
protected void generateLevel(){
System.out.println("Tiles: " + tiles[0]);
}
}
I've made one other .jar before for another program and didn't have a problem. Any help would be greatly appreciated.
If it helps, I used this code to display the resource folder path information.
System.out.println("classpath is: " + System.getProperty("java.class.path"));
Here's what my current path for my resources folder looks like. Before I export it from eclipse.
classpath is: C:\Users\name\workspace\Rpg_Game\bin;C:\Users\name\workspace\Rpg_Game\res
After I export to .jar
classpath is: 2ndGameTest.jar
If your images are in your resources package in the src The path you should be using for getResource() is something like
class.getResource("/resources/levels/level1.png")
UPDATE with test program
import java.awt.Image;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
public class TestImage {
public static void main(String[] args) throws IOException {
Image image = ImageIO.read(TestImage.class.getResource("/resources/images/image.png"));
ImageIcon icon = new ImageIcon(image);
JLabel label = new JLabel(icon);
JOptionPane.showMessageDialog(null, label);
}
}

Package doesn't exist in Netbeans, successful compilation in cmd, why?

I have a requirement to capture photos using system webcam. For this, I'm using webcam capture library. It contained three jar files. I wanted to package every thing including dependency jars in single jar application.
So, I sorted to unpack the dependent jar files place them in the src folder of netbeans project.
So, I extracted them. Two of them start with the same package name org, so I extracted and merged them. Important third package starts with com package. I've placed these three directories in src folder of my netbeans project.
Now the problem is that netbeans, says that package doesn't exist. But, the same project when compiled from command line in windows. It compiled and ran successfully, everything was working fine.
But, when I added the problematic library as a jar to the project, it worked fine, only extracting and placing it in src folder causing the problem.
Why is this error occurring and why in netbeans only, how to resolve this problem?
My code:
package screen;
import com.github.sarxos.webcam.*; **//Error here**
import com.github.sarxos.webcam.log.*; **// Error here**
import java.io.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class Util extends TimerTask{
private String filePath;
private String type;
private Timer timer;
private Webcam webcam;
public Util(String path,String type){
this.timer=new Timer("Capture Timer");
this.filePath=path;
this.type=type;
this.webcam=Webcam.getDefault();
}
public void capture() throws IOException{
if(webcam!=null){
webcam.open();
BufferedImage image=webcam.getImage();
Date date=new Date();
ImageIO.write(image,type,new File(filePath+"_"+date.toString().replace(' ','_').replace(':','-')+"."+type.toLowerCase()));
webcam.close();
}
else{
webcam=Webcam.getDefault();
}
}
public void startCapturing(int period){
this.timer.schedule(this,0,period);
}
public void stopCapturing(){
timer.cancel();
}
public void run(){
try{
capture();
System.out.println("Doing");
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String args[]){
Util u=new Util("n082430_","PNG");
u.startCapturing(60000);
}
}

VLCJ NullPointer (I just want a simple cross-platform java video player)

I am wanting to make a simple java applicaiton to play video. I want it to play mpeg4 and mov formats in particular. JMF is what I started with and I have a lovely working example. However, there is no support for mov or mpeg4 formats. I've looked at Xuggler but can't see a SIMPLE way to get it working. VLCJ seemed easy - I downloaded the jar files and attached them to my project (vlcj-2.1.0.jar, jna-3.4.0.jar, platform-3.4.0.jar, vlcj-2.1.0.jar)). I got the sample code and adapted it (below). But when I run the code, I get a java.lang.NullPointerException exception. I've tried adjusting the number and direciton of the slashes (forward and backward) in the filename. Nothing seems to work. Please could you help???
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.JFileChooser;
import uk.co.caprica.vlcj.component.EmbeddedMediaPlayerComponent;
import java.lang.Object;
import uk.co.caprica.vlcj.mrl.FileMrl;
import uk.co.caprica.vlcj.binding.LibVlc;
import uk.co.caprica.vlcj.runtime.RuntimeUtil;
import com.sun.jna.Native;
import com.sun.jna.NativeLibrary;
public class TestPlayer {
private final JFrame frame;
private EmbeddedMediaPlayerComponent mediaPlayer;
public static void loadLibs(){
NativeLibrary.addSearchPath(
RuntimeUtil.getLibVlcLibraryName(), "C:/Program Files/VideoLAN/VLC/"
);
Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);
}
public static void main(final String[] args){
loadLibs();
final String mrl = "file://C:/Test.mov";
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new TestPlayer().run(mrl);
}
});
}
public TestPlayer(){
frame = new JFrame("test VLCJ");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(100,100);
frame.setSize(600,400);
frame.setVisible(true);
}
private void run(String mrl){
System.out.println(mrl);
try{
mediaPlayer.getMediaPlayer().playMedia(mrl);
}catch(Exception e){
System.err.println(e.toString());
}
}
}
I'm using VLC version 2.0.2 and VLCJ 2.1.0 sources and JDK 1.7 on windows 32 bit. I hope it's something simple...
It looks like you are using mediaPlayerwithout ever initializing it, thus causing a NullPointerException in run().
Try initializing it in your constructor.

Categories

Resources