Error scheduling jobs using Spring annotations - spring

I am trying to schedule jobs using annotations Spring.. My configuration file is as follows:
<?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:task="http://www.springframework.org/schema/task"
xmlns:util="http://www.springframework.org/schema/util"
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
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-
util.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-
task-3.0.xsd">
<task:annotation-driven />
<util:properties id="batchProp" location="application.properties" />
<context:property-placeholder properties-ref="batchProp" />
<bean id="indexer" class="package-name"></bean>
</beans>
and this is how I am invoking it:
public static void main(String[] args){
ApplicationContext appCont = new ClassPathXmlApplicationContext("Spring-Config.xml");
prop_file = appCont.getBean("batchProp", Properties.class);
}
My pom build segment is file:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>my_main_class</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-my-jar-with-dependencies</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
this works abosultely fine when I run it in Spring Tool Suite but throws an error when I run the jar file in the server.
The error is as follows:
Exception in thread "main"
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/task]
Offending resource: class path resource [Spring-Config.xml]
at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:68)
at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:85)
at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:80)
at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.error(BeanDefinitionParserDelegate.java:316)
at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1416)
at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1409)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:184)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.doRegisterBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:140)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:111)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:493)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:390)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:243)
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:131)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:527)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:441)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.mypackage.name.Indexer.main(Indexer.java:204)
Can someone please tell me what's going on?
Thank you!

Looks like there are no spring jar with "task" namespace (and maybe not only "task", but all namespaces: try to put "task:annotation-driven" after "util:properties" and you see other message) definition at servers classpath.
Check that server doesnt have old spring jars in its "libs" directory.
Try to put necessary spring jars to servers "libs" directory.

Related

cargo-maven3-plugin: unable to add datasource

I am trying to configure datasources on a Tomcat using Cargo to allow people doing local tests of the software.
When I start the application using cargo, I have the following error:
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mosesEntityManagerFactory' defined in class path resource [app-logic.xml]: Cannot resolve reference to bean 'appDataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'appDataSource' defined in class path resource [logic-datasource.xml]: Invocation of init method failed; nested exception is javax.naming.NameNotFoundException: Name [jdbc/app] is not bound in this Context. Unable to find [jdbc].
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:334)
...
The cargo maven plugin is configured in this way:
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven3-plugin</artifactId>
<version>1.10.4</version>
<configuration>
<container>
<containerId>tomcat7x</containerId>
<zipUrlInstaller>
<url>https://repo.maven.apache.org/maven2/org/apache/tomcat/tomcat/7.0.55/tomcat-7.0.55.zip</url>
</zipUrlInstaller>
<timeout>600000</timeout>
<output>${project.build.directory}/tomcat7x/container.log</output>
<append>false</append>
<log>${project.build.directory}/tomcat7x/cargo.log</log>
<dependencies>
[...] some dependencies like mysql, etc...
</dependencies>
</container>
<configuration>
<type>standalone</type>
<home>${project.build.directory}/tomcat7x/catalina-base</home>
<properties>
<cargo.jvmargs>-Xmx4096m</cargo.jvmargs>
<cargo.datasource.datasource.mysql>
cargo.datasource.driver=com.mysql.jdbc.Driver|
cargo.datasource.url=jdbc:mysql://server:3306/app|
cargo.datasource.jndi=jdbc/app|
cargo.datasource.username=ignore|
cargo.datasource.password=ignore
</cargo.datasource.datasource.mysql>
</properties>
<configfiles>
<configfile>
<file>${project.basedir}/src/test/resources/tomcat7/tomcat-users.xml</file>
<todir>conf</todir>
</configfile>
<configfile>
<file>${project.basedir}/src/test/resources/tomcat7/context.xml</file>
<todir>conf</todir>
</configfile>
</configfiles>
</configuration>
</configuration>
</plugin>
The context.xml I provide contains:
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/">
<Loader loaderClass="org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader"/>
<ResourceLink global="jdbc/app" name="jdbc/app" type="javax.sql.DataSource" />
</Context>
and the logic-datasource.xml mentioned in the exception contains:
<?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.xsd">
<bean id="appDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/app" />
</bean>
</beans>
I didn't find a way to debug what's happening by myself.
Do you know what is wrong looking at the configuration or how can I check what cargo is doing?
Thanks

Spring 4.2 - Dependancy Injection - http://www.springframework.org/schema/p error

I'm doing a book simple example for DI where execution returns:
Exception in thread "main" org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/p]
Offending resource: class path resource [META-INF/spring/app-context.xml]
where app-context.xml is :
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="provider" class="com.apress.prospring4.ch2.HelloWorldMessageProvider"/>
<bean id="renderer" class="com.apress.prospring4.ch2.StandardOutMessageRenderer" p:messageProvider-ref="provider"/>
</beans>
All prevous exercices are working fine...
External additional exercice on bean without DI works fine also...
Using Spring 4.2, Maven 3, JVM 1.8.
Any idea?
EDIT I've changed the p:notation to
<bean id="renderer" class="com.apress.prospring4.ch2.StandardOutMessageRenderer">
<property name="messageProvider" ref="provider" />
</bean>
and removed the p namespace reference so that app now works, which is good because I'm a learning newbie there.
Nevertheless I keep wondering how to have p namespace working!
As a side note i don't see any "http://www.springframework.org/schema/p" despite I can see a "http://www.springframework.org/schema/beans"; Probably my lack of knowledge.
I encountered this error when packaging the application into a single executable Jar (worked when running directly in eclipse/IntelliJ).
The problem was the spring.schemas file was being overwritten in the META-INF folder during the packaging.
You can solve that by using the maven shade plugin, and building the jar using mvn clean package
You'll need to use the transformers to append the spring files like so (and change the main class to be your own)
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>tom.lightwaverf.Main</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>

Maven Java application crashes when running from command line but not from within the Netbeans IDE

