117

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

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

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

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$1.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...

But no success, I try with Java 1.6 and 1.7, Always the same error (No file found, I kept 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)?

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.

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

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

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.

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

Zoe
  • 27,060
  • 21
  • 118
  • 148
lebill
  • 1,293
  • 2
  • 8
  • 9
  • That's weird... did you refresh the android-project in Eclipse after adding the assets? Because that happens to me a lot when I add a new asset to the asset-folder in the android-project and then forget to refresh the android project in Eclipse, it will give me an error saying the image isn't there... – florianbaethge Apr 03 '14 at 06:07

9 Answers9

229

From libgdx wiki

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!

link your desktop project to android assets folder?

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

enter image description here

Phonbopit
  • 3,393
  • 3
  • 21
  • 29
  • also to help other with the same issue, to make the same directory work with the Android build, we need to right click on the Android / assets directory and Click on Built path and choose to Add it. – lebill Apr 03 '14 at 21:46
  • 11
    That did it for me too. What a shame that LibGDX has this issue right from the start-- it's a great system, but things like that are going to scare away devs with little patience. – Richard Aug 20 '14 at 17:57
  • Awesome! Wouldn't it make more sense for libgdx to replace the image with a text field to the sample project explaining to do this? I guess a simple google search leads everyone here anyhow but none the less, it is a weird thing to encounter – Kai Qing Jan 17 '15 at 18:19
  • Wow, This works. I saw the images for Eclipse and ignored them as I use IntelliJ... well, it works for IntelliJ as well :) – Kenny Cason Dec 23 '15 at 18:28
  • 1
    In case you don't have android/assets folder, setting the desktop/assets folder the same way worked for me. – Fredrik Andersson Feb 17 '16 at 19:23
  • @Richard it's more of a problem with IntelliJ Idea. If you work with Eclipse, you shouldn't have this problem at all. The actual problem is that LibGDX uses an Eclipse feature to keep the assets folder linked among modules, but it seems such feature doesn't exist in Idea. http://stackoverflow.com/questions/23058448/linked-files-and-folder-in-intellij – Fran Marzoa Sep 15 '16 at 13:40
  • 1
    Please, note that this is not a solution but a workaround. It will work in most circumstances, but not in all of them. For example, if you plan to generate a texture atlas from the Desktop module you'll have to recompute the paths, that by default are relative to the Desktop module. – Fran Marzoa Sep 15 '16 at 13:43
  • This is a bad solution to the problem. Please use the solution https://stackoverflow.com/a/53602731/2689500. – Friedrich Jun 10 '19 at 10:39
76

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

  1. Select Run -> Edit Configurations from the menu

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

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.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
13

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.

EntangledLoops
  • 1,951
  • 1
  • 23
  • 36
  • 5
    Correct and correct, just ran into it; setting the Desktop working directory to the assets folder in core worked for me. – naftalimich Mar 04 '16 at 06:01
  • @naftalimich Yeah, I think assets should really be in core. I understand why they did it, as the android gradle plugin wants them there and they didn't want to deal w/copying everything each build / dealing with sync issues. But this should really be addressed. – EntangledLoops Oct 06 '16 at 21:27
12

For those using 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
Sku Sku
  • 191
  • 1
  • 5
  • 3
    You might mention: 1) This requires you to first create a desktop configuration. 2) This assumes you selected android when generating your original project. 3) This answer was already provided by Code-Apprentice above as instructions for Android Studio, which is built on the IntelliJ IDE. – EntangledLoops Mar 04 '16 at 14:13
6

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 resources directory for the gradle build. To fix this you have to add the following line in the ./core/build.gradle build file:

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

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

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"
}
Unchained
  • 677
  • 7
  • 8
1

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.

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.

Zoe
  • 27,060
  • 21
  • 118
  • 148
1

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.

Salek
  • 449
  • 1
  • 10
  • 19
0

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.

Juanjo
  • 11
  • 1
0

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.