从同一jar中存在的java代码中读取jar中的文件 [英] Read a file within a jar from java code present in same jar

查看:73
本文介绍了从同一jar中存在的java代码中读取jar中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用保存在同一个jar中的java代码来读取保存在jar中的xsd文件。我使用以下代码。

I am trying to read a xsd file kept in a jar using java code kept inside the same jar. I am using the following code.

URL tmpurl = ClassLoader.getSystemResource("com/abc/filename.xsd");

Schema s = schemaFactory.newSchema(tmpurl);
jaxbUnMarshaller.setSchema(s);

当我将它作为一个单独的项目运行时工作正常但是当我制作一个jar时,tmpurl是null,因此setSchema给出一个空指针异常。

It is working fine when I run it as a separate project but when I make a jar, tmpurl is null, hence the setSchema gives a null pointer exception.

你能否解决一个可以让它在jar文件中运行的解决方法。

Can you please a workaround that can make it run even inside a jar file.

推荐答案

你试过Hava吗?

getClass().getClassLoader().getResource()

此外你的类路径在jar中的清单文件中确实很重要。

Also your classpath in manifest file in jar does matter.

请在这里查看接受的答案:

Please, take a look at accepted answers in here:

Class.getResource和ClassLoader.getSystemResource:有吗选择彼此的原因是什么?

使用ClassLoader加载文件

这篇关于从同一jar中存在的java代码中读取jar中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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