I'm trying to run my simple program, i added spring framework jar files and common-logging jar files too into my reference library.
Here is my first program says HelloWorld.java:
package com.rajendra.lesson01;
public class HelloWorld {
private String message;
public void setMessage(String message){
this.message=message;
}
public void getMessage(){
System.out.println("Messge: "+message);
}
}
Here is my another program says MainProgram.java:
package com.rajendra.lesson01;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainProgram {
public static void main(String[] args){
ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
HelloWorld hw=(HelloWorld) ac.getBean("helloWorld");
hw.getMessage();
}
}
and here is my last one bean.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="helloWorld" class="com.rajendra.lesson01.HelloWorld">
<property name="message" value="My name is foo.." />
</bean>
</beans>
Everything goes fine according to tutorial but my Output says:
Oct 20, 2014 9:12:19 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext#1c12fb0: startup date [Mon Oct 20 09:12:19 IST 2014]; root of context hierarchy
Oct 20, 2014 9:12:19 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [bean.xml]
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [bean.xml]; nested exception is java.io.FileNotFoundException: class path resource [bean.xml] cannot be opened because it does not exist
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:343)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:303)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:216)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:187)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:251)
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:540)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:454)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.rajendra.lesson01.MainProgram.main(MainProgram.java:9)
Caused by: java.io.FileNotFoundException: class path resource [bean.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:329)
... 13 more
What's happening here please help? and also tell me how do you guys find the errors?
Help Would be appreciated!
http://www.tutorialspoint.com/spring/spring_hello_world_example.htm
In this example you have to put bean.xml out of the "com.rajendra.lesson01" package ... put it in resource package.
I hope this can help you.
Related
After build my application with grade fat jar plugin as is configured bellow:
task fatJar(type: Jar) {
manifest {
attributes 'Implementation-Title': 'PACMAN',
'Implementation-Version': version,
'Main-Class': 'com.y.y.z.Mainclass'
}
baseName = project.name + '-all'
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}
And running this app (fat-jar with all dependencies) as follow:
-bash-3.2$ java -Dconf.properties=/app/home/wasa/sat/bin/app.properties -jar app-all-1.0.jar
Two thinks happens:
First is received the spring bootstrap start messages:
11/01/2016 11:38:59 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext#11f23e5: startup date
[Mon Jan 11 11:38:59 BRST 2016]; root of context hierarchy
11/01/2016 11:39:00 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
And then after a while thi error/exception raises as described bellow:
11/01/2016 11:42:12 org.springframework.beans.factory.xml.XmlBeanDefinitionReader warning
Ignored XML validation warning
org.xml.sax.SAXParseException: schema_reference.4: Failed to read schema document
'http://www.springframework.org/schema/beans/spring-beans.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>.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)
...
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:287)
at org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:76)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadDocument(XmlBeanDefinitionReader.java:429)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:391)
I also have tried to download the spring-beans.xsd file definition and included it in the resources folder in my project without success.
I'm load the spring xml configuration files as showed bellow:
public static void main(String[] args) throws Exception {
AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
getAllActiveUsersInPeople(context);
}
My appicationContext.xml is defined as bellow:
<?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"
xmlns:ldap="http://www.springframework.org/schema/ldap"
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 http://www.springframework.org/schema/ldap http://www.springframework.org/schema/ldap/spring-ldap.xsd">
Someone has the similar problem or a possible solution to fix this issue?
Thanks
I created a dynamic web project named HibernateSetUpTest2, jpa is marked as one of the project facets. I want to put this on a tomcat server in the future but right now I simply need to test if hibernate is working and it isn't.
I followed the MySQL DBCP Example on here (except for the jsp part):
https://tomcat.apache.org/tomcat-6.0-doc/jndi-datasource-examples-howto.html
Now my xmls look like this:
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="unitName" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>jdbc/TestDB</jta-data-source>
<class>entities.TestDataRow</class>
<properties>
<property name="hibernate.transaction.jta.platform"
value="org.hibernate.service.jta.platform.internal.SunOneJtaPlatform" />
<!-- andere properties hier definiƫren -->
<!-- geen properties definiƫren voor password, user, driver etc; want
we gebruiken datasource -->
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.connection.autocommit" value="true" />
</properties>
</persistence-unit>
</persistence>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>HibernateSetUpTest2</display-name>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/TestDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
In the project explorer of eclipse it shows a separate folder named Servers and in it is the context.xml :
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="javauser" password="javadude" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/javatest"/>
</Context>
In my src folder I wrote a Test:
package tests;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import entities.TestDataRow;
public class Hibernate {
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("unitName");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
TestDataRow t = em.find(TestDataRow.class, 1);
System.out.println(t.getFoo());
tx.commit();
em.close();
emf.close();
}
}
This uses the entity:
package entities;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
#Entity
#Table(name="testdata")
public class TestDataRow {
#Id
private int id;
private String foo;
private int bar;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFoo() {
return foo;
}
public void setFoo(String foo) {
this.foo = foo;
}
public int getBar() {
return bar;
}
public void setBar(int bar) {
this.bar = bar;
}
}
When I run the test as a java application it throws errors:
Dec 14, 2015 3:34:31 PM org.hibernate.jpa.internal.util.LogHelper logPersistenceUnitInformation
INFO: HHH000204: Processing PersistenceUnitInfo [
name: unitName
...]
Dec 14, 2015 3:34:31 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.0.5.Final}
Dec 14, 2015 3:34:31 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Dec 14, 2015 3:34:31 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Dec 14, 2015 3:34:31 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
Exception in thread "main" javax.persistence.PersistenceException: Unable to build entity manager factory
at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:66)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
at tests.Hibernate.main(Hibernate.java:12)
Caused by: org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:244)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:208)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:189)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:51)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:217)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:189)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.handleTypes(MetadataBuildingProcess.java:352)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:111)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:770)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:797)
at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:58)
... 3 more
Caused by: org.hibernate.engine.jndi.JndiException: Error parsing JNDI name [jdbc/TestDB]
at org.hibernate.engine.jndi.internal.JndiServiceImpl.parseName(JndiServiceImpl.java:124)
at org.hibernate.engine.jndi.internal.JndiServiceImpl.locate(JndiServiceImpl.java:95)
at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.configure(DatasourceConnectionProviderImpl.java:98)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:217)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:189)
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.buildJdbcConnectionAccess(JdbcEnvironmentInitiator.java:145)
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:66)
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:88)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:234)
... 14 more
Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:662)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:350)
at javax.naming.InitialContext.getNameParser(InitialContext.java:505)
at org.hibernate.engine.jndi.internal.JndiServiceImpl.parseName(JndiServiceImpl.java:118)
... 24 more
What am I doing wrong?
Your code itself looks fine (can't tell for sure but nothing seems to be seriously wrong) but you have a huge issue with your test itself:
It is a standalone test.
What the errormessage is telling you:
javax.naming.NoInitialContextException: Need to specify class name in environment or system property
Basically means, that while attempting to lookup the JNDI variable (your DB resource definition) it had nowhere to "look". There is no context in your test. That is why it failed.
The standard ways of dealing with this kind of problem are:
either programmatically creating a context
or - just for the sake of the test - inserting a DB resource definition via simple autowiring, and not using JNDI at all.
Example and source for further ideas can be found here:
Setting up JNDI Datasource for uni testing
I think you need some initialisation or its a lack of configuration
jndi.java.naming.provider.url=jnp://localhost:1099/
jndi.java.naming.factory.url=org.jboss.naming:org.jnp.interfaces
jndi.java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
I'm a newbie for Spring and I was referring to this tutorial for my first Spring project. After follow all the instruction, I try to run the program but I'm having this error.
May 26, 2015 11:42:45 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext#31221be2: startup date [Tue May 26 11:42:45 CST 2015]; root of context hierarchy
May 26, 2015 11:42:45 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [Beans.xml]
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/expression/ParserContext
at org.springframework.context.support.AbstractApplicationContext.prepareBeanFactory(AbstractApplicationContext.java:553)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:455)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.tutorialspoint.MainApp.main(MainApp.java:9)
Caused by: java.lang.ClassNotFoundException: org.springframework.expression.ParserContext
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 5 more
Any idea where I did the wrong. As I know I was following the tutorial correctly.
CODE
package com.tutorialspoint;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("Beans.xml");
HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
obj.getMessage();
}
}
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="helloWorld" class="com.tutorialspoint.HelloWorld">
<property name="message" value="Hello World!"/>
</bean>
</beans>
Check if you have added all the required jars in your classpath.
The class org.springframework.expression.ParserContext lies inside spring-expresssion-YOUR_SPRING_VERSION.jar Check if you have this jar added on classpath.
I was having the same issue. But I found my mistake.
My mistake was, I created "Beans.xml" inside com.tutorialspont package.
The solution for me was "Beans.xml" has to be created directly under src folder.
log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/expression/PropertyAccessor
at org.springframework.context.support.AbstractApplicationContext.prepareBeanFactory(AbstractApplicationContext.java:553)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:454)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:83)
at com.javahonk.client.TestWebService.main(TestWebService.java:19)
Caused by: java.lang.ClassNotFoundException: org.springframework.expression.PropertyAccessor
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 5 more
Solution to the above problem:This happens if spring-expression-xxx.Release.jar in not available in your class path. Please add spring-expression-3.2.3.RELEASE or latest version jar in your class path to fix this issue.
I'm newly at Spring.
I tried to write easy project and stack with weird exception FileNotFoundException.
To my mind it is some problem with loading app-context.xml. But I sure that set correct absolute path.
Any suggestions?
Main code:
package com.prospring.ch2;
public class HelloWorldSpringDI {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"/home/nazar_art/workspace/ch2/src/main/resources/META-INF/spring/app-context.xml");
MessageRenderer mr = ctx.getBean("renderer", MessageRenderer.class);
mr.render();
}
}
Here content of app-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:p="http://www.springframework.org/schema/p"
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 id="provider" class="com.prospring.ch2.HelloWorldMessageProvider" />
<bean id="rendere" class="com.prospring.ch2.StandartOutMessageRenderer"
p:messageProvider-ref="provider" />
<!--<description>Example configuration to get you started.</description -->
<!-- context:component-scan base-package="com.prospring.ch2" /> -->
</beans>
snippet of console message:
14:24:06,360 INFO t.support.ClassPathXmlApplicationContext: 456 - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext#3f62e847: startup date [Wed Nov 13 14:24:06 EET 2013]; root of context hierarchy
14:24:06,509 INFO eans.factory.xml.XmlBeanDefinitionReader: 315 - Loading XML bean definitions from class path resource [home/nazar_art/workspace/ch2/src/main/resources/META-INF/spring/app-context.xml]
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [home/nazar_art/workspace/ch2/src/main/resources/META-INF/spring/app-context.xml]; nested exception is java.io.FileNotFoundException: class path resource [home/nazar_art/workspace/ch2/src/main/resources/META-INF/spring/app-context.xml] cannot be opened because it does not exist
I checked path to app-context.xml from terminal all looks ok:
nazar_art#nazar-desktop:~/workspace/ch2/src/main/resources/META-INF/spring$ pwd
/home/nazar_art/workspace/ch2/src/main/resources/META-INF/spring
nazar_art#nazar-desktop:~/workspace/ch2/src/main/resources/META-INF/spring$ ls -lg
total 4
-rw-rw-r-- 1 nazar_art 867 Nov 13 14:25 app-context.xml
Here is looking of my struckture:
How to solve this trouble?
You are using the class path application context loader ClassPathXmlApplicationContext and passing an absolute path to it.
You need to use FileSystemXmlApplicationContext
If you want to use ClassPathXmlApplicationContext then you have to make sure your app-context.xml is in the class path of your application and pass the appropriate resource name, which is different from the absolute path on a filesystem.
Use either
public static void main(String[] args) {
ApplicationContext ctx = new FileSystemXmlApplicationContext(
"/home/nazar_art/workspace/ch2/src/main/resources/META-INF/spring/app-context.xml");
...
}
or
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"META-INF/spring/app-context.xml");
...
}
FileSystemXmlApplicationContext
ClassPathXmlApplicationContext
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;
import org.springframework.context.support.ClassPathXmlApplicationContext;
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");
instance.run();
}
}
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 org.springframework.context.support.AbstractApplicationContext - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext#b24044e: 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 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.<init>(AbstractAutowireCapableBeanFactory.java:121)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.<init>(AbstractAutowireCapableBeanFactory.java:168)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.<init>(DefaultListableBeanFactory.java:167)
at org.springframework.context.support.AbstractRefreshableApplicationContext.createBeanFactory(AbstractRefreshableApplicationContext.java:195)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:128)
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.nortal.pirs.test.independant.Test4.main(Test4.java:9)
Caused by: java.lang.ClassNotFoundException: org.springframework.asm.ClassVisitor
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 10 more
My springconfig4.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:aop="http://www.springframework.org/schema/aop"
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/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<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>
</beans>
Or is the problem somewhere else maybe?
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:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-asm</artifactId>
<version>3.1.2.RELEASE</version>
</dependency>
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.
Exception says it is not finding a class, not Spring context xml
Caused by: java.lang.ClassNotFoundException: org.springframework.asm.ClassVisitor
please add asm dependency
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.asm</artifactId>
<version>3.1.0.RELEASE</version>
</dependency>
Add spring-asm-3.0.1.RELEASE.jar and spring-expression-3.0.1.RELEASE.jar.
java.lang.ClassNotFoundException: org.springframework.asm.ClassVisitor
Just add the spring-asm-3.1.2.RELEASE.jar file. Place it in your lib folder.
For the above problem as mentioned above these jars are missing namely 1.org.springframework.asm-3.0.1.RELEASE-A.jar
2.org.springframework.expression-3.0.1.RELEASE-A.jar
or
Add all the core Jars of Spring so that any new programmer can Run all basic level programs.
There is no need of below line in your program. insted of that you can use context insted of factory in the above program.
BeanFactory factory = context
We usually use ApplicationContext insted of BeanFactory.
Even if you use BeanFactory there will be no effect for the program.
Add the Jars which is shown in the Screen short and it will work