I have created a runnable jar using maven assembly plugin:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.loaders.FundLoader</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
I have some Spring dependencies packaged into the jar.
When I run the program using intellIj the program runs perfectly fine.
However when I run the program from the command line using: java -jar myjar.jar
I got the following exception:
Exception in thread "main" org.springframework.beans.factory.xml.XmlBeanDefiniti
onStoreException: Line 6 in XML document from class path resource [spring/loader-context.xml] is invalid; nested exception is org.xml.sax.SAXParseExceptio
n; lineNumber: 6; columnNumber: 120; cvc-elt.1: Cannot find the declaration of e
lement 'beans'.
Attached my config file: loader-context.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config />
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:hibernate.properties</value>
</list>
</property>
<property name="ignoreResourceNotFound" value="false"/>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${hibernate.driver.class.name}" />
<property name="url" value="${hibernate.jdbc.url}" />
<property name="username" value="${hibernate.username}" />
<property name="password" value="${hibernate.password}" />
<property name="defaultAutoCommit" value="false" />
</bean>
</beans>
And then the content of my pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.operations</groupId>
<artifactId>parent</artifactId>
<version>3.0.2-SNAPSHOT</version>
</parent>
<artifactId>loader</artifactId>
<name>[FOR] loader</name>
<packaging>jar</packaging>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.loaders.FundLoader</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>net.sf.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc14</artifactId>
<version>10.2.0.4.0</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.5</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
<scope>runtime</scope>
</dependency>
</dependencies>
Could someone please help me out ?
Thank you
Spring applications cannot be simly packaged as a fat JAR with the maven-assembly-plugin, for each Spring module JAR has its schemas located in a file that is always at the same location (META-INF/spring.schemas).
When you flatten all your dependencies in one final JAR, you will obviously only has one of these spring.schemas files in your resulting JAR, hence your issue.
I would suggest to either use Spring Boot for developing your application, as it includes a plugin that deals nicely with Spring JARs.
Or you can also use onejar-maven-plugin, which also packages the fat JAR in a specific way that avoids having this issue.
Other solutions also exists, such as the maven-shade-plugin and its AppendingTransformer
You can even provide merged versions of the problematic files by yourself, as project resources, and then excluding the originals from being assembled.
Related
I have just created simple Java project, it works fine when I run the from intellji, however when I convert to the jar file and run, gives me error like this: (when i remove Property 'includeEventTypes, it works fine with jar!!)
Bean 'ignite.cfg'; nested exception is org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/util]
Offending resource: URL [jar:file:.../JarIgnite/target/JarIgnite-1.0-SNAPSHOT-jar-with-dependencies.jar!/config.xml]
Bean 'ignite.cfg'
-> Property 'includeEventTypes']
at org.apache.ignite.internal.util.spring.IgniteSpringHelperImpl.applicationContext(IgniteSpringHelperImpl.java:392)
at org.apache.ignite.internal.util.spring.IgniteSpringHelperImpl.loadConfigurations(IgniteSpringHelperImpl.java:104)
at org.apache.ignite.internal.util.spring.IgniteSpringHelperImpl.loadConfigurations(IgniteSpringHelperImpl.java:98)
at org.apache.ignite.internal.IgnitionEx.loadConfigurations(IgnitionEx.java:744)
at org.apache.ignite.internal.IgnitionEx.start(IgnitionEx.java:945)
at org.apache.ignite.internal.IgnitionEx.start(IgnitionEx.java:854)
at org.apache.ignite.internal.IgnitionEx.start(IgnitionEx.java:724)
at org.apache.ignite.internal.IgnitionEx.start(IgnitionEx.java:693)
at org.apache.ignite.Ignition.start(Ignition.java:352)
... 2 more
There is no validation error in pom.xml and config.xml,
Here is pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.JarIgnite</groupId>
<artifactId>JarIgnite</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.ignite</groupId>
<artifactId>ignite-core</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.ignite</groupId>
<artifactId>ignite-spring</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.ignite</groupId>
<artifactId>ignite-log4j</artifactId>
<version>2.5.0</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<groupId>org.apache.maven.plugins</groupId>
<executions>
<execution>
<id>make-executable-jar-with-dependencies</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>default_package.MainApp</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Here is the config.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"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<bean id="ignite.cfg" class="org.apache.ignite.configuration.IgniteConfiguration">
<!-- Set to true to enable distributed class loading for examples, default is false. -->
<property name="peerClassLoadingEnabled" value="true"/>
<!-- Enable task execvcution events for examples. -->
<property name="includeEventTypes">
<list >
<!--Task execution events-->
<util:constant static-field="org.apache.ignite.events.EventType.EVT_TASK_STARTED"/>
<util:constant static-field="org.apache.ignite.events.EventType.EVT_TASK_FINISHED"/>
<util:constant static-field="org.apache.ignite.events.EventType.EVT_TASK_FAILED"/>
<util:constant static-field="org.apache.ignite.events.EventType.EVT_TASK_TIMEDOUT"/>
<util:constant static-field="org.apache.ignite.events.EventType.EVT_TASK_SESSION_ATTR_SET"/>
<util:constant static-field="org.apache.ignite.events.EventType.EVT_TASK_REDUCED"/>
<!--Cache events-->
<util:constant static-field="org.apache.ignite.events.EventType.EVT_CACHE_OBJECT_PUT"/>
<util:constant static-field="org.apache.ignite.events.EventType.EVT_CACHE_OBJECT_READ"/>
<util:constant static-field="org.apache.ignite.events.EventType.EVT_CACHE_OBJECT_REMOVED"/>
</list>
</property>
<property name="gridLogger">
<bean class="org.apache.ignite.logger.log4j.Log4JLogger">
<constructor-arg type="java.lang.String" value="/home/nova-stats/Desktop/log/ignite-log4j.xml"/>
</bean>
</property>
<!-- Explicitly configure TCP discovery SPI to provide list of initial nodes. -->
<property name="discoverySpi">
<bean class="org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi">
<property name="ipFinder">
<!--
Ignite provides several options for automatic discovery that can be used
instead os static IP based discovery. For information on all options refer
to our documentation: http://apacheignite.readme.io/docs/cluster-config
-->
<!-- Uncomment static IP finder to enable static-based discovery of initial nodes. -->
<!--<bean class="org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder">-->
<bean class="org.apache.ignite.spi.discovery.tcp.ipfinder.multicast.TcpDiscoveryMulticastIpFinder">
<property name="addresses">
<list>
<!-- In distributed environment, replace with actual host IP address. -->
<value>127.0.0.1:47500..47509</value>
</list>
</property>
</bean>
</property>
</bean>
</property>
</bean>
</beans>
Add next dependencies (you must follow specified order!)
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.1.1.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.ignite/ignite-core -->
<dependency>
<groupId>org.apache.ignite</groupId>
<artifactId>ignite-core</artifactId>
<version>2.6.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.ignite/ignite-spring -->
<dependency>
<groupId>org.apache.ignite</groupId>
<artifactId>ignite-spring</artifactId>
<version>2.6.0</version>
</dependency>
I have a jar which reads a excel template. If I use Maven shade to package without using resources filter and execute my jar, it is ok; However, I got some exception after I use Maven resources filter! The exception which occurs when I execute jar is as follows:
2018-01-05 11:20:45,631 [main] INFO tw.com.test.aop.SystemPointcuts- AOP Aro
und start : ServiceImpl.ServiceImpl.genExcel(..)(). param : 20171106,20171112
Exception in thread "main" org.apache.poi.openxml4j.exceptions.InvalidOperationE
xception: Can't open the specified file: 'C:\Users\seesaw\AppData\Local\Temp\Wee
klyTelecomAnalysis2553686971923113156.xlsx'
at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:131)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:246)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:200)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.ja
va:90)
at tw.com.test.weeklyTelecomAnalysis.service.ServiceImpl.genExcel(Ser
viceImpl.java:73)
at tw.com.test.weeklyTelecomAnalysis.service.ServiceImpl$$FastClassBy
SpringCGLIB$$9d7f5cb8.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:2
04)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation
.invokeJoinpoint(CglibAopProxy.java:738)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(
ReflectiveMethodInvocation.java:157)
at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.p
roceed(MethodInvocationProceedingJoinPoint.java:85)
at tw.com.test.aop.SystemPointcuts.execAround(SystemPointcuts.java:59
)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMet
hodWithGivenArgs(AbstractAspectJAdvice.java:629)
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMet
hod(AbstractAspectJAdvice.java:618)
at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAro
undAdvice.java:70)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(
ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invok
e(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(
ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterce
ptor.intercept(CglibAopProxy.java:673)
at tw.com.test.weeklyTelecomAnalysis.service.ServiceImpl$$EnhancerByS
pringCGLIB$$1731d8a8.genExcel(<generated>)
at tw.com.test.weeklyTelecomAnalysis.main.Entry.main(Entry.java:45)
Caused by: java.util.zip.ZipException: error in opening zip file
at java.util.zip.ZipFile.open(Native Method)
at java.util.zip.ZipFile.<init>(ZipFile.java:219)
at java.util.zip.ZipFile.<init>(ZipFile.java:149)
at java.util.zip.ZipFile.<init>(ZipFile.java:163)
at org.apache.poi.openxml4j.opc.internal.ZipHelper.openZipFile(ZipHelper
.java:157)
at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:129)
... 23 more
here's my pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>tw.com.test</groupId>
<artifactId>WeeklyTelecomAnalysis</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<name>WeeklyTelecomAnalysis</name>
<profiles>
<profile>
<id>prod</id>
<properties>
<pps.datasource>jdbc:oracle:thin:#11.111.11.111:1111:MYPROD</pps.datasource>
<pps.username>xxxxxx</pps.username>
<pps.password>xxxxxx</pps.password>
</properties>
</profile>
</profiles>
<properties>
<!-- 執行類別 -->
<mainClass>tw.com.test.weeklyTelecomAnalysis.main.Entry</mainClass>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jdk.target.version>1.7</jdk.target.version>
<maven.compiler.source>${jdk.target.version}</maven.compiler.source>
<maven.compiler.target>${jdk.target.version}</maven.compiler.target>
<org.springframework.version>4.3.8.RELEASE</org.springframework.version>
</properties>
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!-- commons -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>com.springsource.org.apache.commons.dbcp</artifactId>
<version>1.2.2.osgi</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>com.springsource.org.apache.commons.lang</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>com.springsource.org.apache.commons.pool</artifactId>
<version>1.5.3</version>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
<!-- others -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
<!-- <dependency> -->
<!-- <groupId>org.apache.poi</groupId> -->
<!-- <artifactId>poi</artifactId> -->
<!-- <version>3.16</version> -->
<!-- </dependency> -->
<!-- <dependency> -->
<!-- <groupId>org.apache.poi</groupId> -->
<!-- <artifactId>poi-ooxml</artifactId> -->
<!-- <version>3.16</version> -->
<!-- </dependency> -->
<!-- 安裝ojdbc6.jar至本地maven : mvn install:install-file -Dfile=ojdbc6.jar -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=11.2.0 -Dpackaging=jar -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0</version>
</dependency>
<dependency>
<groupId>org.junit</groupId>
<artifactId>com.springsource.org.junit</artifactId>
<version>4.9.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<repositories>
<repository>
<id>com.springsource.repository.bundles.external</id>
<name>EBR External Release Repository</name>
<url>http://repository.springsource.com/maven/bundles/external</url>
</repository>
<repository>
<id>com.springsource.repository.libraries.external</id>
<name>EBR Libraries Release Repository</name>
<url>http://repository.springsource.com/maven/libraries/external</url>
</repository>
</repositories>
<build>
<finalName>WeeklyTelecomAnalysis</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.7.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>${mainClass}</mainClass>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
here's my application-context.xml which is for spring framework and jdbc config, I use Maven resource filter to replace some parameter, but after I do it, I got some error which is as mentioned above.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:aop="http://www.springframework.org/schema/aop"
xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- enable autowiring of components -->
<context:component-scan base-package="tw.com.test" />
<context:annotation-config />
<aop:aspectj-autoproxy />
<!-- MYPROD -->
<bean id="pps_dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close" lazy-init="false">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="${pps.datasource}" />
<property name="username" value="${pps.username}" />
<property name="password" value="${pps.password}" />
<property name="maxActive" value="20" />
<property name="maxIdle" value="0" />
<property name="maxWait" value="180000" />
<property name="removeAbandoned" value="true" />
<property name="removeAbandonedTimeout" value="300" />
<property name="logAbandoned" value="true" />
<property name="testOnBorrow" value="true" />
<property name="testWhileIdle" value="true" />
<property name="validationQuery" value="select 1 from dual" />
</bean>
<bean id="pps_sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="pps_dataSource" />
<property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml" />
</bean>
<bean id="pps_sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="pps_sqlSessionFactory" />
</bean>
<bean id="pps_stsMapper" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionTemplateBeanName" value="pps_sqlSessionTemplate" />
<property name="basePackage" value="tw.com.test.weeklyTelecomAnalysis.manual.pps" />
</bean>
</beans>
here's the program which read the excel template
public XSSFWorkbook genExcel(String fromDate, String toDate) throws Exception {
// get Excel template
Resource resource = new ClassPathResource("excel/WeeklyTelecomAnalysis.xlsx");
File excel = stream2file(resource.getInputStream(), "WeeklyTelecomAnalysis", ".xlsx");
XSSFWorkbook workbook = (XSSFWorkbook) WorkbookFactory.create(excel);
// style
this.setStyle(workbook);
XSSFSheet sheet1 = workbook.getSheet("WeeklyTelecomAnalysis"); // template
XSSFSheet sheet2 = workbook.createSheet("detail");
// start
createSheet1(sheet1, fromDate, toDate);
createSheet2(sheet2, fromDate, toDate);
return workbook;
}
public static File stream2file (InputStream in, String prefix, String suffix) throws IOException {
final File tempFile = File.createTempFile(prefix, suffix);
tempFile.deleteOnExit();
try (FileOutputStream out = new FileOutputStream(tempFile)) {
IOUtils.copy(in, out);
}
return tempFile;
}
could anyone help me?
It would seem like your .xlsx contains some values that shouldn't be replaced but are replaced by filtering, breaking the file.
I just want to use "mvn clean package -Pprod"to filter the parameters that are url,username and password in the application-context.xml rather than the excel template.
Given that, just do
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<includes>
<include>**/*.xlsx</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
to only filter your xml configs, leaving your excel template out.
I am not able to resolve the issue am getting.
Full stacktrace:
12:14:08.172 [main] DEBUG o.s.core.env.StandardEnvironment - Adding [systemProperties] PropertySource with lowest search precedence
12:14:08.178 [main] DEBUG o.s.core.env.StandardEnvironment - Adding [systemEnvironment] PropertySource with lowest search precedence
12:14:08.178 [main] DEBUG o.s.core.env.StandardEnvironment - Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment]
12:14:08.183 [main] INFO o.s.c.s.ClassPathXmlApplicationContext - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext#24273305: startup date [Thu Mar 16 12:14:08 GMT 2017]; root of context hierarchy
12:14:08.222 [main] DEBUG o.s.core.env.StandardEnvironment - Adding [systemProperties] PropertySource with lowest search precedence
12:14:08.223 [main] DEBUG o.s.core.env.StandardEnvironment - Adding [systemEnvironment] PropertySource with lowest search precedence
12:14:08.223 [main] DEBUG o.s.core.env.StandardEnvironment - Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment]
12:14:08.233 [main] INFO o.s.b.f.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [org/postprocessor/config/dataSupport.xml]
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [org/postprocessor/config/dataSupport.xml]; nested exception is java.io.FileNotFoundException: class path resource [org/postprocessor/config/dataSupport.xml] cannot be opened because it does not exist
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:344)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:181)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:217)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:252)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:129)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:612)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:513)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at org.postprocessor.dao.DaoMain.main(DaoMain.java:12)
Caused by: java.io.FileNotFoundException: class path resource [org/postprocessor/config/dataSupport.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:330)
... 13 more
Bean config file dataSupport.xml is here:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- <bean/> definitions here -->
<context:annotation-config />
<context:component-scan base-package="org.postprocessor.dao" />
<context:component-scan base-package="org.postprocessor.model" />
<context:component-scan base-package="org.postprocessor.util" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="org.apache.derby.jdbc.EmbeddedDriver"
/> <property name="url" value="jdbc:derby:postprocessor;create=true;" />
<!-- <property name="driverClassName" value="org.apache.derby.jdbc.ClientDriver" />
<property name="url" value="jdbc:derby://localhost:1527/db;create=true;" /> -->
<property name="initialSize" value="1" />
<property name="maxActive" value="1" />
</bean>
<bean id="daoImpl" class="org.postprocessor.dao.DaoImpl" scope="prototype">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>resourceBundles/general</value>
<value>resourceBundles/codeLists/codelist_eortcqlqc30</value>
<value>resourceBundles/codeLists/codelist_eortcqlqlc13</value>
<value>resourceBundles/codeLists/codelist_eq5d3l</value>
<value>resourceBundles/codeLists/codelist_eq5d5l</value>
<value>resourceBundles/codeLists/codelist_noncompletedquestionnaires</value>
<value>resourceBundles/codeLists/codelist_pgis</value>
<value>resourceBundles/codeLists/codelist_silc</value>
</list>
</property>
</bean>
snipped from pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>SBP</groupId>
<artifactId>SBP</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>gt20047</name>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.1.RELEASE</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-asm -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-asm</artifactId>
<version>3.1.4.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/asm/asm-all -->
<dependency>
<groupId>asm</groupId>
<artifactId>asm-all</artifactId>
<version>3.3.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.derby/derbynet -->
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbynet</artifactId>
<version>10.12.1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.derby/derbyclient -->
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbyclient</artifactId>
<version>10.12.1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>2.0.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-dbcp/commons-dbcp -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-pool/commons-pool -->
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>1.6</version>
</dependency>
<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>3.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-io -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>19.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-collections/commons-collections -->
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>net.lingala.zip4j</groupId>
<artifactId>zip4j</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
<build>
<finalName>gt20047</finalName>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3</version>
<configuration>
<archive>
<manifest>
<!-- Jar file entry point -->
<mainClass>org.postprocessor.dao.DaoMain</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
am not able to figure out where is the issue, my jar file doesn't have this config folder
DaoMain.java
package org.postprocessor.dao;
import java.io.File;
import org.postprocessor.util.FileZipper;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class DaoMain {
public static void main(String[] args) {
ApplicationContext ctx= new ClassPathXmlApplicationContext("classpath:/org/postprocessor/config/dataSupport.xml");
DaoImpl daoImpl=null;
FileZipper fileZipper=null;
try{
daoImpl= ctx.getBean("daoImpl",DaoImpl.class);
fileZipper= ctx.getBean("fileZipper",FileZipper.class);
daoImpl.startProcess();
fileZipper.zipFile(new File("").getAbsolutePath()+"/output");
}
catch(Exception e){
e.printStackTrace();
}
finally{
((ClassPathXmlApplicationContext) ctx).close();
}
}
}
How to solve this issue?
Looks like the main problem is at this line:
new ClassPathXmlApplicationContext("classpath:/org/postprocessor/config/dataSupport.xml");
You are trying to build context from classpath. Thus you have to locate your config file accessible from a classpath of your project.
You using Maven for building your project. Maven has defined project structure for code sources:
/src/main/java
and for resources:
/src/main/resorces
You can find more here: Introduction to the Standard Directory Layout
If you want to make your file accessible from classpath you have to put it exactly under - resources/ folder:
Now you main() will be, something like:
public static void main(String[] args) {
ApplicationContext ctx= new ClassPathXmlApplicationContext("classpath:config/dataSupport.xml");
Object dataSource = ctx.getBean("userRepo");
System.out.println(dataSource.getClass().getName());
// use your beans here
I created just demo bean. And here is output:
2017-03-20 15:22:40 INFO ClassPathXmlApplicationContext - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext#16f65612: startup date [Mon Mar 20 15:22:40 EET 2017]; root of context hierarchy
2017-03-20 15:22:40 INFO XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [config/dataSupport.xml]
demo.repository.UserRepository
BTW you can use try with resources for application context:
try(ClassPathXmlApplicationContext ctx =
new ClassPathXmlApplicationContext("classpath:config/dataSupport.xml")) {
// use your beans here
}
Additional resources:
using classpath: in spring
Java Spring - How to use classpath to specify a file location?
Your XML files are not well formed.
While I do not know if this is the issue, it may be the root cause of why your project is not taking the files correctly in the first place.
Your <beans> tag does not have a closing tag. Try putting </beans> at the end of the file.
Also, your pom.xml file has the same issue. The <project> is not closed. Try putting </project> at the end of the file.
I think the following should work:
ApplicationContext ctx= new ClassPathXmlApplicationContext("org/postprocessor/config/dataSupport.xml");
You don't need to specify "classpath" since you already use ClassPathXmlApplicationContext, and you have extra slash in the beginning.
move:
src/org/postprocessor/config/dataSupport.xml
to:
src/main/resources/org/postprocessor/config/dataSupport.xml
By default maven-compiler-plugin not includes non-java files. which left you with two options.
1. move your resource files (.properties, .xml etc) to "src/main/resources" (recomended.)
2. use maven resource plugin
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/path/where/you/want</outputDirectory>
<resources>
<resource>
<directory>${basedir}</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
I'm working on a project made with :
Web Dynamic Project + JPA 2.1 / EclipseLink 2.5.2
Tomcat 8.0.28 / JRE 1.8.0_66
Eclipse Luna
JPA part of the project was running fine when executed in a simple JPA project as a Java Application.
I made no changes on that part of the code.
I wanted to print some request results in a JSP. I generated a servlet and tried to print the name of a user already created in the database. For hours I had errors about JDBC driver and persistence.xml not found.
In order to solve dependancies problems I added Maven.
Now, my user is printed in the JSP and that's fine, but my "Run.java" class I used previously doesn't work anymore.
I'm quiet lost with the error I get when running my app as a "Java Application" :
Exception in thread "main" java.lang.ExceptionInInitializerError
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.<clinit>(EntityManagerFactoryProvider.java:55)
at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactoryImpl(PersistenceProvider.java:92)
at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactory(PersistenceProvider.java:188)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:79)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
at dao.LayerDAO.<init>(LayerDAO.java:16)
at exec.Run.main(Run.java:18)
Caused by: java.lang.NullPointerException
at org.eclipse.persistence.platform.server.NoServerPlatformDetector.checkPlatform(NoServerPlatformDetector.java:28)
at org.eclipse.persistence.platform.server.ServerPlatformUtils.detectServerPlatform(ServerPlatformUtils.java:58)
at org.eclipse.persistence.internal.jpa.IsolatedHashMap.<clinit>(IsolatedHashMap.java:48)
... 7 more
Clearly the error occurs when I'm creating the EntityManagerFactory in the DAO class (the same one that is working with the JSP).
Here is my POM :
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>CitizenWeb</groupId>
<artifactId>com.citizenweb</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<!-- Generic properties -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- Web -->
<jsp.version>2.2</jsp.version>
<jstl.version>1.2</jstl.version>
<servlet.version>2.5</servlet.version>
<!-- Logging -->
<logback.version>1.0.13</logback.version>
<slf4j.version>1.7.5</slf4j.version>
<!-- Test -->
<junit.version>4.11</junit.version>
</properties>
<dependencies>
<!-- Other Web dependencies -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>${servlet.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>${jsp.version}</version>
<scope>provided</scope>
</dependency>
<!-- Logging with SLF4J & LogBack -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
<scope>runtime</scope>
</dependency>
<!-- EclipseLink -->
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.core</artifactId>
<version>2.5.2</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.jpa</artifactId>
<version>2.5.2</version>
</dependency>
<!-- MySQL DB -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.37</version>
</dependency>
</dependencies>
</project>
If needed, here is my persistence.xml :
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="CitizenWeb" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<mapping-file>META-INF/orm.xml</mapping-file>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/citizen"/>
<property name="javax.persistence.jdbc.user" value="user"/>
<property name="javax.persistence.jdbc.password" value="user"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
</properties>
</persistence-unit>
</persistence>
Entities mapping is managed with an orm.xml
Here's also the beginning of the DAO class :
package dao;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Persistence;
import javax.persistence.Query;
import entities.Address;
import entities.GroupAssoc;
import entities.RightsGranted;
import entities.User;
public class LayerDAO implements IFLayerDAOLocal {
EntityManager em = Persistence.createEntityManagerFactory("CitizenWeb").createEntityManager();
public void createUser(User user) {
System.out.println("DAO > addUser > "+em.hashCode());
em.getTransaction().begin();
em.persist(user);
em.getTransaction().commit();
}
Nothing has been changed here, neither in the persistence.xml
The "only" changes are in the build.path because of the change in the nature of project (JPA -> Web Dynamic+JPA). Maven has solved the problem with the webserver and the mysql connector, but now the app side is broken.
Do you see something I don't ?
Thanx.
In the buildpath of the project, I added an alternate "JRE System Library".
I previously had : JDK 1.8.0_66
I added : JRE 1.8.0_66
Now JSP works and also java Run class.
I am having a project using OSGi-(felix), SpringDM, hibernate, maven. when I installed bundle, it is ok, the output when I run bundle id is:
LastModified 1384619994484
Headers [Manifest-Version=1.0, Bundle-Vendor=NguyenVinhLinh, Bnd-LastModified=1384619954778, Tool=Bnd-2.1.0.20130426-122213, Bundle-Name=DrugManager, Built-By=nguyenvinhlinh, Import-Package=org.hibernate,org.hibernate.classic,org.hibernate.criterion,org.springframework.beans.factory;version="[2.5,3)",org.springframework.core.io;version="[2.5,3)",org.springframework.transaction.annotation;version="[2.5,3)", Bundle-SymbolicName=DrugManagerDAO, Export-Package=drug,drugGroup,model;version="1.0.0", Bundle-Version=1.0.0, Build-Jdk=1.7.0_45, Created-By=Apache Maven Bundle Plugin, Bundle-ManifestVersion=2]
BundleContext null
Revisions [169.0]
BundleId 169
SymbolicName DrugManagerDAO
RegisteredServices null
ServicesInUse null
Version 1.0.0
Location file:/home/nguyenvinhlinh/Projects/felix-framework-4.2.1/bundle/DrugManager-1.0.jar
State 2
Bundle 169|Installed | 1|DrugManagerDAO (1.0.0)
This is what I see, when I start this bundle:
org.osgi.framework.BundleException: Unresolved constraint in bundle DrugManagerDAO [169]: Unable to resolve 169.0: missing requirement [169.0] osgi.wiring.package; (osgi.wiring.package=org.hibernate)
This is my beans.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"
xmlns:osgi="http://www.springframework.org/schema/osgi"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/osgi
">
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingDirectoryLocations" ref="mappingProvider"/>
<property name="hibernateProperties" ref="propertiesProvider"/>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/WOLOLO"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<bean id="mappingProvider" class="hibernate.config.HibernateMappingProvider"/>
<bean id="propertiesProvider" class="hibernate.config.HibernatePropertiesProvider"/>
<bean id="drugDao" class="drug.HibernateDrugDao">
<property name="clazz" value="model.Drug"/>
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="drugGroupDao" class="drugGroup.HibernateDrugGroupDao">
<property name="clazz" value="model.DrugGroup"/>
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<osgi:service ref="drugDao" interface="drug.DrugDao"/>
<osgi:service ref="drugGroupDao" interface="drugGroup.DrugGroupDao"/>
</beans>
This is my pom file:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>DrugManager</groupId>
<artifactId>DrugManager</artifactId>
<version>1.0</version>
<packaging>bundle</packaging>
<dependencies>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>4.3.0</version>
</dependency>
<dependency>
<groupId>hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>1.8.0.10</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.5.6</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>20030825.184428</version>
</dependency>
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>20030825.183949</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate</artifactId>
<version>3.2.6.ga</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.27</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.4.0</version>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>DrugManagerDAO</Bundle-SymbolicName>
<Bundle-Vendor>NguyenVinhLinh</Bundle-Vendor>
<Export-Package>"drug,drugGroup,model"</Export-Package>
<Import-Package>org.hibernate</Import-Package> <!-- this line I try to insert into to fix requirement, but it does'nt work-->
</instructions>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>java.net</id>
<url>http://download.java.net/maven/2/</url>
</repository>
</repositories>
</project>
In addition, there is an completed error of bundle in felix.
g! ERROR: Bundle DrugManagerDAO [190] Error starting file:DrugManager-1.0.jar (org.osgi.framework.BundleException: Unresolved constraint in bundle DrugManagerDAO [190]: Unable to resolve 190.0: missing requirement [190.0] osgi.wiring.package; (osgi.wiring.package=org.hibernate))
org.osgi.framework.BundleException: Unresolved constraint in bundle DrugManagerDAO [190]: Unable to resolve 190.0: missing requirement [190.0] osgi.wiring.package; (osgi.wiring.package=org.hibernate)
at org.apache.felix.framework.Felix.resolveBundleRevision(Felix.java:3974)
at org.apache.felix.framework.Felix.startBundle(Felix.java:2037)
at org.apache.felix.framework.Felix.setActiveStartLevel(Felix.java:1291)
at org.apache.felix.framework.FrameworkStartLevelImpl.run(FrameworkStartLevelImpl.java:304)
at java.lang.Thread.run(Thread.java:744)
Do you have this bundle installed in your container (Felix) as well?
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate</artifactId>
<version>3.2.6.ga</version>
</dependency>
If not install it and then see what happens when you start your bundle.
For missing requirement [Bundle-number] osgi.wiring.package,
I added it as export-package in pom.xml, as mentioned below:
<Export-Package>
package for which error is thrown
</Export-Package>
and it worked.
So if it throws the above error even after declaring it as dependency, export-package may help