Monday, June 25, 2012

Java script code to find the difference between 2 dates in terms of months

Its easy to find the month difference in Java since java supports calendar objects.
But java script supports Date objects but not calendar objects.
Here is a code for finding the month difference in javascript.

function getMonthsDifference(firstDate,lastDate)
{
 var date1DtParse = Date.parse(lastDate);
 var date2DtParse = Date.parse(firstDate);
 var date1 = new Date(date1DtParse);         
 var date2 = new Date(date2DtParse);

 var diffYears = date2.getFullYear()-date1.getFullYear();
 var diffMonths = date2.getMonth()-date1.getMonth();
 var diffDays = date2.getDate()-date1.getDate(); 
 var months = (diffYears*12 + diffMonths);
 if(diffDays>0)
 {     months += '.'+diffDays; }
 else if(diffDays<0)
 {     months--;    
 months += '.'+(new Date(date2.getFullYear(),date2.getMonth(),0).getDate()+diffDays);
 }
 return months;
}

Thursday, May 24, 2012

Resolve error "org.springframework.beans.factory.BeanCreationException: Error creating bean with name Hello': Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: Method must not be null"

I think every Spring beginner will face this issue.Have no clues,Code seems like perfect.Tried all possible ways to resolve it? 

see below!

Reason: Its because of using different versions of the spring jars in the project.

Resolution:
 Clean the libraries/jars added to the project.Remove all the libraries from the class path.Then add the libraries into the project from the same version of Spring.
You can find the libraries in the Spring website  

Make sure that all the libraries added in the project are from the same spring version/release.

Stacktrace:
Nested in org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#0': Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: Method must not be null:
java.lang.IllegalArgumentException: Method must not be null
    at org.springframework.util.Assert.notNull(Assert.java:112)
    at org.springframework.core.BridgeMethodResolver.findBridgedMethod(BridgeMethodResolver.java:63)
    at org.springframework.beans.GenericTypeAwarePropertyDescriptor.<init>(GenericTypeAwarePropertyDescriptor.java:58)
    at org.springframework.beans.CachedIntrospectionResults.<init>(CachedIntrospectionResults.java:250)
    at org.springframework.beans.CachedIntrospectionResults.forClass(CachedIntrospectionResults.java:144)
    at org.springframework.beans.BeanWrapperImpl.getCachedIntrospectionResults(BeanWrapperImpl.java:252)
    at org.springframework.beans.BeanWrapperImpl.getPropertyDescriptorInternal(BeanWrapperImpl.java:282)
    at org.springframework.beans.BeanWrapperImpl.isWritableProperty(BeanWrapperImpl.java:333)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1247)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1010)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:515)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:362)
    at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:255)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:199)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:45)
   

Resolve Error "org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [Beans.xml]"

Spring projects will compile without any warnings and errors.But when you run the code you will see the exception.

 Reason:  It is the issue with the location of the Beans.xml.So server could not find the  location of the Spring beans xml.
Stacktrace :org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [Beans.xml]
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [Beans.xml]; nested exception is java.io.FileNotFoundException: class path resource [Beans.xml] cannot be opened because it does not exist
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:349)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:310)
    at org.springframework.beans.factory.xml.XmlBeanFactory.<init>(XmlBeanFactory.java:73)
    at org.springframework.beans.factory.xml.XmlBeanFactory.<init>(XmlBeanFactory.java:61)
    at com.mySpringExample.helloExample.HelloClient.main(HelloClient.java:20)
Caused by: java.io.FileNotFoundException: class path resource [Beans.xml] cannot be opened because it does not exist
    at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:143)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:336)
    ... 4 more

Resolution :  
1) Move the spring Beans.xml to the src folder from the current folder. 
Clean and run the project.
See the screen shots below.

Wrong location(Example)

















Right location  

Resolve Error "Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory" in Spring

Spring projects compiles without any errors and warnings but when the code is run it will through the exception.You will have no clues on what is wrong with the code and project.

Stack trace:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
    at org.springframework.core.CollectionFactory.<clinit>(CollectionFactory.java:64)
    at org.springframework.core.SimpleAliasRegistry.<init>(SimpleAliasRegistry.java:41)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.<init>(DefaultSingletonBeanRegistry.java:73)
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.<init>(FactoryBeanRegistrySupport.java:41)
    at org.springframework.beans.factory.support.AbstractBeanFactory.<init>(AbstractBeanFactory.java:146)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.<init>(AbstractAutowireCapableBeanFactory.java:144)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.<init>(AbstractAutowireCapableBeanFactory.java:155)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.<init>(DefaultListableBeanFactory.java:121)
    at org.springframework.beans.factory.xml.XmlBeanFactory.<init>(XmlBeanFactory.java:72)
    at org.springframework.beans.factory.xml.XmlBeanFactory.<init>(XmlBeanFactory.java:61)
    at com.mySpringExample.helloExample.HelloClient.main(HelloClient.java:20)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
    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)
    ... 11 more
 
Issue: The Spring project does not have a required Jars in the build path.

Resolution : 
Add the library commons-logging.jar to the build path of the project.Then clean and build the project.

