简体   繁体   中英

Text files won't open when launching app from an executable

I am developping a small libgdx project using jdk 17 and gradle 7.3. In this project I am generating my map using a text file located in the assets folder. I use the Gdx.files.internal("map.txt"); line to load it than i use a buffered reader to read it. Here is what is in my text file:

g w g g g g g g g g
g w w w w g g c g c 
g g g g w g g g g g
g g g g w g g g g g 
g g g g w w g g g u
g g g g g w g g g g 
g g g g g w g c g g
g u u g g w g g g g 
g g w g g w w g g g
g g g g g g w g g g 

Here is the sample of my code where I am using this file.

file = Gdx.files.internal("map.txt");
        
        BufferedReader bufferedReader = new BufferedReader(new FileReader(file.path()));
        String s = "";
        int count = 0;
        while((s = bufferedReader.readLine()) != null) {
            System.out.println(s);
            map[count] = s.split(" ");
            count++;
        }
        bufferedReader.close();
        
        
        for(int row = 9; row >= 0; row--) {
            for(int col = 9; col >= 0; col--) {
                
                float x = (row - col) * 64 / 2F; 
                float y = (col + row) * 32 / 2F;
                
                if(map[row][col].equals("g")) {
                    base.add(new Tile(grass, new Vector2(row, col), new Vector2(x, y)));
                } else if(map[row][col].equals("w")) {
                    base.add(new Tile(water, new Vector2(row, col), new Vector2(x, y)));
                } else if(map[row][col].equals("c")){
                    base.add(new Tile(charcoal, new Vector2(row, col), new Vector2(x, y)));
                } else if(map[row][col].equals("u")) {
                    base.add(new Tile(uranium, new Vector2(row, col), new Vector2(x, y)));
                }
            }
        }

This works perfectly when I am running it from my IDE which is eclipse:

app running from ide

Now I want to create an.exe of my project. And I did it with Jpackage, almost. The app is running the.png in the assets folder are loading but not my map.txt.

app running from the.exe

I am not sure the link for the images are working but basically the map won't show up;

Here is my gradle.build for my desktop application: I think the issue might be somewhere in there but I only learning java and all this so I can't figure it out...

plugins { id 'org.beryx.runtime' version '1.8.4' }
apply plugin: "java"

sourceCompatibility = 1.7

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


mainClassName = "com.simpower.desktop.DesktopLauncher"
def osName = System.getProperty('os.name').toLowerCase(Locale.ROOT)
project.ext.assetsDir = new File("../core/assets")


task runGame(dependsOn: classes, type: JavaExec) {
    main = project.mainClassName
    classpath = sourceSets.main.runtimeClasspath
    standardInput = System.in
    workingDir = project.assetsDir
    ignoreExitValue = true
}

task debug(dependsOn: classes, type: JavaExec) {
    main = project.mainClassName
    classpath = sourceSets.main.runtimeClasspath
    standardInput = System.in
    workingDir = project.assetsDir
    ignoreExitValue = true
    debug = true
}

task dist(type: Jar) {
    manifest {
        attributes 'Main-Class': project.mainClassName
    }
    dependsOn configurations.runtimeClasspath
    from {
        configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
    }
    with jar
    destinationDirectory = file("$buildDir/lib")
}
jpackageImage.dependsOn dist

dist.dependsOn classes

eclipse.project.name = appName + "-desktop"

runtime {
    options = ['--strip-debug',
               '--compress', '2',
               '--no-header-files',
               '--no-man-pages',
               '--strip-native-commands',
               '--vm', 'server']
    modules = ['java.base' ,
               'java.desktop',
               'jdk.unsupported']
    distDir = file(buildDir)

    jpackage {
        //jpackageHome = '/usr/lib/jvm/open-jdk'
        mainJar = dist.archiveFileName.get()
        if (osName.contains('windows')) {
            imageOptions = ["--icon", file("../icons/icon.ico")]
        } else if (osName.contains('linux')) {
            imageOptions = ["--icon", file("../icons/icon.png")]
        } else if (osName.contains('mac')) {
            imageOptions = ["--icon", file("../icons/icon.icns")]
        }
    }

So I was wondering how I could fix this.

You are opening the file using

file = Gdx.files.internal("map.txt");
... new FileReader(file.path()) ... ;

I assume this accesses the filesystem in the current working directory.

  • Print out file.getAbsolutePath()
  • Check if file.exists()
  • Check your current working directory when you launch from Eclipse - it is for sure the project directory that also hosts your map.txt.
  • Check the current working directory when launching the exe. Does it contain map.txt?

As an alternative check you could hardcode the absolute path to the file and see what happens.

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