Arturas M Arturas M - 1 year ago 86
Java Question

Spring can't find configuration in path

well I'm runnning this code and it cant' find the springconfig4.xml file:

package com.nortal.pirs.test.independant;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;

public class Test4 {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("/com/nortal/pirs/beans/springconfig4.xml");
BeanFactory factory = context;
Test3 instance = (Test3) factory.getBean("Test3");;

I mean I was kind of expecting it not to work, because in Java it never works when you try to use a path that is not in your current package. However as many Spring tutorials I've seen, they all show this way of specifying the configuration file.

Now my springconfig4.xml is in my applications' src/com/nortal/pirs/beans folder. So how do I specify it so that it can be found here?

The current Test4 class is located in src/com/nortal/pirs/test/independant folder.

My stacktrace:

2012-12-09 06:16:15,734 [main] INFO - Refreshing startup date [Sun Dec 09 06:16:15 EET 2012]; root of context hierarchy
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/asm/ClassVisitor
at com.nortal.pirs.test.independant.Test4.main(
Caused by: java.lang.ClassNotFoundException: org.springframework.asm.ClassVisitor
at$ Source)
at$ Source)
at Method)
at Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 10 more

My springconfig4.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

<context:component-scan base-package="com.nortal.pirs.businesslogic.logic"></context:component-scan>
<context:component-scan base-package="com.nortal.pirs.test.independant"></context:component-scan>


Or is the problem somewhere else maybe?

Answer Source

Your stacktrace points this:

java.lang.ClassNotFoundException: org.springframework.asm.ClassVisitor

You need to download the spring-asm-3.1.2.RELEASE.jar file from the maven repo (supposing that you are using spring 3.1.2) or if you are using maven, edit your pom.xml file and add the next lines:


Also, this line:

ApplicationContext context = new ClassPathXmlApplicationContext("/com/nortal/pirs/beans/springconfig4.xml");

Has an error, the path must not start with a trailing /., so the path to the file becomes: "com/nortal/pirs/beans/springconfig4.xml".

The ClassPathResource javadoc states that it will be removed anyways:

Create a new ClassPathResource for ClassLoader usage. A leading slash will be removed, as the ClassLoader resource access methods will not accept it.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download