Java Libgdx:导出的桌面jar文件链接到外部路径,而不是jar根目录

Java Libgdx:导出的桌面jar文件链接到外部路径,而不是jar根目录,java,intellij-idea,jar,libgdx,assets,Java,Intellij Idea,Jar,Libgdx,Assets,我正在尝试将我的libgdx应用程序部署到jar桌面文件中。我使用Intellij构建了桌面模块,然后执行./gradlew desktop:dist生成jar,一切都很顺利!我使用资产管理器加载我的资产。现在,当我执行jar时,资产管理器尝试在jar目录中获取资产,而不是直接在jar文件中获取资产 我的资产经理: package com.com8.game.Utils; import com.badlogic.gdx.assets.AssetDescriptor; import com.ba

我正在尝试将我的libgdx应用程序部署到jar桌面文件中。我使用Intellij构建了桌面模块,然后执行./gradlew desktop:dist生成jar,一切都很顺利!我使用资产管理器加载我的资产。现在,当我执行jar时,资产管理器尝试在jar目录中获取资产,而不是直接在jar文件中获取资产

我的资产经理:

package com.com8.game.Utils;

import com.badlogic.gdx.assets.AssetDescriptor;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.assets.loaders.SkinLoader;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;


public class Assets {

public AssetManager manager = new AssetManager();

public static final AssetDescriptor<Texture> logo =
        new AssetDescriptor<Texture>("logo.png", Texture.class);

public static final AssetDescriptor<Texture> tableBack =
        new AssetDescriptor<Texture>("back.png", Texture.class);

public static final AssetDescriptor<Texture> frontBackground =
        new AssetDescriptor<Texture>("frontBackground.png", Texture.class);

public static final AssetDescriptor<Texture> backBackground =
        new AssetDescriptor<Texture>("backBackground.png", Texture.class);

public static final AssetDescriptor<TextureAtlas> uiAtlas =
        new AssetDescriptor<TextureAtlas>("UI/uiskin.atlas", TextureAtlas.class);

public static final AssetDescriptor<Skin> uiSkin =
        new AssetDescriptor<Skin>("UI/uiskin.json", Skin.class,
                new SkinLoader.SkinParameter("UI/uiskin.atlas"));

public static final AssetDescriptor<TextureAtlas> gameAtlas =
        new AssetDescriptor<TextureAtlas>("Atlas/gameImages.atlas", TextureAtlas.class);

public static final AssetDescriptor<TextureAtlas> flamesAnimation =
        new AssetDescriptor<TextureAtlas>("Atlas/flamesAnimation.atlas", TextureAtlas.class);

public static final AssetDescriptor<TextureAtlas> hitAnimation =
        new AssetDescriptor<TextureAtlas>("Atlas/hitAnimation.atlas", TextureAtlas.class);

public static final AssetDescriptor<TextureAtlas> shipExplosionAnimation =
        new AssetDescriptor<TextureAtlas>("Atlas/shipExplosionAnimation.atlas", TextureAtlas.class);

public static final AssetDescriptor<TextureAtlas> dustAnimation =
        new AssetDescriptor<TextureAtlas>("Atlas/dustAnimation.atlas", TextureAtlas.class);


public void load()
{
    manager.load(logo);
    manager.load(tableBack);
    manager.load(frontBackground);
    manager.load(backBackground);
    manager.load(uiAtlas);
    manager.load(uiSkin);
    manager.load(gameAtlas);
    manager.load(flamesAnimation);
    manager.load(hitAnimation);
    manager.load(shipExplosionAnimation);
    manager.load(dustAnimation);
}

public void dispose() {
    manager.dispose();
}
}
有人能帮我吗?:)谢谢

编辑: 我只是在每个路径名前面加了一个“/”,看起来更好! ,文件logo.png位于jar文件的根目录中

下面是我如何使用资产类别:

// Assets
private AssetManager manager;
private Stage stage;
private Skin skin;

