[英]After exporting it with Eclipse, my runnable JAR doesn't open, probably because of resources
I've searched through dozens of threads all over the Internet (including stackoverflow) and I haven't found anything that helped me, maybe because I'm too dumb to understand it. 我在Internet上搜索了数十个线程(包括stackoverflow),但找不到任何有用的信息,可能是因为我太笨了,无法理解它。 I'm kind of new to Java so that might be it.
我是Java的新手,所以就可以了。
Anyway, the program runs perfectly inside Eclipse, it gets all the needed images, files, etc. Once I export it as a runnable JAR, I can't open it, not even with the command prompt (says "Unable to access). 无论如何,该程序可以在Eclipse内完美运行,并且可以获取所有需要的图像,文件等。一旦将其导出为可运行的JAR,就无法打开它,即使使用命令提示符也是如此(例如“无法访问”)。
This is what I tried, importing the resources: 这是我尝试的导入资源:
package main;
import java.io.File;
public class Loader {
public static String path = "/Z:/Eclipse/workspace/FirstGame/src/";
static File smoothFont;
static File roughFont;
static File alahuSong;
static File deathAlahu;
static File hitSoundAlahu;
static File hitSoundBit;
static File deathBit;
static File hpBit;
static File selectbit;
static File songBit;
static File colorFile;
static File easyScoreFile;
static File normalScoreFile;
static File hardScoreFile;
static File insaneScoreFile;
static File meSteam;
static File manelFaxe;
static File mePassado;
static File manelSteam;
public static void load(Game game) {
game.getClass().getResource(path + "res/summerStormSmooth.ttf");
game.getClass().getResource(path + "res/summerStormRough.ttf");
game.getClass().getResource(path + "res/alahuSong.ogg");
game.getClass().getResource(path + "res/deathAlahu.ogg");
game.getClass().getResource(path + "res/hitSoundAlahu.ogg");
game.getClass().getResource(path + "res/hitSoundBit.ogg");
game.getClass().getResource(path + "res/deathBit.ogg");
game.getClass().getResource(path + "res/hpBit.ogg");
game.getClass().getResource(path + "res/selectBit.ogg");
game.getClass().getResource(path + "res/songBit.ogg");
game.getClass().getResource(path + "res/colorFile.txt");
game.getClass().getResource(path + "res/easyScoreFile.txt");
game.getClass().getResource(path + "res/normalScoreFile.txt");
game.getClass().getResource(path + "res/hardScoreFile.txt");
game.getClass().getResource(path + "res/insaneScoreFile.txt");
game.getClass().getResource(path + "res/meSteam.png");
game.getClass().getResource(path + "res/manelFaxe.png");
game.getClass().getResource(path + "res/mePassado.png");
game.getClass().getResource(path + "res/manelSteam.png");
}
}
Also, this is how my build is organized: :D 另外,这就是我的构建的组织方式::D
I can give you any more information you need, thanks for your help! 我可以为您提供更多所需信息,谢谢您的帮助!
You are passing an invalid String value. 您正在传递无效的String值。 The String argument accepted by Class.getResource is not a file name.
Class.getResource接受的String参数不是文件名。
You should be passing a path relative to the root of your .jar. 您应该传递一个相对于.jar根目录的路径。 If you're not sure exactly which files are in your .jar file, either examine the .jar file in Eclipse, or use a Command Window to view the .jar file with jar tf MyApplication.jar .
如果不确定.jar文件中到底包含哪些文件,请在Eclipse中检查.jar文件,或使用命令窗口通过jar tf MyApplication.jar查看.jar文件。
If your project is set up correctly, it probably bundles the contents of the res
directory into your final .jar file, in which case the correct way to invoke Class.getResource is: 如果您的项目设置正确,则可能会将
res
目录的内容捆绑到最终的.jar文件中,在这种情况下,调用Class.getResource的正确方法是:
static URL smoothFont;
static URL roughFont;
// ...
static URL colorFile;
// ...
static URL meSteam;
public static void load(Game game) {
smoothFont = Loader.class.getResource("/summerStormSmooth.ttf");
roughFont = Loader.class.getResource("/summerStormRough.ttf");
// ...
colorFile = Loader.class.getResource("/colorFile.txt");
// ...
meSteam = Loader.class.getResource("/meSteam.png");
// ...
}
You cannot refer to your data as File
s when they are inside a .jar file, because they're just entries in the .jar file—byte sequences representing compressed data—and not actual separate files. 当它们位于.jar文件中时, 不能将它们称为
File
,因为它们只是.jar文件中的条目(代表压缩数据的字节序列),而不是实际的单独文件。
Fortunately, loading a URL is just as easy as loading a File: 幸运的是,加载URL与加载文件一样容易:
Font loadedSmoothFont;
try (InputStream stream = smoothFont.openStream()) {
loadedSmoothFont = Font.createFont(Font.TRUETYPE_FONT, stream);
}
String colorFileContents;
try (Scanner scanner = new Scanner(colorFile.openStream())) {
colorFileContents = scanner.useDelimiter("\\z").next();
}
BufferedImage meSteamImage = ImageIO.read(meSteam);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.