Edit: Question Rewritten:
I created a new minimal example to illustrate my problem. First here is the source code of a simple class App.java:
package testlog4j.testlog4j;
import java.io.*;
import java.net.*;
import org.apache.log4j.*;
import org.apache.log4j.xml.*;
public class App
{
static Logger logger = Logger.getLogger(App.class);
URL url = getClass().getClassLoader().getResource("log4j.xml");
public static void main( String[] args )
{
App app = new App();
DOMConfigurator.configure(app.url);
System.out.println( "Hello World!" );
System.out.println("Resource(.): " + App.class.getResource("."));
System.out.println("Resource(): " + App.class.getResource(""));
System.out.println("URL :" + app.url.toString());
logger.info("INFO");
}
}
Second, Here is the pom.xml file of the project
<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>testlog4j</groupId>
<artifactId>testlog4j</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>testlog4j</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>testlog4j.testlog4j.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.dstovall</groupId>
<artifactId>onejar-maven-plugin</artifactId>
<version>1.4.4</version>
<executions>
<execution>
<configuration>
<mainClass>testlog4j.testlog4j.App</mainClass>
<attachToBuild>true</attachToBuild>
</configuration>
<goals>
<goal>one-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
</dependencies>
<pluginRepositories>
<pluginRepository>
<id>onejar-maven-plugin.googlecode.com</id>
<url>http://onejar-maven-plugin.googlecode.com/svn/mavenrepo</url>
</pluginRepository>
</pluginRepositories>
</project>
Third, I have my log4j.xml under src/main/resources folder:
<?xml version="1.0" encoding="iso-8859-1"?>
<!--<!DOCTYPE log4j:configuration PUBLIC-->
<!--"-//APACHE//DTD LOG4J 1.2//EN" "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">-->
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
<log4j:configuration debug="true">
<appender name="default.console" class="org.apache.log4j.ConsoleAppender">
<param name="target" value="System.out" />
<param name="threshold" value="debug" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} %-5p [%c{1}] - %m%n" />
</layout>
</appender>
<appender name="main.file" class="org.apache.log4j.RollingFileAppender">
<param name="file" value="log/main.log" />
<param name="MaxFileSize" value="2MB" />
<param name="append" value="true" />
<param name="threshold" value="debug" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} %-5p [%c{1}] - %m%n" />
</layout>
</appender>
<logger name="testlog4j.testlog4j.App" additivity="false">
<level value="debug" />
<appender-ref ref="main.file" />
<appender-ref ref="default.console" />
</logger>
<root>
<priority value="debug" />
<appender-ref ref="default.console" />
<appender-ref ref="main.file" />
</root>
OK, building the application and running in Netbeans works perfectly as expected logging out into both the console and a file under the log directory.
However, running from command line (both the normal jar and oneJar) doesn't work as you can see below:
java -jar testlog4j-1.0.jar
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger
at testlog4j.testlog4j.App.<clinit>(App.java:17)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 1 more
java -jar testlog4j.1.0.one-jar.jar
log4j:ERROR Could not parse url [jar:file:/testlog4j-1.0.one-jar.jar!/main/testlog4j-1.0.jar!/log4j.xml].
java.io.FileNotFoundException: /testlog4j-1.0.one-jar.jar (No such file or directory)
at java.util.zip.ZipFile.open(Native Method)
at java.util.zip.ZipFile.<init>(ZipFile.java:215)
at java.util.zip.ZipFile.<init>(ZipFile.java:145)
at java.util.jar.JarFile.<init>(JarFile.java:154)
at java.util.jar.JarFile.<init>(JarFile.java:91)
at sun.net.www.protocol.jar.URLJarFile.<init>(URLJarFile.java:93)
at sun.net.www.protocol.jar.URLJarFile.getJarFile(URLJarFile.java:69)
at sun.net.www.protocol.jar.JarFileFactory.get(JarFileFactory.java:84)
at sun.net.www.protocol.jar.JarURLConnection.connect(JarURLConnection.java:122)
at sun.net.www.protocol.jar.JarURLConnection.getInputStream(JarURLConnection.java:150)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:613)
at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java:189)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:812)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:243)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:347)
at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:177)
at org.apache.log4j.xml.DOMConfigurator$2.parse(DOMConfigurator.java:612)
at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:711)
at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:618)
at org.apache.log4j.helpers.OptionConverter.selectAndConfigure(OptionConverter.java:470)
at org.apache.log4j.LogManager.<clinit>(LogManager.java:122)
at org.apache.log4j.Logger.getLogger(Logger.java:117)
at testlog4j.testlog4j.App.<clinit>(App.java:17)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.simontuffs.onejar.Boot.run(Boot.java:340)
at com.simontuffs.onejar.Boot.main(Boot.java:166)
log4j:ERROR Could not parse url [jar:file:/testlog4j-1.0.one-jar.jar!/main/testlog4j-1.0.jar!/log4j.xml].
java.io.FileNotFoundException: /testlog4j-1.0.one-jar.jar (No such file or directory)
at java.util.zip.ZipFile.open(Native Method)
at java.util.zip.ZipFile.<init>(ZipFile.java:215)
at java.util.zip.ZipFile.<init>(ZipFile.java:145)
at java.util.jar.JarFile.<init>(JarFile.java:154)
at java.util.jar.JarFile.<init>(JarFile.java:91)
at sun.net.www.protocol.jar.URLJarFile.<init>(URLJarFile.java:93)
at sun.net.www.protocol.jar.URLJarFile.getJarFile(URLJarFile.java:69)
at sun.net.www.protocol.jar.JarFileFactory.get(JarFileFactory.java:84)
at sun.net.www.protocol.jar.JarURLConnection.connect(JarURLConnection.java:122)
at sun.net.www.protocol.jar.JarURLConnection.getInputStream(JarURLConnection.java:150)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:613)
at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java:189)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:812)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:243)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:347)
at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:177)
at org.apache.log4j.xml.DOMConfigurator$2.parse(DOMConfigurator.java:612)
at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:711)
at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:618)
at org.apache.log4j.xml.DOMConfigurator.configure(DOMConfigurator.java:743)
at testlog4j.testlog4j.App.main(App.java:23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.simontuffs.onejar.Boot.run(Boot.java:340)
at com.simontuffs.onejar.Boot.main(Boot.java:166)
Hello World!
Resource(.): null
Resource(): jar:file:/testlog4j-1.0.one-jar.jar!/main/testlog4j-1.0.jar!/testlog4j/testlog4j/
URL :jar:file:/testlog4j-1.0.one-jar.jar!/main/testlog4j-1.0.jar!/log4j.xml
log4j:WARN No appenders could be found for logger (testlog4j.testlog4j.App).
log4j:WARN Please initialize the log4j system properly.
Your help is highly appreciated.
The first problem with NoClassDefFoundError was setting correct classpaths, I solved this by adding the maven-dependencies-plugin and included the class prefix attribute in the maven-jar-plugin.
For one-jar plugin; When building the one-jar file, DOMConfigurator.configure() method of log4j was not able to parse the log4j.xml file anymore (as can be seen in the question above). I was able to replace this by log4j.properties file and using the PropertyConfigurator.configure() method. Then the problem would be solved. However, OneJar is causing another problem for me. Since it builds the first executable jar into another jar; I will need to change my code to find the path of the parent executable jar to write log and properties files. For this reason, I changed into the maven-assembly-plugin using its non deprecated single goal. In this case I can also keep the nice log4j.xml file.
Below is the new pom.xml file:
<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>testlog4j</groupId>
<artifactId>testlog4j</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>testlog4j</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>testlog4j.testlog4j.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.5.1</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>testlog4j.testlog4j.App</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
</dependencies>
<repositories>
</repositories>
</project>
Moreover, I uploaded the testing project into github:
https://github.com/mtleis/simpleLog4jMaven
I Hope someone will find this useful.
[As answer since too long for comment]
Maybe helpful for java.lang.NoClassDefFoundError:
3 ways to solve java.lang.NoClassDefFoundError in Java J2EE
java.lang.NoClassDefFoundError: How to resolve – Part 1.
I'm not too familiar with NetBeans but I can imagine that it works therein because NetBeans accesses your classes from target/classes and log4j from your local Maven repository, not from the created jars.
Have you unzipped the jar files and checked what's in it? Are all files therein as expected and at the right place? /main/testlog4j-1.0.jar!/log4j.xml seems not to be where it is expected.

Cloudfoundry: Deploying a standalone application using Spring and RabbitMQ

I am trying to deploy a standalone application to CloudFoundry. I am using a Spring ApplicationContext in my main-method to initialize Spring and I have a simple Bean to handle the messaging with RabbitMQ.
In the spring.xml I have configured RabbitMQ and in the pom file I have added the necessary dependencies for cglib-nodep, spring-rabbit and cloudfoundry-runtime. I am further more using the maven-assembly-plugin plugin to create a single jar file which contains all the libraries, and I then deploy it to cloudfoundry with the vmc-tool.
When I deploy the jar-file to CloudFoundry I am getting the following error:
Exception in thread "main" org.springframework.beans.factory.parsing.BeanDefinitionParsingException:
Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace
[http://www.springframework.org/schema/context]
Offending resource: class path resource [spring.xml]
[stacktrace ommited]
Here is my spring.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xmlns:cloud="http://schema.cloudfoundry.org/spring"
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
http://www.springframework.org/schema/rabbit
http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd
http://schema.cloudfoundry.org/spring
http://schema.cloudfoundry.org/spring/cloudfoundry-spring-0.7.xsd">
<context:component-scan base-package="mypackage" />
<!-- Obtain a connection to the RabbitMQ via cloudfoundry-runtime: -->
<cloud:rabbit-connection-factory id="connectionFactory"/>
<!-- Set up the AmqpTemplate/RabbitTemplate: -->
<rabbit:template connection-factory="connectionFactory"/>
<!-- Request that queues, exchanges and bindings be automatically
declared on the broker: -->
<rabbit:admin connection-factory="connectionFactory"/>
<!-- Declare the "messages" queue: -->
<rabbit:queue name="messages" durable="true"/>
</beans>
And the configuration of the maven-assembly-plugin:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>at.ac.tuwien.infosys.dse2013s.group17.allocator.ui.StartUpAllocator</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
What could be the reason for the above error? Is there something wrong with how I configured the maven-assembly-plugin, or is there really a problem in the spring.xml?
Update
I was able to solve the error, by adding the spring-context dependency to my root pom as a managed dependency and then referencing it from my module without the version element. Now I am getting the same error but this time the Exception complains about the http://schema.cloudfoundry.org/spring schema.
[Update] I recommend to take a look at the cf-maven-plugin, which makes it much easier to deploy to CloudFoundry, so its no longer needed to work with the vmc tool directly. The GitHub page explains both howto deploy normal and standalone applications with this plugin, and also explains to use the appassembler-maven-plugin for standalone applications. This blog post is also useful to get started.
[/Update]
It turned out I was deploying the application in a wrong way to CloudFoundry. I was using the maven-assembly-plugin, which unpacked the class files of all dependencies into the jar file. In fact I had to use the appassembler-maven-plugin:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>appassembler-maven-plugin</artifactId>
<version>1.1.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>assemble</goal>
</goals>
<configuration>
<assembledirectory>target</assembledirectory>
<programs>
<program>
<mainClass>my.package.myclass</mainClass>
</program>
</programs>
</configuration>
</execution>
</executions>
</plugin>
I then deployed the application to CloudFoundry like this from the the target/ folder
vmc push <myappname> --path=appassembler
As run command I needed to choose the name of the run script in the appassembler/bin folder. For example bin/start-up-myapp.

Apache Camel Spring configuration error - Unable to locate Spring NamespaceHandler for XML schema namespace [http://camel.apache.org/schema/spring]

I've posted an excerpt from my spring.xml file and the stack trace I'm getting.
<?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:lang="http://www.springframework.org/schema/lang"
xmlns:util="http://www.springframework.org/schema/util" xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.5.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring-2.0.0.xsd"
default-init-method="init" default-lazy-init="false">
<!-- import web service configuration -->
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
<package>com.testdomain.testpackage</package>
</camelContext>
...
</beans>
Which throws this exception:
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://camel.apache.org/schema/spring]
Offending resource: class path resource [spring-config.xml]
at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:68)
at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:85)
at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:80)
at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.error(BeanDefinitionParserDelegate.java:281)
at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1294)
at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1287)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:135)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:92)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:507)
at.......
Can somebody please help where the issue is?
For Camel versions < 3.8, add camel-spring.jar to your project. For versions after 3.8 add camel-spring-xml as well.
Using the spring xml dsl has been split off
If building a JAR with maven assembly plugin then try the shade plugin :)
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<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>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.mypackage.mainclass</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>

Resources