简体   繁体   English

从可执行文件启动应用程序时,文本文件不会打开

[英]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.我正在使用 jdk 17 和 gradle 7.3 开发一个小型 libgdx 项目。 In this project I am generating my map using a text file located in the assets folder.在这个项目中,我使用资产文件夹中的文本文件生成我的 map。 I use the Gdx.files.internal("map.txt");我使用 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:当我从 eclipse 的 IDE 运行它时,这非常有效:

app running from ide从 ide 运行的应用程序

Now I want to create an.exe of my project.现在我想为我的项目创建一个.exe。 And I did it with Jpackage, almost.我几乎是用 Jpackage 做到的。 The app is running the.png in the assets folder are loading but not my map.txt.该应用程序正在加载资产文件夹中的.png,但不是我的 map.txt。

app running from the.exe从 .exe 运行的应用程序

I am not sure the link for the images are working but basically the map won't show up;我不确定图像的链接是否有效,但基本上 map 不会出现;

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...这是我的桌面应用程序的 gradle.build:我认为问题可能出在某个地方,但我只学习 java 和所有这些,所以我无法弄清楚......

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()打印出file.getAbsolutePath()
  • Check if file.exists()检查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.当您从 Eclipse 启动时检查您当前的工作目录 - 可以肯定的是项目目录也托管您的 map.txt。
  • Check the current working directory when launching the exe.启动 exe 时检查当前工作目录。 Does it contain map.txt?它是否包含 map.txt?

As an alternative check you could hardcode the absolute path to the file and see what happens.作为替代检查,您可以硬编码文件的绝对路径并查看会发生什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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