简体   繁体   中英

Cannot export default Java libGDX project as a jar from Eclipse

When I export the default libgdx project as a runnable JAR file from eclipse and then run the .jar file, it simply pops up a black screen and then closes.

When I run the project code from inside Eclispe, it works fine. The correct red screen with the "bad logic" logo is displayed by the program (this is what is supposed to happen).

If I build the project from the command line using gradle with the command

gradlew desktop:dist

as instructed here

and then run the jar that is created, it will simply open up show a black screen for a second and then close.

The code being used is the default Java libgdx project code:

package com.mygdx.game.tesprac;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class TestLibgdx extends ApplicationAdapter {
SpriteBatch batch;
Texture img;

@Override
public void create () {
    batch = new SpriteBatch();
    img = new Texture("badlogic.jpg");
}

@Override
public void render () {
    Gdx.gl.glClearColor(1, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    batch.begin();
    batch.draw(img, 0, 0);
    batch.end();
}
}

Desktop Launcher:

package com.mygdx.game.tesprac.desktop;

import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.mygdx.game.tesprac.TestLibgdx;

public class DesktopLauncher {
    public static void main (String[] arg) {
        LwjglApplicationConfiguration config = new     LwjglApplicationConfiguration();
        new LwjglApplication(new TestLibgdx(), config);
    }
}

Additional information

I Finally found the solution and will post it here for others to see.

1.) Ensure the assets folder from the android project is 'linked' in the desktop project. To do this, in Package Explorer drag the assets folder down to the desktop project and when prompted, select the option that allows the folders to be linked.

2.) Right click the desktop project select properties -> build path, then add the android project to the build path under the 'projects' tab.

3.) In the android project right-click the assets folder and select build path -> 'use as source'

4.) Right click the desktop project then select: Export -> Java -> Runnable JAR file, select the 'Package required libraries into generated JAR' option -> Finish.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM