我用maven创建了一个Spring独立应用程序。因此,还添加了maven依赖项。
如果我从命令行运行jar,我会收到以下错误
D:\>java -jar "myTest.jar"
Aug 31, 2016 7:20:21 PM org.springframework.context.support.ClassPathXmlApplicat
ionContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationCont
ext@74be95bf: startup date [Wed Aug 31 19:20:21 PKT 2016]; root of context hiera
rchy
Aug 31, 2016 7:20:22 PM org.springframework.beans.factory.xml.XmlBeanDefinitionR
eader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [resources/applicati
onContext.xml]
Exception in thread "main" java.lang.ExceptionInInitializerError
at com.my.sftp.SFTPTester.main(SFTPTester
.java:78)
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: IOExc
eption parsing XML document from class path resource [resources/applicationConte
xt.xml]; nested exception is java.io.FileNotFoundException: class path resource
[resources/applicationContext.xml] cannot be opened because it does not exist
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBea
nDefinitions(XmlBeanDefinitionReader.java:344)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBea
nDefinitions(XmlBeanDefinitionReader.java:304)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReade
r.loadBeanDefinitions(AbstractBeanDefinitionReader.java:181)
at org.springframework.context.support.AbstractXmlApplicationContext.loa
dBeanDefinitions(AbstractXmlApplicationContext.java:123)
at org.springframework.context.support.AbstractXmlApplicationContext.loa
dBeanDefinitions(AbstractXmlApplicationContext.java:93)
at org.springframework.context.support.AbstractRefreshableApplicationCon
text.refreshBeanFactory(AbstractRefreshableApplicationContext.java:129)
at org.springframework.context.support.AbstractApplicationContext.obtain
FreshBeanFactory(AbstractApplicationContext.java:612)
at org.springframework.context.support.AbstractApplicationContext.refres
h(AbstractApplicationContext.java:513)
at org.springframework.context.support.ClassPathXmlApplicationContext.<i
nit>(ClassPathXmlApplicationContext.java:197)
at org.springframework.context.support.ClassPathXmlApplicationContext.<i
nit>(ClassPathXmlApplicationContext.java:172)
at org.springframework.context.support.ClassPathXmlApplicationContext.<i
nit>(ClassPathXmlApplicationContext.java:158)
at com.my.sftp.ApplicationContextProvider.<clinit>(Applicat
ionContextProvider.java:10)
... 1 more
Caused by: java.io.FileNotFoundException: class path resource [resources/applica
tionContext.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPat
hResource.java:172)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBea
nDefinitions(XmlBeanDefinitionReader.java:330)
... 12 more
Here are the few lines of pom.xml suggesting how i am adding "resources" folder reference
<build>
<resources>
<resource><directory>src/main/resources</directory></resource>
</resources>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
这是main()代码
public static void main(String[] args)
{
ConfigurableApplicationContext context = null;
try {
context = new ClassPathXmlApplicationContext("resources/applicationContext.xml", SFTPTester.class);
context.getBean("sftpTester");
} catch (Exception e) {
e.printStackTrace();
context.close();
}
catch (Throwable t) {
t.printStackTrace();
context.close();
}
}
答案 0 :(得分:0)
使用maven jar文件时,资源文件将直接位于根级别。您必须将resources/applicationContext.xml
更改为applicationContext.xml
才能使其正常运行。
在旁注中,如果您想知道Jar文件中的内容,请将.jar更改为.zip并提取内容。您将更好地了解正在发生的事情。
答案 1 :(得分:0)
除了在java main()中更改行引用上下文之外,其他所有问题都解决了问题。现在项目在Eclipse调试以及maven生成的jar文件中运行良好。 所以我改行了
context = new ClassPathXmlApplicationContext("applicationContext.xml");