15

I am able to compile and start my Spring project using Maven:

mvn -e clean compile exec:java -Dexec.mainClass=de.fraunhofer.fkie.tet.vmware.manager.Test

However, when I assemble all jars in a single file using the maven-assembly-plugin (including applicationContext.xml), I always get an Exception during the java execution:

java -cp target/test-jar-with-dependencies.jar:. de.fraunhofer.fkie.tet.vmware.manager.Test

  INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
  Sep 6, 2010 10:37:21 AM org.springframework.util.xml.SimpleSaxErrorHandler warning
  WARNING: Ignored XML validation warning
  org.xml.sax.SAXParseException: schema_reference.4: Failed to read schema document 'http://www.springframework.org/schema/context/spring-context.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.
  ...
  Exception in thread "main" org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: 
  Line 10 in XML document from class path resource [applicationContext.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-complex-type.2.4.c: 
  The matching wildcard is strict, but no declaration can be found for element 'context:annotation-config'.
  ...
  Caused by: org.xml.sax.SAXParseException: cvc-complex-type.2.4.c: 
  The matching wildcard is strict, but no declaration can be found for element 'context:annotation-config'.

I also tried to attach the schema definitions, i.e. spring-context.xsd etc., directly to the classpath, but without any success.

less src/main/resources/applicationContext.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.xsd
                             http://www.springframework.org/schema/context 
                             http://www.springframework.org/schema/context/spring-context.xsd">

      <context:annotation-config />   <!-- wegen '@PostConstruct' -->
  <!--<context:component-scan base-package="de.fraunhofer" />     -->
  ...
  </beans>
rmv
  • 3,195
  • 4
  • 26
  • 29
  • Can you post your applicationContext.xml - or at least the header section. I would suspect that there is a problem with your headers/dtd reference, etc. – Harry Lime Sep 06 '10 at 09:55
  • 'applicationContext.xml' is posted above. Amazingly, the application runs without any problem, if 'mvn exec:java' is used to start it. – rmv Sep 06 '10 at 10:32

6 Answers6

19

Spring namespace handlers are resolved using files /META-INF/spring.schemas and /META-INF/spring.handlers. Because files with these names exist in different Spring jars, probably only one of them remains in target jar after maven-assembly-plugin.

Perhaps you may merge these files manually and somehow configure maven-assembly-plugin to overwrite file in target jar with this merged file.

axtavt
  • 239,438
  • 41
  • 511
  • 482
  • 2
    Yes, you are right, that's it! The independent '/META-INF/spring.handlers' files from different jars get lost in the final jar. Facing this, i decide not to use the 'maven-assembly-plugin' for now, but include all jars manually from the 'target/lib/' directory (after 'mvn install'), e.g. 'java -cp target/lib/spring-aop-3.0.4.RELEASE.jar:target/lib/spring-asm-3.0.4.RELEASE.jar:[...]:target/classes de.fraunhofer.fkie.tet.vmware.manager.Test'. This works as expected. Thank you very much! ;-) – rmv Sep 06 '10 at 13:59
  • 3
    @rmv Or you can let the maven-dependency-plugin do the copy for you, as in [here](http://stackoverflow.com/questions/574594/how-can-i-create-an-executable-jar-with-dependencies-using-maven/4323501#4323501) – staromeste Oct 17 '11 at 11:15
5

I suspect your spring config file is missing the context XML namespace. It should be added to the root element of your spring config file like this:

<?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">

    <context:annotation-config />
    <context:component-scan base-package="com.abc.xyz" />

</beans>
Betlista
  • 10,327
  • 13
  • 69
  • 110
Abhinav Sarkar
  • 23,534
  • 11
  • 81
  • 97
  • Thanks for the rapid answer! I didn't post my applicationContext.xml file, because 'mvn exec:java -Dexec.mainClass=de.fraunhofer.fkie.tet.vmware.manager.Test ' works fine. So i think there is nothing wrong with the configuration file in principal, it actually contains all elements from your example above. The problem occurs, when i try to start the assembled version directly from Java. – rmv Sep 06 '10 at 09:22
3

I've found the root problem for this as per the reply of axtavt, and I've reported it as a ?bug? in Spring: https://jira.springsource.org/browse/SPR-8368 - a workaround to generate your own merged copies of these files is included there. For posterity the code is here as well:

//IOUtils and FileUtils come from Apache Commons IO
for(String s : new String[] {"spring.schemas", "spring.handlers", "spring.tooling"}) {
    Enumeration<?> e = Test.class.getClassLoader().getResources("META-INF/"+s);
    StringBuilder out = new StringBuilder();
    while(e.hasMoreElements()) {
        URL u = (URL) e.nextElement();
        out.append(IOUtils.toString(u.openStream())).append("\n");
    }
    File outf = new File(s);
    FileUtils.writeStringToFile(outf, out.toString(), "UTF-8");
}
Ian
  • 71
  • 5
1

I believe there are 3 solutions to this problem

  1. The tx jar file should be included in the classpath/buildpath of the project. (most common error)
  2. Mentioned by "axtavt" above
  3. Try this before "axtaxt" solution: go to your war directory (specified in GWT compile's advanced tab) and put the spring-tx.jar file in the lib folder under your war directory, refresh and run again.
keyser
  • 18,829
  • 16
  • 59
  • 101
lokesh
  • 11
  • 1
0

As it suggests there is a problem with the parsing either in Request or in Response. This error can occur if the RestTemplate client is expecting particular kind of Response from the Resource, but the Resource is returning something entirely. I got this error for a POST RestTemplate client that was related to change in the Response returned.

For e.g. Initial RestTemplate that was returning an Entity 'MyClass' changed and stared returning String so the Parser started giving error

 ResponseEntity<MyClass> postResponseEntity = restTemplate.postForEntity(postEndPoint, httpRequestEntity, MyClass.class);
 MyClass postResponseBody = postResponseEntity.getBody();

Changed to

 ResponseEntity<String> postResponseEntity = restTemplate.postForEntity(postEndPoint, httpRequestEntity, String.class);
 String postResponseBody = postResponseEntity.getBody();

As the response type changed from the entity to String the parser started giving error when processing the Response. Changed it to correct Response type (which in my case was String) and it started working.

Vivek Kumar
  • 219
  • 2
  • 5
0

What spring dependencies do you have in your pom? You may get xml parsing errors due to some spring jar files not being on the class path. In spring 3 the library was split up into many jar files. Check out this post to see what you need, specifically:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>${org.springframework.version}</version>
</dependency>
Betlista
  • 10,327
  • 13
  • 69
  • 110
krock
  • 28,904
  • 13
  • 79
  • 85
  • Thanks for the hint, but 'spring-context.jar' is already assembled within 'target/test-jar-with-dependencies.jar', i.e. the final jar contains 'org/springframework/context/config/spring-context-3.0.xsd'. By the way: we are connected with the internet via a proxy server. Is it possible, that Maven uses some system-wide proxy settings, while Java is unable to connect and check '.xsd' documents from the web? – rmv Sep 06 '10 at 10:19
  • Update: it has nothing to do with proxy issues as i verified by a test run at home directly connected to the internet. – rmv Sep 06 '10 at 12:20