簡體   English   中英

從可執行文件啟動應用程序時,文本文件不會打開

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

我正在使用 jdk 17 和 gradle 7.3 開發一個小型 libgdx 項目。 在這個項目中,我使用資產文件夾中的文本文件生成我的 map。 我使用 Gdx.files.internal("map.txt"); 行加載它比我使用緩沖閱讀器讀取它。 這是我的文本文件中的內容:

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 

這是我使用此文件的代碼示例。

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)));
                }
            }
        }

當我從 eclipse 的 IDE 運行它時,這非常有效:

從 ide 運行的應用程序

現在我想為我的項目創建一個.exe。 我幾乎是用 Jpackage 做到的。 該應用程序正在加載資產文件夾中的.png,但不是我的 map.txt。

從 .exe 運行的應用程序

我不確定圖像的鏈接是否有效,但基本上 map 不會出現;

這是我的桌面應用程序的 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")]
        }
    }

所以我想知道如何解決這個問題。

您正在使用打開文件

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

我假設這會訪問當前工作目錄中的文件系統。

  • 打印出file.getAbsolutePath()
  • 檢查file.exists()
  • 當您從 Eclipse 啟動時檢查您當前的工作目錄 - 可以肯定的是項目目錄也托管您的 map.txt。
  • 啟動 exe 時檢查當前工作目錄。 它是否包含 map.txt?

作為替代檢查,您可以硬編碼文件的絕對路徑並查看會發生什么。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM