2011-09-20 51 views
0

我有一个小行家应用程序,它从classpath加载XML文件,并做一些操作。它运行在Eclipse很好,但是当我运行Maven:装配,并获得与依赖条件可执行的JAR,程序执行到需要的地方获取XML文件中的点,然后它给:加载XML文件给出了错误文件找不到,从Eclipse的运行是好的

java.io.FileNotFoundException: /home/ubuntu/Documents/workspaces/workspace-sts-2.7.2/test/target/file: 
/home/ubuntu/Documents/workspaces/workspace-sts-2.7.2/test/target/test-0.0.1-SNAPSHOT-jar-with-dependencies.jar!/test.xml (No such file or directory) 

test.xml文件肯定是在jar中,就像我说的那样,它运行并从eclipse运行时发现文件很好。我相信,清单文件设置正确:

Manifest-Version: 1.0 
Archiver-Version: Plexus Archiver 
Created-By: Apache Maven 
Built-By: ubuntu 
Build-Jdk: 1.6.0_26 
Main-Class: org.test.test1.App 
Class-Path:. 

这里是加载XML文件中的代码:

​​

回答

1

尝试改变这一点:

URL classpathFileLocation = classLoader.getResource("test.xml"); 
File file = new File(classpathFileLocation.getFile()); 

这样:

InputStream is = classLoader.getResourceAsStream("test.xml"); 
Document doc = builder.parse(is); // Or look at the builder API to see what accepts InputStream 

我不知道它是否使任何d ifference,但我会为当前类使用类加载器。

+0

烨奏效,想我应该从创建文件的远离,只是使用inputstream- – chrismarx

+0

这是更普遍的这种方式 - 它会与Web应用程序,WAR和JAR文件。 – duffymo

相关问题