// UI
private TextField userIDRegistered;
private TextField userPwd;
private TextField userIDGuest;
private Label authentificationResponse;
private Label socketConnectedMessage;

// Socket
private Socket socket;

public ConnexionScreen(Com8 game) {
    this.game = game;
    this.socket = game.getSocket();
    this.manager = game.assets.manager;

    skin = game.skin;

    stage = game.stage;
    stage.clear();

    configSocketEvents();
}

@Override
public void show() {
    //Create Table
    Table mainTable = new Table();

    //Set table position properties
    mainTable.align(Align.center|Align.center);
    mainTable.setFillParent(true);

    //Create image
    Texture logoTexture = manager.get("/logo.png", Texture.class);
    Image logoImage = new Image(logoTexture);
改变

在桌面模块的
build.gradle
文件中

然后运行
/gradlew desktop:dist

希望您的所有资源都位于
android模块的assets文件夹中

编辑

我测试过了,对我来说很好,找到你做错了什么

方法中的代码create()

AssetDescriptor<Texture> assetDescriptor=new AssetDescriptor<Texture>("brick.png",Texture.class);

AssetManager assetManager=new AssetManager();
assetManager.load(assetDescriptor);   // load by assetDescriptor
assetManager.load("badlogic.jpg", Texture.class);   // manual loading

assetManager.finishLoading();

texture=assetManager.get("badlogic.jpg",Texture.class);  // fetch data manually 
texture1=assetManager.get(assetDescriptor);              // fetch by descriptor
AssetDescriptor AssetDescriptor=新的AssetDescriptor(“brick.png”,Texture.class);
AssetManager AssetManager=新AssetManager();
assetManager.load(assetDescriptor);//按资产描述人加载
load(“badlogic.jpg”,Texture.class);//人工装载
assetManager.finishLoading();
texture=assetManager.get(“badlogic.jpg”,texture.class);//手动获取数据
texture1=assetManager.get(assetDescriptor);//按描述符获取
我在
render()
方法中绘制这两个纹理

资源位于
资产
文件夹中的核心模块内

然后我在桌面模块的
build.gradle
文件中分配资产目录,该文件也在
dist
任务中使用

project.ext.assetsDir=新文件(“../core/assets”)

在终端上运行
/gradlew dist
命令

我在桌面模块的build文件夹中找到了我的
desktop-1.0.jar
。复制并保存在其他文件夹中,双击运行,工作正常

这两个资源(
badlogic.jpg
&
brick.png
)都位于我的
.jar
的上根目录中。

更改

project.ext.assetsDir = new File("../core/assets");

在桌面模块的
build.gradle
文件中

然后运行
/gradlew desktop:dist

希望您的所有资源都位于
android模块的assets文件夹中

编辑

我测试过了,对我来说很好,找到你做错了什么

方法中的代码create()

AssetDescriptor<Texture> assetDescriptor=new AssetDescriptor<Texture>("brick.png",Texture.class);

AssetManager assetManager=new AssetManager();
assetManager.load(assetDescriptor);   // load by assetDescriptor
assetManager.load("badlogic.jpg", Texture.class);   // manual loading

assetManager.finishLoading();

texture=assetManager.get("badlogic.jpg",Texture.class);  // fetch data manually 
texture1=assetManager.get(assetDescriptor);              // fetch by descriptor
AssetDescriptor AssetDescriptor=新的AssetDescriptor(“brick.png”,Texture.class);
AssetManager AssetManager=新AssetManager();
assetManager.load(assetDescriptor);//按资产描述人加载
load(“badlogic.jpg”,Texture.class);//人工装载
assetManager.finishLoading();
texture=assetManager.get(“badlogic.jpg”,texture.class);//手动获取数据
texture1=assetManager.get(assetDescriptor);//按描述符获取
我在
render()
方法中绘制这两个纹理

资源位于
资产
文件夹中的核心模块内

然后我在桌面模块的
build.gradle
文件中分配资产目录,该文件也在
dist
任务中使用

project.ext.assetsDir=新文件(“../core/assets”)

在终端上运行
/gradlew dist
命令

我在桌面模块的build文件夹中找到了我的
desktop-1.0.jar
。复制并保存在其他文件夹中,双击运行,工作正常


这两个资源(
badlogic.jpg
&
brick.png
)都在我的
.jar
上根目录中。

多亏了@Abhishek Aryan,我发现了这个问题! 我的资产描述符是静态的,当与资产经理打交道时,似乎没有什么是静态的。我改变我的资产类别如下:

public class Assets {

public AssetManager manager;

public AssetDescriptor<Texture> logo;

public AssetDescriptor<Texture> tableBack;
public AssetDescriptor<Texture> frontBackground;

public AssetDescriptor<Texture> backBackground;

public AssetDescriptor<TextureAtlas> uiAtlas;

public AssetDescriptor<Skin> uiSkin;

public AssetDescriptor<TextureAtlas> gameAtlas;

public AssetDescriptor<TextureAtlas> flamesAnimation;

public AssetDescriptor<TextureAtlas> hitAnimation;

public AssetDescriptor<TextureAtlas> shipExplosionAnimation;

public AssetDescriptor<TextureAtlas> dustAnimation;

public Assets(){
    manager = new AssetManager();
    logo = new AssetDescriptor<Texture>("logo.png", Texture.class);
    tableBack = new AssetDescriptor<Texture>("back.png", Texture.class);
    frontBackground = new AssetDescriptor<Texture>("frontBackground.png", Texture.class);
    backBackground = new AssetDescriptor<Texture>("backBackground.png", Texture.class);
    uiAtlas = new AssetDescriptor<TextureAtlas>("UI/uiskin.atlas", TextureAtlas.class);
    uiSkin = new AssetDescriptor<Skin>("UI/uiskin.json", Skin.class,
            new SkinLoader.SkinParameter("UI/uiskin.atlas"));
    gameAtlas = new AssetDescriptor<TextureAtlas>("Atlas/gameImages.atlas", TextureAtlas.class);
    flamesAnimation = new AssetDescriptor<TextureAtlas>("Atlas/flamesAnimation.atlas", TextureAtlas.class);
    hitAnimation = new AssetDescriptor<TextureAtlas>("Atlas/hitAnimation.atlas", TextureAtlas.class);
    shipExplosionAnimation = new AssetDescriptor<TextureAtlas>("Atlas/shipExplosionAnimation.atlas", TextureAtlas.class);
    dustAnimation = new AssetDescriptor<TextureAtlas>("Atlas/dustAnimation.atlas", TextureAtlas.class);
}

public void load()
{
    manager.load(logo);
    manager.load(tableBack);
    manager.load(frontBackground);
    manager.load(backBackground);
    manager.load(uiAtlas);
    manager.load(uiSkin);
    manager.load(gameAtlas);
    manager.load(flamesAnimation);
    manager.load(hitAnimation);
    manager.load(shipExplosionAnimation);
    manager.load(dustAnimation);
}

public void dispose() {
    manager.dispose();
}
}
公共类资产{
公共资产管理人;
公共资产描述者标识;
公共资产描述表;
公共资产描述人背景;
公共资产描述人后台;
公共资产描述人uiAtlas;
公共资产描述人尤斯金;
公共资产描述者游戏地图册;
公共资产描述人Flamesanaimation;
公共资产描述人;
公共资产描述人shipExplosionAnimation;
公共资产描述人;
公共资产(){
manager=新的AssetManager();
logo=新资产描述器(“logo.png”,Texture.class);
tableBack=新的AssetDescriptor(“back.png”,Texture.class);
frontBackground=newassetdescriptor(“frontBackground.png”,Texture.class);
backBackground=newAssetDescriptor(“backbackbackground.png”,Texture.class);
uiAtlas=新资产描述人(“UI/uiskin.atlas”,TextureAtlas.class);
uiSkin=newAssetDescriptor(“UI/uiSkin.json”,Skin.class,
新的SkinLoader.SkinParameter(“UI/uiskin.atlas”);
gameAtlas=新资产描述器(“Atlas/gameImages.Atlas”,TextureAtlas.class);
flamesAnimation=新资产描述(“Atlas/flamesAnimation.Atlas”,TextureAtlas.class);
hitAnimation=new AssetDescriptor(“Atlas/hitAnimation.Atlas”,TextureAtlas.class);
shipExplosionAnimation=新资产描述符(“Atlas/shipExplosionAnimation.Atlas”,TextureAtlas.class);
dustAnimation=新资产描述符(“Atlas/dustAnimation.Atlas”,TextureAtlas.class);
}
公共空荷载()
{
经理。加载(徽标);
manager.load(tableBack);
manager.load(前台后台);
manager.load(后台);
加载管理器(uiAtlas);
加载管理器(uiSkin);
加载管理器(gameAtlas);
经理负荷(火焰净化);
加载(动画);
manager.load(shipExplosionAnimation);
加载(动画);
}
公共空间处置(){
manager.dispose();
}
}

现在一切正常:)

多亏了@Abhishek Aryan,我发现了问题所在! 我的资产描述符是静态的,当我
public class Assets {

public AssetManager manager;

public AssetDescriptor<Texture> logo;

public AssetDescriptor<Texture> tableBack;
public AssetDescriptor<Texture> frontBackground;

public AssetDescriptor<Texture> backBackground;

public AssetDescriptor<TextureAtlas> uiAtlas;

public AssetDescriptor<Skin> uiSkin;

public AssetDescriptor<TextureAtlas> gameAtlas;

public AssetDescriptor<TextureAtlas> flamesAnimation;

public AssetDescriptor<TextureAtlas> hitAnimation;

public AssetDescriptor<TextureAtlas> shipExplosionAnimation;

public AssetDescriptor<TextureAtlas> dustAnimation;

public Assets(){
    manager = new AssetManager();
    logo = new AssetDescriptor<Texture>("logo.png", Texture.class);
    tableBack = new AssetDescriptor<Texture>("back.png", Texture.class);
    frontBackground = new AssetDescriptor<Texture>("frontBackground.png", Texture.class);
    backBackground = new AssetDescriptor<Texture>("backBackground.png", Texture.class);
    uiAtlas = new AssetDescriptor<TextureAtlas>("UI/uiskin.atlas", TextureAtlas.class);
    uiSkin = new AssetDescriptor<Skin>("UI/uiskin.json", Skin.class,
            new SkinLoader.SkinParameter("UI/uiskin.atlas"));
    gameAtlas = new AssetDescriptor<TextureAtlas>("Atlas/gameImages.atlas", TextureAtlas.class);
    flamesAnimation = new AssetDescriptor<TextureAtlas>("Atlas/flamesAnimation.atlas", TextureAtlas.class);
    hitAnimation = new AssetDescriptor<TextureAtlas>("Atlas/hitAnimation.atlas", TextureAtlas.class);
    shipExplosionAnimation = new AssetDescriptor<TextureAtlas>("Atlas/shipExplosionAnimation.atlas", TextureAtlas.class);
    dustAnimation = new AssetDescriptor<TextureAtlas>("Atlas/dustAnimation.atlas", TextureAtlas.class);
}

public void load()
{
    manager.load(logo);
    manager.load(tableBack);
    manager.load(frontBackground);
    manager.load(backBackground);
    manager.load(uiAtlas);
    manager.load(uiSkin);
    manager.load(gameAtlas);
    manager.load(flamesAnimation);
    manager.load(hitAnimation);
    manager.load(shipExplosionAnimation);
    manager.load(dustAnimation);
}

public void dispose() {
    manager.dispose();
}
}