Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java getClassLoader().getResource(“Configuration.xml”).getPath()在jar中不起作用_Java_Xml_Maven_Intellij Idea_Maven Resources Plugin - Fatal编程技术网

Java getClassLoader().getResource(“Configuration.xml”).getPath()在jar中不起作用

Java getClassLoader().getResource(“Configuration.xml”).getPath()在jar中不起作用,java,xml,maven,intellij-idea,maven-resources-plugin,Java,Xml,Maven,Intellij Idea,Maven Resources Plugin,我创建了一个基于maven的java项目,其中包含resources目录(src/java/resources)和一些xml文件,maven将它们复制到target/classes/resources中(target/classes是我的类路径) 要了解xml文件的内容,我使用: new FileInputStream(Main.class.getClassLoader().getResource("Configuration.xml").getPath()); Main.class.getC

我创建了一个基于maven的java项目,其中包含resources目录(src/java/resources)和一些xml文件,maven将它们复制到target/classes/resources中(target/classes是我的类路径)

要了解xml文件的内容,我使用:

new FileInputStream(Main.class.getClassLoader().getResource("Configuration.xml").getPath());
Main.class.getClassLoader().getResource(“Configuration.xml”).getPath()为我提供了xml的完整路径:“c:\project…\Configuration.xml”

这在intellij上运行良好

但当我将项目编译并打包成jar文件时,我得到:

Exception in thread "main" java.io.FileNotFoundException: 
file:\C:\project\target\project-12.50.14-SNAPSHOT-jar-with-dependencies
.jar!\Configuration.xml 

(The filename, directory name, or volume label syntax is incorrect)
这是因为路径:\C:\project\target\project-12.50.14-SNAPSHOT-jar-with-dependencies jar先生\Configuration.xml不能是FileInputStream()中的参数


我无法将getResource()替换为getResourceAsStream()

我需要一种方法来调整我的jar和资源,使其像在intellij上一样工作。 我还在运行时更改xml资源文件的内容,这是将它们保存在jar中的另一个问题

有谁能给我提供一个解决方案,让我不必更改代码,而是更改资源目录结构,或更改类路径,或其他可以让代码正常工作的方法?
谢谢。

更新:更改为使用
文件
,而不是
路径

这是因为
getResource
返回一个
URL
,并且并非所有的URL都是磁盘上的文件,即可以转换为
路径。如前所述,“如果[路径部分]不存在,则返回[…]一个空字符串”

如果它绝对必须是
文件
文件输入流
,因为无法更改的其他代码需要它,请将内容复制到临时文件:


如果资源是一个文件,上面的代码可以优化为不复制,但由于这仅适用于开发模式,而生产模式始终是一个Jar,并且始终需要副本,因此在开发模式中使用copy有助于测试代码。

“我无法将getResource()替换为getResourceAsStream()”-为什么不?从根本上说,您不能使用
FileInputStream
获取嵌入jar文件中的文件。您应该适当地重构代码,以便可以使用
getResourceAsStream
。为什么需要使用FileInputStream?我无法替换它,因为这不是我的代码。我只能消耗这个类,不能改变它。此外,我还在运行时更改connection.xml,因此我希望该文件位于jar之外(如intellij“target/class/connection.xml”上的目录)啊,我明白了——这是一个关于构建/打包的问题,而不是关于编程的问题。Intellij似乎将包含Configuration.xml的目录放在类路径中,但当您将项目打包到jar中时,只有jar在类路径中。您需要提取Configuration.xml,并确保它的目录位于jar的前面运行时类路径。
File file = File.createTempFile("Configuration", ".xml");
try {
    try {InputStream in = Main.class.getClassLoader().getResourceAsStream("Configuration.xml")) {
        Files.copy(in, file.toPath()); // requires Java 7
    }
    // use file here, or:
    try {FileInputStream in = new FileInputStream(file)) {
        // use file stream here
    }
} finally {
    file.delete();
}