Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.NoClassDefFoundError (libgdx)

Been trying to work with the Libgdx framework and have been experiencing some frustrating issues.. I'm following a tutorial and I'm unable to run my project. What am I doing wrong?

package com.mygdx.game.android;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class AndroidLauncher implements ApplicationListener {

    SpriteBatch batch;
    Texture mario;


    @Override
    public void create() {
        batch = new SpriteBatch();

        mario = new Texture(Gdx.files.internal("mario.png"));
    }

    @Override
    public void resize(int width, int height) {
        // TODO Auto-generated method stub

    }

    @Override
    public void render() {
        Gdx.gl.glClearColor(1,1,1,1);


        batch.begin();
        batch.draw(mario, 50, 50);
        batch.end();



    }

    @Override
    public void pause() {
        // TODO Auto-generated method stub

    }

    @Override
    public void resume() {
        // TODO Auto-generated method stub

    }

    @Override
    public void dispose() {
        // TODO Auto-generated method stub

    }

}

Here's what the console displays when I run the project:

Exception in thread "main" java.lang.NoClassDefFoundError: com/badlogic/gdx/jnigen/NativeCodeGenerator
    at com.badlogic.gdx.utils.GdxBuild.main(GdxBuild.java:34)
Caused by: java.lang.ClassNotFoundException: com.badlogic.gdx.jnigen.NativeCodeGenerator
    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:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 1 more
like image 741
user3355817 Avatar asked Aug 01 '14 12:08

user3355817


4 Answers

I had the same problem, and the only way I could reproduce it was by using a desktop configuration. So instead of using a desktop configuration, run it as a gradle config. In the command line, that would be:

gradlew desktop:run

Since it's a pain manually doing that every single time you want to run the project, create a new Gradle run configuration

Press the plus icon and select "Gradle"

If you pick the Gradle project in the run config to be the sub module, all you need to type as the task is gradlew run instead of gradlew desktop:run.

You have to define the memory flags in the VM option input (does not have to have these values though):

-Xmx2G -Xms128M

Script parameters can remain empty. Note that this entire thing counts as a Gradle build, so you have to kill the process (if it's running) before you can launch a new one.

I can export a jar without problems, so you should be fine in production.

TL:DR; Use a Gradle run config with gradlew desktop:run instead of using an application

like image 137
Zoe stands with Ukraine Avatar answered Oct 04 '22 13:10

Zoe stands with Ukraine


I had the same problem, but realised that I was running GdxBuild instead. Ensure that you right click on DesktopLauncher.java > Run As > Java Application.

like image 8
Lee Jun Xiang Avatar answered Oct 16 '22 00:10

Lee Jun Xiang


In my case it was my first libGDX project, I was running it using Android Studio version 3.0.1 on KUBUNTU, and as soon as I imported the freshly created project using gdx-setup.jar (Setup App) to Android Studio a popup message was asking informing me to update Gradle which I did, and that broke the project.

enter image description here

Solution: create a new libGDX project and import it without updating Gradle (click "Don't remind me again for this project") and it should work.

like image 3
Waqleh Avatar answered Oct 15 '22 22:10

Waqleh


Rigth click project- properties - build options - order and export and check all gdx libraries. Thats probably it, that error comes when it cant find a class at runtime.

like image 2
Mero Avatar answered Oct 15 '22 22:10

Mero