spring utility project - java.io.FileNotFoundException on Ubuntu - java

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

Related

Gradle Fat Jar Plugin and Spring: Error Loading XML bean definitions from class path resource

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

My simple "Hello World" program is not running into Spring

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.

How to stop Spring #Scheduled task on Tomcat7?

I'm building Java Web Project on Servlet 3.0, Spring 3, and some other stuff.
I'm trying to use Spring's #Sheduled task.
It is working fine, but when i undeploy war from Tomcat scheduled task continues running.How to stop it on Spring Container destroy?
UPDATE
I've found what was the root of my problem - i instantiated Spring context twice. First time in web.xml as
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/servlet-context.xml
</param-value>
</context-param>
and second one in code like:
context = new ClassPathXmlApplicationContext(new String[]
{"/WEB-INF/spring/servlet- context.xml", "/WEB-INF/spring/beans.xml"});
So two instances of SheduledProcessor was created and ony one of them was destroyed. So spring destroys one it's aware about and second continues running making me to think that nothing was destroyed.
Removing ClassPathXmlApplicationContext solved my problem.
Controller.java
package a.b.c;
imports...
#Controller
#RequestMapping("/")
public class ReceiverController {
#RequestMapping(method = RequestMethod.GET)
public String welcome(ModelMap model) {
new ClassPathXmlApplicationContext("config.xml", ReceiverController.class);
return "index";
}
}
ScheduledProcessor.java
package a.b.c;
imports..
#Service
public class ScheduledProcessor {
private Logger logger = LoggerFactory.getLogger(ScheduledProcessor.class);
#Scheduled(fixedDelay = 30000)
public void process() {
logger.info("Processing task");
}
}
spring-config.xml
<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:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="a.b.c"/>
<task:annotation-driven/>
</beans>
Log file output :
03 Jul 2013 11:32:34,436 [ INFO ] (ScheduledProcessor.process:17)-> Processing task
11:33:04,439 [ INFO ] (ScheduledProcessor.process:17)-> Processing task
Jul 2013 11:33:06,913 [ INFO ] (XmlWebApplicationContext.doClose:1042)-> Closing
WebApplicationContext for namespace 'Test-servlet': startup date [Wed
Jul 03 11:32:21 EEST 2013]; root of context hierarchy 03 Jul 2013
11:33:06,914 [ INFO ]
(DefaultListableBeanFactory.destroySingletons:444)-> Destroying
singletons in
org.springframework.beans.factory.support.DefaultListableBeanFactory#1a56058:
defining beans
[scheduledProcessor,receiverController,org.springframework.context.annotation.internalConfigurationAnnotationProcessor, etc..;
root of factory hierarchy
**03 Jul 2013 11:33:34,440 INFO -> Processing task
03 Jul 2013 12:34:0,440 INFO -> Processing task

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;
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

XML error when running a spring-GWT app in development mode

I'm using eclipse and when I try to run my spring-GWT application in development mode I get the following Exception when parsing the applicationContext.xml file:
Ignored XML validation warning
org.xml.sax.SAXParseException: schema_reference.4: Failed to read schema document 'http://www.springframework.org/schema/tx/spring-tx-3.0.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.util.ErrorHandlerWrapper.warning(ErrorHandlerWrapper.java:96)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:380)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:318)
(...)
Context initialization failed
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 51 in XML document from ServletContext resource [/WEB-INF/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 'tx:annotation-driven'.
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:396)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
And my applicationContext.xml file starts like this:
<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:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
(...)
Line 51 ---> <tx:annotation-driven transaction-manager="transactionManager"/>
Does anyone know what's happening??
Check you have spring-tx dependencies (or any other spring dependency) in your class path for it to use your schema. All schemas referenced should map to a spring dependency.
xmlns:tx="http://www.springframework.org/schema/tx"
Check this:
http://www.dashaun.com/2010/03/10/springframework-applicationcontext-woes-unable-to-locate-spring-namespacehandler/
Found workaround:
When running GWT devmode, there is an issue starting the spring appcontext,
the tx...xsd cannot be found. This is related to the classloader defined
in this Devmode class of GWT, which delegates to the systemclassloader
(override of jetty)
see: http://groups.google.com/group/google-web-toolkit/browse_thread/thread/ac495ee6605d21b4
This occurs only when defining the tag in spring which is needed for
processing #Transactional annotations for transactions.
The only way to solve it I found (after 2 days) is to look in the stacktrace and find which class of Spring
invoked xerces (since it is xerces which can't find the file). This was "DefaultDocumentLoader"
This class is in spring-beans.jar.
=> So what I did is, I copied all classes from spring-tx.jar into the new spring-beans.jar
so those classes will be loaded by the same classloader
Then I also merged the META-INF/spring.handlers, spring.schemas, spring.tooling files,
All is now in a new jar i've created: spring-beans-tx-3.0.5.RELEASE.jar
This new jar is the first in the classpath of my project.
In this case it works !!

Categories