Java 运行新的 LibGDX 项目时“找不到文件”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22822767/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 18:07:59  来源:igfitidea点击:

"File not found" when running new LibGDX project

javalibgdx

提问by lebill

I trying to learn LibGDX, I install all the software listed herewith a new Eclipse 4.3 on a fresh formatted mac OS X Maverick.

我试图学习 LibGDX,我在新格式化的 mac OS X Maverick 上安装了这里列出的所有软件和新的 Eclipse 4.3。

Everything goes smooth, after a reboot, I download, and execute the gdx-setup.jar, fill the form, and import into Eclipse.

一切顺利,重启后,我下载并执行gdx-setup.jar,填写表格,然后导入Eclipse。

No error, no warning, when I try to run the desktop. (Right click the desktop project, Run As -> Java Application).

当我尝试运行桌面时,没有错误,没有警告。(右键单击桌面项目,运行方式 -> Java 应用程序)。

I got this error

我收到这个错误

Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: Couldn't load file: badlogic.jpg
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:130)
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:121)
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:100)
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:92)
at com.diesel.bugs.DieselBugs.create(DieselBugs.java:21)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:136)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.run(LwjglApplication.java:114)


Caused by: com.badlogic.gdx.utils.GdxRuntimeException: File not found: badlogic.jpg (Local)
at com.badlogic.gdx.files.FileHandle.read(FileHandle.java:134)
at com.badlogic.gdx.files.FileHandle.readBytes(FileHandle.java:218)
at com.badlogic.gdx.graphics.Pixmap.<init>(Pixmap.java:137)

I found a lot of similar issue here, I try them all without any good result... Last night I found this, very cool I have the latest Java 1.8, a mac, and Eclipse fit perfectly...

我在这里发现了很多类似的问题,我都尝试了,但没有任何好的结果......昨晚我发现了这个,非常酷,我有最新的 Java 1.8、Mac 和 Eclipse 非常适合......

But no success, I try with Java 1.6 and 1.7, Always the same error (No file found, I kept Java 1.7)

但是没有成功,我尝试使用 Java 1.6 和 1.7,总是同样的错误(找不到文件,我保留了 Java 1.7)

I begin to do some debug, here my only modification of the original code generated by the importation.

我开始做一些调试,这里我只修改了输入生成的原始代码。

package com.diesel.bugs;

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 DieselBugs extends ApplicationAdapter {
    SpriteBatch batch;
    Texture imgExternal,imgLocal;

    @Override
    public void create () {
        batch = new SpriteBatch();
        String pathLocal = Gdx.files.getLocalStoragePath();
        String pathExternal = Gdx.files.getExternalStoragePath();
        Boolean isExternal = Gdx.files.isExternalStorageAvailable();
        Boolean isLocal = Gdx.files.isLocalStorageAvailable();
        imgExternal = new Texture(Gdx.files.external("/Desktop/badlogic.jpg"));
        imgLocal = new Texture(Gdx.files.local("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(imgExternal, 0, 0);
        batch.end();
    }
}

The weird thing is pathLocal is equal to "". Is it normal for Gdx.files.getLocalStoragePath() to return nothing (empty string)?

奇怪的是 pathLocal 等于“”。Gdx.files.getLocalStoragePath() 不返回任何内容(空字符串)是否正常?

Also the

还有

imgExternal = new Texture(Gdx.files.external("/Desktop/badlogic.jpg"));

Works great. only the local one gives the error, also isLocal, and isExternal return true.

效果很好。只有本地一个给出错误,isLocal 和 isExternal 返回 true。

And I try a lots of combinations, like /assets/data/badlogic.jpg, /assets/badlogic.jpg, /data/badlogic.jpg, data/badlogic.jpg, and badlogic.jpg.

我尝试了很多组合,像/assets/data/badlogic.jpg/assets/badlogic.jpg/data/badlogic.jpgdata/badlogic.jpg,和badlogic.jpg

The image badlogic.jpg is there and I put it in multiple places to be sure.

图像 badlogic.jpg 在那里,我把它放在多个地方以确保。

And now the reason why I'm here for help is I just try all the same step on a PC and everything works great.

现在我来这里寻求帮助的原因是我只是在 PC 上尝试所有相同的步骤,并且一切正常。

What is wrong with my new mac and it's setting?

我的新 Mac 及其设置有什么问题?

采纳答案by Phonbopit

From libgdx wiki

来自 libgdx 维基

When you run Desktop Project

运行桌面项目时

The application will fail the first time. Open the Run Configuration you just created and set the working directory to the android/assets/ directory!

该应用程序将在第一次失败。打开刚刚创建的运行配置,将工作目录设置为 android/assets/ 目录!

link your desktop project to android assets folder?

将您的桌面项目链接到 android 资产文件夹?

Go to Run=> Run Configurations..=> choose DesktopLauncher, Arguments Tab => Working Directory => Others then browse to yourproject-android/assets/ and click Apply=> Run

转到Run=> Run Configurations..=> 选择DesktopLauncher,Arguments Tab => Working Directory => Others 然后浏览到 yourproject-android/assets/ 并单击Apply=> Run

enter image description here

在此处输入图片说明

回答by Code-Apprentice

For those of us using Android Studio or IntelliJ IDEA, you need to follow these steps:

对于我们这些使用 Android Studio 或 IntelliJ IDEA 的人,您需要按照以下步骤操作:

  1. Select Run -> Edit Configurations from the menu

  2. In the "Working Directory:" text box, append "/android/assets" to the path.

  1. 从菜单中选择运行 -> 编辑配置

  2. 在“工作目录:”文本框中,将“/android/assets”附加到路径。

Note that if you execute tasks with gradle, this is not an issue. The gradle.build files are configured to use the assets folder from the android module.

请注意,如果您使用 gradle 执行任务,这不是问题。gradle.build 文件被配置为使用来自 android 模块的 assets 文件夹。

回答by EntangledLoops

Note that the previous answers will not work for users who unchecked the "Android" box on setup. There should really be a default assets folder installed for those who don't care about deploying for Android.

请注意,之前的答案不适用于在设置时取消选中“Android”框的用户。应该为那些不关心为 Android 部署的人安装一个默认的资产文件夹。

回答by Sku Sku

For those using Intellij IDEA:

对于使用 Intellij IDEA 的用户:

  1. Run -> Edit Configurations
  2. On the left hand side choose: Application -> DesktopLauncher
  3. In the "Configuration"-Tab, undert "Working directory" choose your android/assets path
  1. 运行 -> 编辑配置
  2. 在左侧选择:Application -> DesktopLauncher
  3. 在“配置”-选项卡中,在“工作目录”下选择您的 android/assets 路径

回答by Juanjo

I haven't Android project. My solution is to add the asset folder to the Java Build Path->Libraries as a Class Folder. Additionally check the asset folder in Java Build Path->Order and Export to include this in other projects.

我没有Android项目。我的解决方案是将资产文件夹作为类文件夹添加到 Java Build Path->Libraries。另外检查 Java Build Path->Order and Export 中的资产文件夹以将其包含在其他项目中。

回答by Amit Kumar Shrivastava

You can check the path of Assets in working Directory or linked resources .After done check for the file which you are loading .At last clean and refresh the project and enjoy.

您可以在工作目录或链接资源中检查资产的路径。完成后检查您正在加载的文件。最后清理并刷新项目并享受。

回答by Zoe

I use a different solution.

我使用不同的解决方案。

Instead of creating an application run configuration, create a Gradle build configuration. The task is desktop:run. Once it is executed, the game (should) launch and stay alive without crashing, and the resources should be found.

不是创建应用程序运行配置,而是创建 Gradle 构建配置。任务是desktop:run。执行后,游戏(应该)启动并保持活动状态而不会崩溃,并且应该找到资源。

I am not exactly sure why, but when a Gradle task is run like this, the resources are found. When an application run configuration is used (without modifications like the currently accepted answer) it crashes because it can't find the resources.

我不确定为什么,但是当 Gradle 任务像这样运行时,会找到资源。当使用应用程序运行配置(没有像当前接受的答案那样进行修改)时,它会崩溃,因为它找不到资源。

回答by Unchained

This is how you fix it properly without playing around with the working directory:

这是您如何正确修复它而不使用工作目录:

The problem is that the asset folder is not correctly marked as resourcesdirectory for the gradle build. To fix this you have to add the following line in the ./core/build.gradlebuild file:

问题是资产文件夹没有正确标记为resourcesgradle 构建的目录。要解决此问题,您必须在./core/build.gradle构建文件中添加以下行:

sourceSets.main.resources.srcDirs = [ "assets/" ]

My file after a clean setup with the recent libGDX version looks like this:

使用最新的 libGDX 版本进行干净设置后,我的文件如下所示:

apply plugin: "java"

sourceCompatibility = 1.6
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'

sourceSets.main.java.srcDirs = [ "src/" ]
sourceSets.main.resources.srcDirs = [ "assets/" ]

eclipse.project {
    name = appName + "-core"
}

回答by Salek

I had the same problem in IntelliJ, and I had not generated the libgdx project for Android (Android option was not marked as checked) and I was yet getting the same error. Instead of linking the working directory to "/android/assets/", linked it to "/core/assets" and it worked fine for me.

我在 IntelliJ 中遇到了同样的问题,我还没有为 Android 生成 libgdx 项目(Android 选项没有标记为选中),我还遇到了同样的错误。不是将工作目录链接到“/android/assets/”,而是将其链接到“ /core/assets”,它对我来说很好用。