【问题标题】:XML file is loaded properly in Netbeans IDE but not in JAR that contains itXML 文件在 Netbeans IDE 中正确加载,但在包含它的 JAR 中没有
【发布时间】:2013-12-29 09:47:29
【问题描述】:

我正在编写一个游戏,使用Tiled 的 TMX 格式进行 2D 瓦片映射(所说的格式是基于 XML 的)。当我在 NetBeans IDE 中构建和运行程序时,它可以完美运行。但是,JAR 文件没有,踢出以下错误消息:

[Fatal Error] :1:1: Content is not allowed in prolog.

在程序继续运行并尝试引用已卸载的地图后,自然会出现许多 NullPointerExceptions。

使用 jar xvf 已验证文件已正确构建到 JAR 中,并且没有引入额外数据 - 未归档的副本与我的 src/assets 文件夹中的逐字节相同。

相关代码如下;来自 GameScreenMain.java:

try {
    TMXMapReader mapReader = new TMXMapReader();
    map = mapReader.readMap(ClassLoader.getSystemClassLoader().getResourceAsStream("test_map.tmx"));
} catch (Exception e) {
}

来自 TMXMapReader.java:

public Map readMap(InputStream in) throws Exception {
    xmlPath = makeUrl(".");

    Map unmarshalledMap = unmarshal(in);

    //unmarshalledMap.setFilename(xmlFile)
    //
    return unmarshalledMap;
}

private Map unmarshal(InputStream in) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    Document doc;
    try {
        factory.setIgnoringComments(true);
        factory.setIgnoringElementContentWhitespace(true);
        factory.setExpandEntityReferences(false);
        DocumentBuilder builder = factory.newDocumentBuilder();
        builder.setEntityResolver(entityResolver);
        InputSource insrc = new InputSource(in);
        insrc.setSystemId(xmlPath);
        insrc.setEncoding("UTF-8");
        doc = builder.parse(insrc);
    } catch (SAXException e) {
        throw new Exception("Error while parsing map file: " +
                e.toString());
    }

    buildMap(doc);

    return map;
}

来自 test_map.tmx:

<?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" orientation="orthogonal" width="18" height="10" tilewidth="64" tileheight="64">
    <tileset firstgid="1" name="Indev" tilewidth="64" tileheight="64">
        <image source="graphics/Indev.png" trans="ff00ff" width="128" height="64"/>
        <tile id="1">
            <properties>
                <property name="collision" value="solid"/>
            </properties>
        </tile>
    </tileset>
    <layer name="Tile Layer 1" width="18" height="10">
        <data encoding="base64" compression="zlib">   eJxjYmBgYKISZiQRU8scXGZhEyfHLEYseok1k5A5uPTgM4sYc/C5DZccPj8yYVGHzRxi0wm10hw6BgB7xwES</data>
    </layer>
</map>

为什么完全相同的文件可以在 IDE 中工作,但不能在 JAR 中工作?

【问题讨论】:

  • 在构建InputStream 时,我的赌注是位置不正确,但您没有显示该代码。
  • InputStream 在第一个代码块中创建,作为调用 readMap() 的一部分。 (马虎,我知道。)
  • 哦,对不起,我的眼睛变老变灰了。假设您的构建过程保留该文件夹,为什么不使用getResourceAsStream('/assets/test_map.tmx')
  • 好的,我尝试了几种方式更改字符串。如果我在前面加上正斜杠(或使用“/test_map.tmx”),IDE 会开始抛出相同的错误,但如果我使用“assets/test_map.tmx”则不会。项目类路径包含 src/assets。我还没有自定义构建脚本。
  • 在您尝试解析之前打印出正在加载的内容,以验证您在不同情况下没有加载不同的内容。

标签: java xml netbeans jar


【解决方案1】:

如果资源位于类路径中的 assets 文件夹中,则需要将其添加到资源标识符(即文件名和路径)中,例如assets/test_map.tmx.

它可能在 IDE 中工作,因为当前工作目录与从 JAR 运行程序时不同。从 JAR 运行时,在 JAR 中不可能有工作目录,因为它只是一个文件。从 IDE 运行时,JAR 尚未构建,每个资源都作为文件存在于文件系统中,例如工作目录。 classes 文件夹是可能的。因此,当从 IDE 或 JAR 运行并依赖于特定或隐式环境(如工作目录)时,可能会有不同的行为。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-12
    • 2015-04-22
    • 2023-04-04
    • 2021-04-07
    • 2014-05-18
    • 2015-05-05
    相关资源
    最近更新 更多