访问JAR文件中的资源 [英] Accessing resources within a JAR file
问题描述
我正在尝试访问嵌入在JAR文件中的资源.需要访问文件的类是:
I'm trying to access resources that I have embedded in a JAR file. The class that needs to access a file is:
/worldEntities/factories/RoomFactory.class
并尝试访问:
/map-data/roomDescriptions.xml
为此,我将以下代码添加到RoomFactory.class:
To do this, I have added the following code to RoomFactory.class:
Document doc = docBuilder.parse(this.getClass().getResourceAsStream("/map-data" + File.separator + "roomDescriptions.xml"));
当我在NetBeans中执行项目时,它运行正常,没有错误.但是,当我尝试从命令行运行代码时,请输入
When I execute the project in NetBeans, it works fine with no errors. However, when I attempt to run the code from the command line, by entering
java -jar Program.jar
我得到一个java.lang.IllegalArgumentException:InputStream不能为null,它指向我上面描述的代码.
I get a java.lang.IllegalArgumentException: InputStream cannot be null, and it points the code that I describe above.
在此先感谢您的帮助,
托马斯
推荐答案
-
具有
"-"
char的名称不是有效的Java软件包名称.因此"map-data"
不是有效的程序包名称.
Name with
"-"
char are not valid Java package name. So"map-data"
is not a valid package name.
然后,File.separator
在不同平台上可能会有所不同.在代码中使用"/"
,以确保使用有效的软件包分隔符.
Then File.separator
could be different on different platforms. Use "/"
in your code to be sure to use the valid package separator.
这篇关于访问JAR文件中的资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!