Saturday, May 19, 2012

Code for displaying image in Phonegap mobile application from server

Code for displaying image in Phonegap mobile application from server

I had a requirement to display the image in phonegap mobile application from Web server using JSON.
I could not find any solution over the net. My solution is below.

1) Add an images folder under Webcontent folder and add a jpeg image in it.
(Added the image picture5.jpg in the folder)
      2) Mention the Exact location of the image in the JSON String just like string or int value in the server   side code.
        EX: productMap.put("LOCAT", "http://192.178.3.2:9080/SalesManager/images/picture5.jpg");

      3) Create a new Javascript file picShow.js to get the image from the server using JSON.
            function getPicture()
            {
            $
            .getJSON("http://192.178.3.2:9080/SalesManager/getImage.action",
            {}, function(data) {
            processPictureData(data);
            });
            }
            function processPictureData(data)
            {
            $.each(data.PICTURE, function(i, product) {
            $('#productPic').attr('src', product.LOCAT);
            });
            }

            4) Create a new html to display the picture.
            <!DOCTYPE HTML>
            <html>
            <head>
            <title>Picture POC</title>
            <meta name="viewport" content="width=device-width,initial-scale=1" />
            <script src="scripts/phonegap-1.0.0rc2.js"></script>
            <script src="scripts/jquery-1.6.4.min.js"></script>
            <script src="scripts/jquery.mobile.min.js"></script>
            <script src="scripts/database.js"></script>
            <script src="scripts/synchronizeData.js"></script>
            <link rel="stylesheet" href="css/jquery.mobile.min.css" />
            <link rel="stylesheet" href="css/styles.css" />
            <script src="scripts/picShow.js"></script>
            </head>
            <body>
            <h1>Picture Demo</h1>
            <button onclick="getPicture()">click here</button>
            <div data-role="">
            <img id="productPic">
            </div>
            </body>
            </html>















Learn Spring by Examples: How to add Spring IDE to the Eclipse?

Adding the Spring IDE to the eclipse is not  compulsory. However it will ease out the spring coding.
All you need to do is adding spring related Jars to the workspace.
I assume everyone have the Eclipse workspace ready for adding the Spring IDE.
If not Download the eclipse from the website http://www.eclipse.org/downloads/
In this post I am going to tell you on how to add Spring IDE to the Eclipse.
Step 1:  Open the Eclipse workspace and click on help -> Install New Software























Step 2:  Click on Add button on the window opened.



















Step 3:  Add a site Enter some name and enter location http://springide.org/updatesite and click on OK (Make sure that you are connected to internet before this step)
Select the necessary softwares.







Step 4:  You are done!! Wait till the completion of downloading process and Reopen the workspace to see the added IDE.
Step 5:  Once you reopen the workspace you should be able to create the spring project.
Right click on project explorer -> click on New -> click on Other and select the Spring project.
I will cover all the modules of the Spring framework with working code examples…in my next blogs.

Sunday, March 18, 2012

Problems with a href tag and window.location in phonegap application

I was using <a href> tag and window.location for links in the pages.But it was displaying the old page itself. 
PhoneGap gets notified whenever a new page is to be loaded.  However, there is a problem in Phonegap (phonegap-1.0.0rc2 and older versions) in using a <a href> tag or window.location.
As a result, PhoneGap doesn't have any way to intercept the request.So it continues to display old page or it will not be able to handle the parameters passed from previous page.
Code with problems:
<a href="HomePage.html">Home</a>
and
window.location="HomePage.html";
Solution: 
1) Use navigator.app.loadUrl(file:///android_asset/HomePage.html); instead of 
window.location or <a href tag.
2) Try to make use of same page with sections (<div> tag) than having code written
over multiple page.

Monday, January 9, 2012

"Project has no default.properties file! Edit the project properties to set one."error on adding existing phonegap project into workspace

I was facing the this issue  when i tried to add an existing Phonegap project into another workspace.
Reason:
Android Tools automatically generate the file default.properties which tells about target API level.
But when the project is added into the another workspace this file does not gets created.
Resolution:
Create a properties file default.properties directly under the Phonegap project directory.
Mention the target API level in it.
(ex: If the target API that you want to use is 8,then add the following code in the default.properties file)
 target=android-8

Tuesday, January 3, 2012

How to resolve Conversion to Dalvik format failed with error 1 in Phonegap project?



Project shows this error because the phonegap jar (phonegap-1.0.0rc2.jar) /any other jar  file is not stored in the proper folder in the project setup.
I have created a Lib folder under src folder and placed the phonegap  jar into it and added the jar to buildpath which resulted in the error.


Resolution:
1) Remove the Phonegap jar/any other jar from the build path.
In the eclipse select project -> Properties ->Java Build Path.Select the tab libraries.Then select the phonegap jar and remove it.
2) Add the lib folder directly under the project(i.e not under any folders in project) and
 place the phonegap jar under it. Add the phonegap jar  to build path.
Then Clean and build the project.



