Unable to find the right JVM options - java

Alright, first and foremost, I am new to Java, but not to programming.
For a personnal project I decided that I should use Slick2D, a graphics library in java, and I am running into one problem I haven't been able to fix for now.
Considering the following:
I use Netbeans
I'm currently on linux
From what I understand, slick2d uses lwgjl. I have followed the instructions on the following page:
Setting up Slick2D for netbeans
My problem I think is giving my JVM the right options.
Currently the ones I set are:
-Djava.library.path=/home/karel/Téléchargements/slick/native/unix
Here is the current error output I get:
run:
Exception in thread "main" java.lang.UnsatisfiedLinkError: no lwjgl64 in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1867)
at java.lang.Runtime.loadLibrary0(Runtime.java:870)
at java.lang.System.loadLibrary(System.java:1122)
at org.lwjgl.Sys$1.run(Sys.java:72)
at java.security.AccessController.doPrivileged(Native Method)
at org.lwjgl.Sys.doLoadLibrary(Sys.java:66)
at org.lwjgl.Sys.loadLibrary(Sys.java:87)
at org.lwjgl.Sys.<clinit>(Sys.java:117)
at org.lwjgl.opengl.Display.<clinit>(Display.java:135)
at org.newdawn.slick.AppGameContainer$1.run(AppGameContainer.java:39)
at java.security.AccessController.doPrivileged(Native Method)
at org.newdawn.slick.AppGameContainer.<clinit>(AppGameContainer.java:36)
at jcoinche.client.JcoincheClient.main(JcoincheClient.java:29)
/home/karel/.cache/netbeans/8.1/executor-snippets/run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)
My code is as follow (well, the part that needs a library, and doesn't work - it's all tutorial material, by the way. Nothing I made myself):
package jcoinche.client;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.util.Scanner;
import static jcoinche.client.Game.gamename;
import static jcoinche.client.Game.xSize;
import static jcoinche.client.Game.ySize;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.SlickException;
public class JcoincheClient
{
public static void main(String[] args)
{
Socket socket;
AppGameContainer appgc;
try
{
appgc = new AppGameContainer(new Game(gamename));
appgc.setDisplayMode(xSize, ySize, false);
appgc.setTargetFrameRate(60);
appgc.start();
}
catch(SlickException e)
{
e.printStackTrace();
}
}
Game.java:
package jcoinche.client;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
public class Game extends StateBasedGame
{
public static final String gamename = "MyGameName";
public static final int play = 0;
public static final int xSize = 400;
public static final int ySize = 300;
public Game(String gamename){
super(gamename);
this.addState(new Play());
}
public void initStatesList(GameContainer gc) throws SlickException{
this.getState(play).init(gc, this);
this.enterState(play);
}
}
Play.java:
package jcoinche.client;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
public class Play extends BasicGameState
{
public Play()
{
}
public void init(GameContainer gc, StateBasedGame sbg)
throws SlickException
{
}
public void render(GameContainer gc, StateBasedGame sbg, Graphics g)
throws SlickException
{
}
public void update(GameContainer gc, StateBasedGame sbg, int delta)
throws SlickException
{
}
public int getID()
{
return 0;
}
}

The error is telling you of the absence of a file named 'lwjgl64'. You are getting an UnsatisfiedLinkError, which according to the Java Documentation, is:
Thrown if the Java Virtual Machine cannot find an appropriate native-language definition of a method declared native.
Now, are you using Slick2D alone? Or do you have LWJGL and Slick2D? Slick2D is not standalone; you need LWJGL to run it. I suggest getting LWJGL from their site, and add it's native folder to the classpath.
From the Slick2D docs, they write this:
-Djava.library.path=<lwjgl-X.X path>/native/<linux|macosx|solaris|windows>
Source

There are a few answers on stack that may help you solve this issue as it is clear from the error that it is caused by a linking error to LWJGL64. One post with a number of solutions here offers a number of solutions, though targeted at the eclipse IDE specifically the general solution applies:
To quote from here #Ben Jackson
LWJGL needs the native components for your particular platform to be
in java.library.path. These are in the subdirectory native in the
LWJGL distribution and end in .so on Linux, OSX and Solaris and .dll
for windows.
Other potential examples solving the error:
Setting up natives

Alright, this is a bit embarassing, but I understood what my problem was and it was a simple misunderstanding of the link I gave (instructions on how to install and use slick2D on netbeans):
In fact, my problem was that, because of the lwgjl.jar and other similar files in the slick2D folder, I thought lwgjl was, in the end, included in the slick folder.
As such, for the last step, I gave to my JVM a path directing it to the slick library folder, while it was, this time, asking for lwgjl files, causing the errors shown above.
I changed that path to the lwgjl/native/linux folder, and everything is now working perfectly.
Thanks for your answers, they helped greatly.

Related

Can't use LWJGL correctly, code giving errors

I'm trying to run this code that is used to see if things were setup properly:
package simpleslickgame;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
public class SimpleSlickGame extends BasicGame
{
public SimpleSlickGame(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 SimpleSlickGame("Simple Slick Game"));
appgc.setDisplayMode(640, 480, false);
appgc.start();
}
catch (SlickException ex)
{
Logger.getLogger(SimpleSlickGame.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
All of the Googling I've done has led me to checking the natives over and over and I"m pretty sure I've done it right. When I go to the libraries in this project's build path, JRE System library and Slick2D both say Native library location: .../windows/x64. I've tried just .../windows, and I've tried it with the JRE not having a native location. I followed two different tutorials on how to do this and I keep getting errors trying to run this simple code. Any help?
basically what you need is to add slick.jar and lwjgl.jar to your build path and then right click your project >> open properties >> click on Java Build Path >> open Libraries tab >> expand JRE System Library >> click Native library location >> edit >> External Folder >> find ..\lwjgl-2.9.0\native\windows folder on your hard drive >> select it >> and you're done
g.drawString("Howdy!", 10, 10);
I would just move this string to some other location, cause it's showing over the FPS counter :)
EDIT:
you can use the natives from ..\slick\lib\natives-windows.jar.
open it with WinRAR or something and extract to a folder, then just set that folder to be your Native library location

JNA library and native library not found error

I want to use JNA to detect foreground application on Linux (Ubuntu 14). I followed this link
Find out what application (window) is in focus in Java
but I got the following error:
Exception in thread "main" java.lang.UnsatisfiedLinkError: Unable to load library 'XLib': Native library (linux-x86-64/libXLib.so) not found in resource path ([file:/home/zzhou/workspace/home_prioritization_plus/bin/, file:/home/zzhou/Downloads/jna-4.1.0.jar, file:/home/zzhou/Downloads/jna-platform-4.1.0.jar])
at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:271)
at com.sun.jna.NativeLibrary.getInstance(NativeLibrary.java:398)
at com.sun.jna.Library$Handler.<init>(Library.java:147)
at com.sun.jna.Native.loadLibrary(Native.java:412)
at com.sun.jna.Native.loadLibrary(Native.java:391)
at FunctionalityTest$XLib.<clinit>(FunctionalityTest.java:15)
at FunctionalityTest.main(FunctionalityTest.java:23)
The code is:
import com.sun.jna.Native;
import com.sun.jna.Platform;
import com.sun.jna.Pointer;
import com.sun.jna.platform.unix.X11;
import com.sun.jna.win32.StdCallLibrary;
public class FunctionalityTest {
static class Psapi {
static { Native.register("psapi"); }
public static native int GetModuleBaseNameW(Pointer hProcess, Pointer hmodule, char[] lpBaseName, int size);
}
public interface XLib extends StdCallLibrary {
XLib INSTANCE = (XLib) Native.loadLibrary("XLib", Psapi.class); // <-- PROBLEM
int XGetInputFocus(X11.Display display, X11.Window focus_return, Pointer revert_to_return);
}
public static void main(String args[]) {
if(Platform.isLinux()) { // Possibly most of the Unix systems will work here too, e.g. FreeBSD
final X11 x11 = X11.INSTANCE;
final XLib xlib= XLib.INSTANCE;
X11.Display display = x11.XOpenDisplay(null);
X11.Window window=new X11.Window();
xlib.XGetInputFocus(display, window,Pointer.NULL);
X11.XTextProperty name=new X11.XTextProperty();
x11.XGetWMName(display, window, name);
System.out.println(name.toString());
}
}
}
To import JNA library, I downloaded jna and jna-platform from https://github.com/twall/jna and use Configure Build Path... in Eclipse to add library. I did not install anything. May that be the source of the problem?
Thanks for your help.
Afaik, even for JNA, you have to load the library in Java in order for JNA to find it. (tested for win32, not linux)
Try this just above Native.loadLibrary:
System.loadLibrary("XLib");

passing argument to WordCram code

I have the following question: I am trying to execute the usConstitution wordcram example (code follows) and if provided as is the code executes in eclipse, the applet starts and the word cloud is created. (code follows)
import processing.core.*;
//import processing.xml.*;
import wordcram.*;
import wordcram.text.*;
import java.applet.*;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.event.MouseEvent;
import java.awt.event.KeyEvent;
import java.awt.event.FocusEvent;
import java.awt.Image;
import java.io.*;
import java.net.*;
import java.text.*;
import java.util.*;
import java.util.zip.*;
import java.util.regex.*;
public class usConstitution extends PApplet {
/*
US Constitution text from http://www.usconstitution.net/const.txt
Liberation Serif font from RedHat: https://www.redhat.com/promo/fonts/
*/
WordCram wordCram;
public void setup() {
size(800, 600);
background(255);
colorMode(HSB);
initWordCram();
}
public void initWordCram() {
wordCram = new WordCram(this)
.fromTextFile("http://www.usconstitution.net/const.txt")
.withFont(createFont("https://www.redhat.com/promo/fonts/", 1))
.sizedByWeight(10, 90)
.withColors(color(0, 250, 200), color(30), color(170, 230, 200));
}
public void draw() {
if (wordCram.hasMore()) {
wordCram.drawNext();
}
}
public void mouseClicked() {
background(255);
initWordCram();
}
static public void main(String args[]) {
PApplet.main(new String[] { "--bgcolor=#ECE9D8", "usConstitution" });
}
}
My problem is the following:
I want to pass through main (which is the only static class) an argument so as to call the usConstitution.class from another class providing whichever valid filename I want in order to produce its word cloud. So how do I do that? I tried calling usConstitution.main providing some args but when I try to simply print the string I just passed to main (just to check if it is passed) I get nothing on the screen. So the question is How can I pass an argument to this code to customize .fromTextFile inside initWordCram ?
Thank you a lot!
from: https://wordcram.wordpress.com/2010/09/09/get-acquainted-with-wordcram/ :
Daniel Bernier says:
June 11, 2013 at 1:13 am
You can’t pass command-line args directly to WordCram, because it has no executable.
But you can make an executable wrapper (base it on the IDE examples that come with WordCram), and it can read command-line args & pass them to WordCram as needed.
FYI, it’ll still pop up an Applet somewhere – AFAIK, you can’t really run Processing “headless.” But that’s usually only a concern if you’re trying to run on a server.

Tiled game libGDX - couldn't load file (Exception in thread "LWJGL Application")

I am following a java tiled game tutorial using libGDX and I met the following errors :
Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: Couldn't load file: gaming creation/herbe16.png
at com.badlogic.gdx.graphics.Pixmap.<init>(Pixmap.java:140)
at com.badlogic.gdx.graphics.glutils.FileTextureData.prepare(FileTextureData.java:64)
at com.badlogic.gdx.graphics.Texture.load(Texture.java:142)
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:133)
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:112)
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:108)
at com.badlogic.gdx.maps.tiled.TmxMapLoader.load(TmxMapLoader.java:119)
at com.badlogic.gdx.maps.tiled.TmxMapLoader.load(TmxMapLoader.java:104)
at com.poussins.screens.Play.show(Play.java:35)
at com.badlogic.gdx.Game.setScreen(Game.java:62)
at com.poussins.Poussins.create(Poussins.java:11)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:136)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)
Caused by: com.badlogic.gdx.utils.GdxRuntimeException: File not found: gaming creation/herbe16.png (Internal)
at com.badlogic.gdx.files.FileHandle.read(FileHandle.java:133)
at com.badlogic.gdx.files.FileHandle.length(FileHandle.java:563)
at com.badlogic.gdx.files.FileHandle.readBytes(FileHandle.java:218)
at com.badlogic.gdx.graphics.Pixmap.<init>(Pixmap.java:137)
... 12 more
here is the code i'm using in the Play class :
package com.poussins.screens;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
public class Play implements Screen{
private TiledMap map;
private OrthogonalTiledMapRenderer renderer;
private OrthographicCamera camera;
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
renderer.setView(camera);
renderer.render();
}
#Override
public void resize(int width, int height) {
camera.viewportWidth = width;
camera.viewportHeight = height;
camera.update();
}
#Override
public void show() {
map = new TmxMapLoader().load("maps/map-Herbe-Goudron.tmx");
renderer = new OrthogonalTiledMapRenderer(map);
camera = new OrthographicCamera();
}
#Override
public void hide() {
dispose();
}
#Override
public void pause() {
// TODO Auto-generated method stub
}
#Override
public void resume() {
// TODO Auto-generated method stub
}
#Override
public void dispose() {
map.dispose();
renderer.dispose();
}
}
In order to put the .tmx map into the Eclipse project, I just made a Copy (from the folder where the .tmx map is) and a paste in the Eclipse project named "Poussins-Desktop / assets / maps". I used the same actions (copy paste) to give the .png tiles to the same Eclipse project named "Poussins-Desktop / assets / maps" (can see it in the screenshots).
In the code, I am not sure with this following line :
map = new TmxMapLoader().load("maps/map-Herbe-Goudron.tmx");
because I don't know if the path to the .tmx is good, but it seem to respect what was said in the tutorial ...
In fact, in the error, it is saying that the file herbe16.png cannot be loaded. but this file is a tile from the whole .tmx map.
I am working on UBUNTU 13.10 system, Eclipse 3.8.1
I hope someone will help me and I thank you in advance for paying attention to my problem.
Okay, I don't know if this will help you..
I was just declaring the path for my files not minding caps letters (I'm using windows7).
Now, when I tested the code as a java application on my desktop it was going smoothly but when I tried launching it on an emulator or an android device it would crash.
Turns out that the emulator and device both need the exact path with capital letters.
This may have already been solved or the OP may have given up on it, but I came across this error too on the same tutorial. I solved the issue in my case. It may be possible that the OP was using a .png of a different bit depth (as was in my case). The .png that caused the error was 64 bit depth (you can check by right clicking on the .png -> PROPERTIES -> DETAILS). Using a .png of a bit depth of 32 allowed the loading to work perfectly fine. I used photoshop to create my .png tileset, and when creating the new file, set it to 8 bit (next to the color mode).

NuiGetSensorCount Java JNA

I am just learning how to use Java JNA, and I am trying to call a simple function from the Microsoft Kinect SDK. (NuiGetSensorCount) which just returns the number of connected kinects.
Here is my attempt:
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;
public class Driver {
public interface KinectLibrary extends Library {
KinectLibrary INSTANCE = (KinectLibrary)Native.loadLibrary(("Microsoft.Kinect"),KinectLibrary.class);
//_Check_return_ HRESULT NUIAPI NuiGetSensorCount( _In_ int * pCount );
NativeLong NuiGetSensorCount(Pointer pCount);
}
public static void main(String[] args) {
Pointer devCount = new Pointer(0);
KinectLibrary.INSTANCE.NuiGetSensorCount(devCount);
System.out.println("Devices:"+devCount.getInt(0));
}
}
But I get:
Exception in thread "main" java.lang.UnsatisfiedLinkError: Error looking up function 'NuiGetSensorCount': The specified procedure could not be found.
at com.sun.jna.Function.<init>(Function.java:208)
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:536)
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:513)
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:499)
at com.sun.jna.Library$Handler.invoke(Library.java:199)
at $Proxy0.NuiGetSensorCount(Unknown Source)
at Driver.main(Driver.java:30)
Can anybody provide help of how to change my code so it finds the correct native function? And also provide some information/reference so that I could try to debug this myself (some way to see what function Java JNA is looking for, and compare it to what the .dll contains)
I figured out my problem. First, I used a program called dependency walker http://dependencywalker.com/ to view all the symbols in the DLL and I determined that the DLL I was using (Microsoft.Kinect.dll) did not actually contain the function I was trying to call. I found out that Kinect10.dll was the one I needed. After changing that, I had to make a small change to my pointer and it works perfectly!
Here is the fixed code.
import com.sun.jna.Native;
import com.sun.jna.NativeLong;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.win32.StdCallLibrary;
public class Driver{
public interface KinectLibrary extends StdCallLibrary {
KinectLibrary INSTANCE = (KinectLibrary)Native.loadLibrary(("Kinect10"),KinectLibrary.class);
//_Check_return_ HRESULT NUIAPI NuiGetSensorCount( _In_ int * pCount );
NativeLong NuiGetSensorCount(IntByReference pCount);
}
public static void main(String[] args) {
IntByReference a = new IntByReference();
KinectLibrary.INSTANCE.NuiGetSensorCount(a);
System.out.println("Devices:"+a.getValue());
}
}

Categories