Java getClass()。getResource位于workin目录前

Java getClass()。getResource位于workin目录前,java,getresource,Java,Getresource,我正在开发一个应用程序,需要从jar文件加载一个模式文件(xsd)作为资源, 但是getClass()的文件路径。getResource(“..”的前缀是工作目录 代码如下: //ClassLoader classLoader = getClass().getClassLoader(); URL xsdUrl = getClass().getResource( "/schema_" + XSD_VERSION.replace(".", "_") + ".xsd" ); xsd

我正在开发一个应用程序,需要从jar文件加载一个模式文件(xsd)作为资源, 但是getClass()的文件路径。getResource(“..”的前缀是工作目录

代码如下:

//ClassLoader classLoader = getClass().getClassLoader();
URL xsdUrl = getClass().getResource(
         "/schema_" + XSD_VERSION.replace(".", "_") + ".xsd"
);
xsdUrl应该是: 文件:\D:\NetBeansProjects\Java\reports\u cli\target\reports\u cli-1.0-Beta-jar-with-dependencies.jar\schema_1_1.xsd

但它正在回归: C:\Users\joao\文件:\D:\NetBeansProjects\Java\reports\u cli\target\reports\u cli-1.0-Beta-jar-with-dependencies.jar\schema_1_1.xsd

文件路径的前缀是C:\Users\joao\

这是一个项目:

这是文件:

所有方法代码:

public Unmarshaller unmarshallInstance() throws SAXException, JAXBException {
        LOG.trace(() -> "Create unmarshallInstance");
        LOG.trace(() -> "Read xsd file");

        //ClassLoader classLoader = getClass().getClassLoader();
        URL xsdUrl = getClass().getResource(
                "/schema_" + XSD_VERSION.replace(".", "_") + ".xsd"
        );

        LOG.trace(() -> "Creating JAXBContext.newInstance ");
        JAXBContext jaxbContext = JAXBContext.newInstance(Rreport.class);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

        if (xsdUrl == null) {
            LOG.warn(() -> "Schema not seted please put the schema file ('schema_"
                    + XSD_VERSION.replace(".", "_")
                    + ".xsd' in the same folder as your java file");
        } else {
            File xsd = new File(xsdUrl.getFile());
            if (xsd.isFile() && xsd.canRead()) {
                LOG.debug(() -> "Seting xsd shema file '" + xsd.getAbsolutePath() + "'");
                SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
                Schema schema = sf.newSchema(xsd);
                unmarshaller.setSchema(schema);
                LOG.trace(() -> "Schema seted");
            } else {
                LOG.debug(() -> "XSD shema path file '"
                        + xsd.getAbsolutePath()
                        + "' is not a file or is not readable");
            }
        }

        return unmarshaller;
    }

问题不是
Class.getResource()
。问题是您的代码将jar文件中的资源视为文件系统中的文件

不是


不能使用文件IO读取jar文件中的资源,也不需要这样做。呼叫它需要一个URL,这就是
Class.getResource()
返回的内容。

您犯的错误是试图将其转换为文件句柄:

File xsd = new File(xsdUrl.getFile());
getClass().getResource()返回一个不总是可转换为文件URL的URL。在本例中,它是指向jar的一部分的URL

始终有效的方法是将资源作为inputstream加载

InputStream xsdStream = xsdUrl.openStream();
StreamSource xsdSource = new StreamSource(xsdStream);
Schema schema = sf.newSchema(xsdSource);
或者使用Class.getResourceAsStream()方法跳过URL步骤:

InputStream xsdStream = getClass().getResourceAsStream("/schema_" + ...)

您好@Joao Rebelo,您可以检查堆栈溢出问题:,答案是:您不需要所有这些。只需检查
xsdUrl
是否不为空,作为您存在的证明,然后使用。你根本不必关心资源到底在哪里,也不必试图用它制作一个
文件。此外,您对
文件应该是什么的期望是没有根据的。已解决,将URL作为参数传递给sf.newSchema。这是我第一次使用这个库,我的错误是当错误发生时,我正在搜索一个合适的构造函数来从新的模式创建模式实例,但是没有,然后我失败了。谢谢,我没有测试,但是灰色的答案也很合适。