LWJGL Package is sealed - java

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!

Related

Why am I getting this error for my LWJGL program?

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.

jcurses errors while try to import in IntelliJ

I`m trying to import jcurses library to IntelliJ but get an error.
File -> Project Structure -> Modules -> import jar file.
Then in code:
import jcurses.widgets.Window;
class main {
public static void main(String[] args){
Window win = new Window(800, 600, true, "test");
win.setVisible(true);
}
Exception in thread "main" java.lang.ExceptionInInitializerError
at jcurses.system.InputChar.<clinit>(InputChar.java:25)
at jcurses.widgets.Window.<clinit>(Window.java:209)
at main.main(main.java:7)
Caused by: java.lang.RuntimeException: couldn't find jcurses library
at jcurses.system.Toolkit.getLibraryPath(Toolkit.java:121)
at jcurses.system.Toolkit.<clinit>(Toolkit.java:37)
Could someone point out where is my mistake?
Thanks in advance!
The answer is that dll must be in the same directory as jar file. Thanks to everyone!
Libray download url
https://sourceforge.net/projects/javacurses/files/javacurses/0.9.5/
The path of the library is in the root directory of the project
dynamically add the library
import java.io.File;
import java.lang.reflect.Field;
import java.util.Arrays;
public class LoadLibrary {
public static void main(String cor[]) throws Exception {
// Cargando librerias necesarias
loadLibraryJCourses();
}
public static void loadLibraryJCourses() throws Exception{
loadDynamicLibrary(new File("lib/bin/jcourses/").getAbsolutePath(),"libjcurses64");
}
private static void addLibraryPath(String pathToAdd) throws Exception {
final Field usrPathsField = ClassLoader.class.getDeclaredField("usr_paths");
usrPathsField.setAccessible(true);
//get array of paths
final String[] paths = (String[]) usrPathsField.get(null);
//check if the path to add is already present
for (String path : paths) {
if (path.equals(pathToAdd)) {
return;
}
}
//add the new path
final String[] newPaths = Arrays.copyOf(paths, paths.length + 1);
newPaths[newPaths.length - 1] = pathToAdd;
usrPathsField.set(null, newPaths);
}
private static void loadDynamicLibrary(String pathLibraryDirectory, String libraryName) throws Exception{
File pathLibraryFile = new File(pathLibraryDirectory);
addLibraryPath(pathLibraryFile.getAbsolutePath());
System.loadLibrary(libraryName);
}
}

GdxRuntimeException: Error reading file

I am learning and programming a game for android using libgdx and i have got stuck on this error for quite a long period of time. I have enclosed the following content
The code.
The command i used to access the asset.
Screenshot that contains the error when i debug the code on android.
Screenshot of my Package Explorer.
All the possible combinations i tried to get the code working.
1.The Code
package com.me.mygdxgame;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.assets.AssetErrorListener;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.utils.Disposable;
public class Gameassetloader implements Disposable, AssetErrorListener {
public static final String TAG = Gameassetloader.class.getName();
public static Gameassetloader instance = new Gameassetloader();
private AssetManager assetManager;
/** Load the appropriate texture for the respective entities **/
public AssetCharacter boy;
public AssetRock rock;
public AssetShield shield;
/** singleton: prevent instantiation from other classes **/
private Gameassetloader(){}
public void init(AssetManager assetManager)
{
this.assetManager = assetManager;
//set asset manager error handler
assetManager.setErrorListener(this);
//load texture atlas
assetManager.load(Gameconstants.TEXTURE_ATLAS_OBJECTS,TextureAtlas.class);
//start loading assets and wait until finished
assetManager.finishLoading();
Gdx.app.debug(TAG, "# of assets loaded:" + assetManager.getAssetNames().size);
for (String a : assetManager.getAssetNames())
Gdx.app.debug(TAG, "asset:" + a);
TextureAtlas atlas = assetManager.get(Gameconstants.TEXTURE_ATLAS_OBJECTS);
//enable texture filtering for pixel smoothing
for(Texture t : atlas.getTextures())
t.setFilter(TextureFilter.Linear,TextureFilter.Linear);
//create game resource objects
boy = new AssetCharacter(atlas);
rock = new AssetRock(atlas);
shield = new AssetShield(atlas);
}
public void dispose() { assetManager.dispose(); }
#Override
public void error(String fileName, Class type, Throwable throwable)
{
Gdx.app.error(TAG, "Couldn't load asset' " + fileName + "'", (Exception)throwable);
}
}
2.The Command
public static final String TEXTURE_ATLAS_OBJECTS ="gdxgame-android/assets/packed/packed.png";
3.Screenshot of Package Explorer
http://i1294.photobucket.com/albums/b608/Abhishek_M369/PackageExplorer_zpsd2589ee2.jpg
4.Screenshot of Error log (Android DDMS)
http://i1294.photobucket.com/albums/b608/Abhishek_M369/ErrorLog_zps8a4a68d9.jpg
5.All the possible combinations i tried
"/gdxgame-android/assets/packed/packed.png";
"gdxgame-android/assets/packed/packed.png";
"/assets/packed/packed.png";
"assets/packed/packed.png";
"/packed/packed.png";
"packed/packed.png";
"/packed.png";
"packed.png";
public static FileHandle location = Gdx.files.internal("assets/packed.png");
public static final TEXTURE_ATLAS_OBJECT = location.toString();
//this caused shutdown of emulator
public static FileHandle location = Gdx.files.internal("assets/packed.png");
public static final TEXTURE_ATLAS_OBJECT = location.readString();
// I even tried setting the build path of the asset folder as source folder
// Also tried placing the image in the data folder.
// Tried using the absolute path too , i.e Gdx.files.absolute("the absolute");
// Tried passing the absolute path directly as string
Nothing seems to work for me.
The problem was very simple, the problem lied in the different versions of libgdx and the documentation. The answer explained below will solely confirm to libgdx version 0.9.8 only.
(Note: The usage of Texturepacker GUI was used to pack textures && not the method from the libgdx library)
First the "assetManager" had to be supplied with a file that contains the coordinates of the images which was a mistake here as i was supplying the packed image.
The GL20 should be able to parse NPOT images but it was unable to do so and the reason remains unknown so i had to pack the texture to a POT which was accomplished by selecting the POT options in the GUI. After doing this i was able to load the newly POT image easily with the following code
/**Mention only the folder/file under the asset dir**/
public class Gameconstants { public static final String location = "packed/packed.txt" }
/**access the same using the following command**/
private AssetManager assetManager;
assetManager.load(Gameconstants.location,Texture.class);
This Answer may not be very convincing but it surely solved my problem.
Thank you to all who helped :)

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);
}
}

Categories

Resources