[英]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 运行它时,这非常有效:
现在我想为我的项目创建一个.exe。 我几乎是用 Jpackage 做到的。 该应用程序正在加载资产文件夹中的.png,但不是我的 map.txt。
我不确定图像的链接是否有效,但基本上 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()
作为替代检查,您可以硬编码文件的绝对路径并查看会发生什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.