Sample Exception stracktrace
[2012-01-03 18:57:54 - MedicalSalesManApplication] Dx
UNEXPECTED TOP-LEVEL EXCEPTION:
java.lang.IllegalArgumentException: already added: Lcom/phonegap/AccelListener;
[2012-01-03 18:57:54 - MedicalSalesManApplication] Dx at com.android.dx.dex.file.ClassDefsSection.add(ClassDefsSection.java:123)
[2012-01-03 18:57:54 - MedicalSalesManApplication] Dx at com.android.dx.dex.file.DexFile.add(DexFile.java:163)
[2012-01-03 18:57:54 - MedicalSalesManApplication] Dx at com.android.dx.command.dexer.Main.processClass(Main.java:486)
[2012-01-03 18:57:54 - MedicalSalesManApplication] Dx at com.android.dx.command.dexer.Main.processFileBytes(Main.java:455)
[2012-01-03 18:57:54 - MedicalSalesManApplication] Dx at com.android.dx.command.dexer.Main.access$400(Main.java:67)
[2012-01-03 18:57:54 - MedicalSalesManApplication] Dx at com.android.dx.command.dexer.Main$1.processFileBytes(Main.java:394)
[2012-01-03 18:57:54 - MedicalSalesManApplication] Dx at com.android.dx.cf.direct.ClassPathOpener.processArchive(ClassPathOpener.java:245)
[2012-01-03 18:57:54 - MedicalSalesManApplication] Dx at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:131)
[2012-01-03 18:57:54 - MedicalSalesManApplication] Dx at com.android.dx.cf.direct.ClassPathOpener.process(ClassPathOpener.java:109)
[2012-01-03 18:57:54 - MedicalSalesManApplication] Dx at com.android.dx.command.dexer.Main.processOne(Main.java:418)
[2012-01-03 18:57:54 - MedicalSalesManApplication] Dx at com.android.dx.command.dexer.Main.processAllFiles(Main.java:329)
[2012-01-03 18:57:54 - MedicalSalesManApplication] Dx at com.android.dx.command.dexer.Main.run(Main.java:206)
[2012-01-03 18:57:54 - MedicalSalesManApplication] Dx at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[2012-01-03 18:57:54 - MedicalSalesManApplication] Dx at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
[2012-01-03 18:57:54 - MedicalSalesManApplication] Dx at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
[2012-01-03 18:57:54 - MedicalSalesManApplication] Dx at java.lang.reflect.Method.invoke(Unknown Source)
[2012-01-03 18:57:54 - MedicalSalesManApplication] Dx at com.android.ide.eclipse.adt.internal.build.DexWrapper.run(DexWrapper.java:179)
[2012-01-03 18:57:54 - MedicalSalesManApplication] Dx at com.android.ide.eclipse.adt.internal.build.BuildHelper.executeDx(BuildHelper.java:585)
[2012-01-03 18:57:54 - MedicalSalesManApplication] Dx at com.android.ide.eclipse.adt.internal.build.builders.PostCompilerBuilder.build(PostCompilerBuilder.java:490)
[2012-01-03 18:57:54 - MedicalSalesManApplication] Dx at org.eclipse.core.internal.events.BuildManager$2.run(BuildManager.java:728)
[2012-01-03 18:57:54 - MedicalSalesManApplication] Dx at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
[2012-01-03 18:57:54 - MedicalSalesManApplication] Dx at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:199)
[2012-01-03 18:57:54 - MedicalSalesManApplication] Dx at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:239)
[2012-01-03 18:57:54 - MedicalSalesManApplication] Dx at org.eclipse.core.internal.events.BuildManager$1.run(BuildManager.java:292)
[2012-01-03 18:57:54 - MedicalSalesManApplication] Dx at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
[2012-01-03 18:57:54 - MedicalSalesManApplication] Dx at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:295)
[2012-01-03 18:57:54 - MedicalSalesManApplication] Dx at org.eclipse.core.internal.events.BuildManager.basicBuildLoop(BuildManager.java:351)
[2012-01-03 18:57:54 - MedicalSalesManApplication] Dx at org.eclipse.core.internal.events.BuildManager.build(BuildManager.java:374)
[2012-01-03 18:57:54 - MedicalSalesManApplication] Dx at org.eclipse.core.internal.events.AutoBuildJob.doBuild(AutoBuildJob.java:143)
[2012-01-03 18:57:54 - MedicalSalesManApplication] Dx at org.eclipse.core.internal.events.AutoBuildJob.run(AutoBuildJob.java:241)
[2012-01-03 18:57:54 - MedicalSalesManApplication] Dx at org.eclipse.core.internal.jobs.Worker.run(Worker.java:54)
[2012-01-03 18:57:54 - MedicalSalesManApplication] Dx 1 error; aborting
[2012-01-03 18:57:54 - MedicalSalesManApplication] Conversion to Dalvik format failed with error 1
[2012-01-03 18:58:45 - MedicalSalesManApplication] Dx

Resolve "Target runtime Apache Tomcat v6.0 is not defined." error

Go to window ->preferences ->Server->Runtime environments . add server Apache Tomcat v6.0 Mention the directory where apache-toma...