我想使用Eclipse Export功能导出我的Java项目。我的项目包括许多Spring库。我右键单击项目/导出/ JAR文件,选择项目,检查项目的所有内容,指定主类。当我用参数运行它时,会发生错误。如何解决?
F:\Downloads\MyProject>java -jar release.jar --d 2014/03/17 --l 1 --s 9001
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/b
eans/factory/BeanFactory
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.getMethod0(Unknown Source)
at java.lang.Class.getMethod(Unknown Source)
at sun.launcher.LauncherHelper.getMainMethod(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.springframework.beans.factory.B
eanFactory
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 6 more
更新 我试图将其导出为Runnable JAR文件。它显示以下错误。 (抱歉,由于安全性,我无法发布整个错误日志)
....
Invocation of init method failed; nested exception is java.io.
FileNotFoundException: ibatis\SqlMapConfig.xml (The system cannot find the path
specified)
at org.springframework.beans.factory.support.AbstractAutowireCapableBean
Factory.initializeBean(AbstractAutowireCapableBeanFactory.java:1455)
at org.springframework.beans.factory.support.AbstractAutowireCapableBean
Factory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
at org.springframework.beans.factory.support.AbstractAutowireCapableBean
Factory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getOb
ject(AbstractBeanFactory.java:294)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistr
y.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBe
an(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean
(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.
preInstantiateSingletons(DefaultListableBeanFactory.java:567)
at org.springframework.context.support.AbstractApplicationContext.finish
BeanFactoryInitialization(AbstractApplicationContext.java:913)
at org.springframework.context.support.AbstractApplicationContext.refres
h(AbstractApplicationContext.java:464)
at org.springframework.context.support.FileSystemXmlApplicationContext.<
init>(FileSystemXmlApplicationContext.java:140)
at org.springframework.context.support.FileSystemXmlApplicationContext.<
init>(FileSystemXmlApplicationContext.java:84)
at net.webike.japan.spring.batch.GlobalEstimateProcessExec.process(Globa
lEstimateProcessExec.java:148)
at net.webike.japan.spring.batch.GlobalEstimateProcessExec.main(GlobalEs
timateProcessExec.java:120)
Caused by: java.io.FileNotFoundException: ibatis\SqlMapConfig.xml (The system ca
nnot find the path specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at org.springframework.core.io.FileSystemResource.getInputStream(FileSys
temResource.java:113)
at org.springframework.orm.ibatis.SqlMapClientFactoryBean.buildSqlMapCli
ent(SqlMapClientFactoryBean.java:336)
at org.springframework.orm.ibatis.SqlMapClientFactoryBean.afterPropertie
sSet(SqlMapClientFactoryBean.java:291)
at org.springframework.beans.factory.support.AbstractAutowireCapableBean
Factory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)
at org.springframework.beans.factory.support.AbstractAutowireCapableBean
Factory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
... 13 more
更新 spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- Database -->
<bean id="test" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://myip/myProject?useUnicode=yes&characterEncoding=SJIS</value>
</property>
<property name="username">
<value>test</value>
</property>
<property name="password">
<value>test</value>
</property>
<property name="maxActive" value="30" />
<property name="maxIdle" value="8" />
<property name="maxWait" value="-1" />
<property name="validationQuery" value="select version()" />
</bean>
<bean id="test" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation">
<value>ibatis/SqlMapConfig.xml</value>
</property>
<property name="dataSource">
<ref local="dsGlobalRanking" />
</property>
</bean>
<bean id="SomethingDAO"
class="mypackage.SomethingDAOImpl"
scope="singleton">
<constructor-arg>
<ref bean="test" />
</constructor-arg>
</bean>
</beans>
然后我加载它
String contextPath = "classpath:spring.xml";
BeanFactory context =
(BeanFactory) new FileSystemXmlApplicationContext(contextPath);
答案 0 :(得分:1)
假设您在Jar中有 SqlMapConfig.xml ,例如位置:
/res/ibatis/SqlMapConfig.xml
您应该在 spring.xml 文件中引用它,如下所示:
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="classpath:/res/ibatis/SqlMapConfig.xml"/>
<property name="dataSource" ref="yourDataSource"/>
</bean>
(注意 classpath:标记)