导出到.jar后,Java游戏eclipse无法找到.txt [英] Java game eclipse can't locate .txt after export to .jar

查看:190
本文介绍了导出到.jar后,Java游戏eclipse无法找到.txt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚在日食中建造了一款游戏。我想像往常一样将它导出到.jar。我之前已经完成了这项工作并与其他项目一起工作。这个不起作用,因为它给出了一个空白的白色屏幕。我在不同的位置放入System.exit(1)以查看程序停止工作的位置。最终我找到了这个地方。它似乎无法找到我的.txt文件,但在eclipse中是否有效。我用7zip(比如winzip)检查了.jar,然后是.txt文件。我还使用命令行打开它,它给出了无法找到.txt文件的相同错误。对于那些对相关问题有相同问题的人,我已经在互联网和堆栈流程上看了很多,但不幸的是他们没有提供修复。



制作一个代码的代码新世界就是这样。

  world = new World(handler,res / worlds / world1.txt); 

世界上它称之为功能:

  loadWorld(path); 

这个功能应该返回一个字符串。首先,它调用另一个函数来获取该字符串:

  String file = Utils.loadFileAsString(path); 

最后,这是无效的代码,因为无法找到该文件。

  public static String loadFileAsString(String path){
StringBuilder builder = new StringBuilder();

try {

BufferedReader br = new BufferedReader(new FileReader(path));
字符串行;
while((line = br.readLine())!= null)
builder.append(line +\ n);
br.close();
} catch(IOException e){
e.printStackTrace();
}
返回builder.toString();
}

我错过了什么? Eclipse运行得很好但是在导出之后就像.txt一样没有导出但它仍然存在。
提前致谢。

解决方案

如果您有一个存档阅读器(例如7zip),请打开.jar和看看你的.txt文件是否在.jar中。如果没有,只需将.txt文件复制到.jar文件中即可。在尝试将.png用于Java应用程序时,这种情况发生在我身上。希望它成功!


I built a game a while ago in eclipse. I wanted to export it like usual to a .jar. I've done this before and with my other projects it works. This one doesn't work because it gives a blank white screen. I put in a System.exit(1) on a different spots to see where the program stops working. Eventually I found the spot. It can't seem to locate my .txt file but in eclipse if does work. I've checked the .jar with 7zip(like winzip) and the .txt file is in there. I also used the command line to open it and it gives the same error of not being able to locate the .txt file. I've looked a lot on the internet and stackflow for people that have same questions of related problems but they didn't offer a fix unfortunately.

The code that make a new world is this.

 world = new World(handler,"res/worlds/world1.txt");

In the world it calls this funtion:

 loadWorld(path);

This funtion should return back a string. First it calls another function to get that string:

 String file = Utils.loadFileAsString(path);

Finally this is the code that doesn't work because the file can't be located.

    public static String loadFileAsString(String path){
    StringBuilder builder = new StringBuilder();

    try{

        BufferedReader br = new BufferedReader(new FileReader(path));
        String line;
        while((line = br.readLine()) != null)
            builder.append(line + "\n");
        br.close();
    }catch(IOException e){
        e.printStackTrace();
    }
    return builder.toString();
}

Is there something that I missed? Eclipse runs it fine but after exporting its like the .txt isn't exported but it is still there. Thanks in advance.

解决方案

If you have an archive reader (such as 7zip) open up the .jar and see if your .txt file is inside the .jar. If not, simply copy the .txt file into the .jar file. This happened to me when trying to use a .png for a Java application. Hope it works out!

这篇关于导出到.jar后,Java游戏eclipse无法找到.txt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