Tuesday, April 30, 2013

Install Sun Java and set up JAVA_HOME on Ubuntu

First of all, go to Sun/Oracle Java official page: http://www.oracle.com/technetwork/java/javase/downloads/index.html.
Find the version you want to install and download it. For me, I download  jdk-7u21-linux-x64.tar.gz and uncompress it to /usr/lib/jvm. So my JDK installation home directory is /usr/lib/jvm/jdk1.7.0_21.

Set up the environment:
$sudo gedit /etc/environment    and add the following content to the end of the file.                                                                                                         


$source /etc/environment

Set Sun JDK as default
$sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk1.7.0_21/bin/java 300
$sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk1.7.0_21/bin/javac 300
$sudo update-alternatives --config java
Select the right number.

Verify it: 
$java -version
$javac -version

Sometimes we need to run jps command (list all daemons to verify the hadoop setup) 
install it: sudo update-alternatives --install /usr/bin/jps jps $JAVA_HOME/bin/jps 1

Then you can run $jps rather than $JAVA_HOME/bin/jps


Sunday, April 28, 2013

[Resolved] Could not create a new instance of class [HibernateGrailsPlugin]!

After I upgraded my Grails app from 1.3.7 to 2.1.0, I can compile it, run app in development mode successfully, But failed to deploy it in tomcat.

I get the following error when I put war file in tomcat and start it:

Could not create a new instance of class [HibernateGrailsPlugin]!
Caused by: groovy.lang.MissingPropertyException: No such property: onChange for class: org.codehaus.groovy.grails.plugins.orm.hibernate.H ibernatePluginSupport

I was struggling with this issue for almost two days, and finally resolve it.

In Grails 1.3.7, org.codehaus.groovy.grails.plugins.orm.hibernate.H ibernatePluginSupport is included in grails-gorm plugin, and you can find that the latest version of gorm package is 1.3.7.

There is no any newer version for this plugin, because in later version of Grails, this class is not included in grails-gorm.jar, but in grails-hibernate-2.x.jar.

So the solution cloud be very simple, I should find the problem maker (grails-gorm 1.3.7 jar) and remove it in my dependency. 

I find that in my app, the other engineers (who created this project one year ago) use both maven and grails to manage dependencies and build project, and there is dependency entry for gorm 1.3.7 in pom file. Then, life is easy, just clean up pom file and recompile it. 


Please do not use both maven and grails to manage dependencies and build project in the same time.

Saturday, April 27, 2013

Ubuntu 12.04: cannot login after enter correct password, the system returns back you login screen

After I upgraded my ubuntu from 11.10 to 12.04 for few days, I cannot login even I entered the correct password. It always returned back to login screen.

After spent 30 minutes research on google, and found what is the possible issue and the solution which works for me.

Issue: lightdm might be the problem

Solution: change to use gdm

  1. Go to recovery mode.
  2. enable network.
  3. re mount / by the using this command: #mount -o rw,remount /
  4. remove the file in your home directory: rm /home/userName/.Xauthority*
  5. install gdm: apt-get install gdm.
  6. change to use gdm as the default login manager: dpkg-reconfigure gdm
  7. reboot
This solution works for me and I can login use my account. But the problem backs when I change back to use lightdm as default. So I have to go with gdm from now on to login successfully. 

Friday, April 26, 2013

Recommended scope for grails plugins

Recommended scope for grails plugins

  • build scope: for plugins that are available to the build system only.
  • test scope: for things that should be available to tests but not to application code
  • runtime scope: for plugins (or JAR dependencies) available to the application at runtime but not at compile time.
  • compile scope: available to the application at compile time.
  • provided scope: available at development time but not included in the WAR file.
If in doubt, compile will definitely work, as plugins in application.properties are treated as if they were declared in compile scope.

The best way to get the plugin scope, search plugin on http://grails.org/plugin and click on the search result, then find the recommend scope. 

e.g build ":tomcat:7.0.39" http://grails.org/plugin/tomcat
Do not use $grails install-plugin to install plugins, due to it will add a line to application.properties and by default the scope is compile.

The property way to add dependencies/install plugins is:
  1. Go to the Grails plugin page and find the right version for your Grails version.
  2. Copy something like: compile ":build-test-data:2.0.5" to your BuildConfig.groovy

Wednesday, April 24, 2013

Grails upgrading from 1.3.7 to 2.1.0

I am working on a Grails project and it was with a very old version 1.3.7, I need to integrate AAF as identity provider to my application. AAF officially provides a grails plugin, however it works only with Grails 2.x. So what I have to do first is upgrading my Grails Application from 1.3.7 to 2.1.0.

I am not expert in Grails and just started to work on Grails project early this month. After spent almost one day on it, finally my application is with Grails 2.1.0 now.

Here, I write down a transcript of my upgrade session, and hope it will be helpful to the people like me who are still a newbie to Grails.

Before doing upgrade, I read the offical upgrade reference doc from:

2013-04-24 10:30 am - Starting Grails 1.3.7 to 2.1.0 upgrade.

# grails upgrade

 Error Failed to resolve dependencies (Set log level to 'warn' in BuildConfig.groovy for more information):

- org.grails.plugins:mail:1.0-SNAPSHOT
- org.grails.plugins:tomcat:1.3.7
- org.grails.plugins:hibernate:1.3.7

This is because I have not manually changed the plugins's version to latest/greatest  (plugins version config can be found in BuildConfig.groovy OR application.properties) . To get the greatest version of plugins for Grails 2.1.0, just use command:

# grails plugin-info pluginName

Then manually update BuildConfig.groovy by looking at a fresh 2.1.0 application.

# grails upgrade

| Error SLF4J: Class path contains multiple SLF4J bindings.
| Error SLF4J: Found binding in [jar:file:/home/htxiong/.grails/ivy-cache/org.grails/grails-plugin-log4j/jars/grails-plugin-log4j-2.1.0.jar!/org/slf4j/impl/StaticLoggerBinder.class]
| Error SLF4J: Found binding in [jar:file:/home/htxiong/.grails/ivy-cache/org.slf4j/slf4j-log4j12/jars/slf4j-log4j12-1.5.8.jar!/org/slf4j/impl/StaticLoggerBinder.class]
| Error SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.

 Project upgraded

Please just ignore the SLF4J error, it wont stop you to compile and run your app. I spent almost half of my time on trying to resolve this error and eventually found that it can be ignored.

# grails clean

  Application cleaned.

# grails compile

hint: If your compile failed because "can not resolove type ........" e.g. can not resolve org.tmatesoft.svn.core.SVNException. do not try to resolve the dependency in pom.xml, but just find the svn Grails plugin and install it.  

# grails test-app

Cannot create JDBC driver of class 'org.h2.Driver' for connect URL 'jdbc:hsqldb:mem:testDb'
java.sql.SQLException: No suitable driver

I have to modify DataSource.groovy and chagne HSQLDB to H2. 

test {
        dataSource {
pooled = true
driverClassName = "org.h2.Driver"
username = "sa"
password = ""
dbCreate = "update"
url = "jdbc:h2:mem:testDb;MVCC=TRUE;LOCK_TIMEOUT=10000"

# grails test-app


# grails run-app

Error executing bootstraps: groovy.lang.GroovyRuntimeException: Could not find matching constructor for: org.codehaus.groovy.grails.web.converters.marshaller.json.DomainClassMarshaller(java.lang.Boolean)
org.codehaus.groovy.grails.web.converters.exceptions.ConverterException: groovy.lang.GroovyRuntimeException: Could not find matching constructor for: org.codehaus.groovy.grails.web.converters.marshaller.json.DomainClassMarshaller(java.lang.Boolean)
at grails.converters.JSON.createNamedConfig(JSON.java:468)
at au.org.emii.portal.config.JsonMarshallingRegistrar._registerSnapshotLayer(JsonMarshallingRegistrar.groovy:88)
at au.org.emii.portal.config.JsonMarshallingRegistrar.registerJsonMarshallers(JsonMarshallingRegistrar.groovy:27)
at au.org.emii.portal.bootstrap.MenuBootStrap$_closure1.doCall(MenuBootStrap.groovy:18)
at grails.util.Environment.evaluateEnvironmentSpecificBlock(Environment.java:301)
at grails.util.Environment.executeForEnvironment(Environment.java:294)
at grails.util.Environment.executeForCurrentEnvironment(Environment.java:270)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:722)
Caused by: groovy.lang.GroovyRuntimeException: Could not find matching constructor for: org.codehaus.groovy.grails.web.converters.marshaller.json.DomainClassMarshaller(java.lang.Boolean)
at au.org.emii.portal.display.SnapshotLayerJsonMarshaller.<init>(SnapshotLayerJsonMarshaller.groovy:43)
at au.org.emii.portal.config.JsonMarshallingRegistrar$__registerSnapshotLayer_closure3.doCall(JsonMarshallingRegistrar.groovy:89)
at grails.converters.JSON.createNamedConfig(JSON.java:464)

Ahh, grails 2.1.0 provides a new version of class "DomainClassMarshaller" and the constructor takes one more parameter 'GrailsApplication'. 
So, to resolve it, just find its subclasses and put grailsApplication as the 2nd parameter in super() .

        SnapshotLayerJsonMarshaller() {
super(false, grailsApplication)

# grails run-app


2013-04-24 40:10 pm - Grails upgraded.

I got dependency not found & plugin failed to install errors many times, and sometimes Grails even tell you failed to resolve some dependencies which are included in Grails installation. 

[ERROR] Failed to execute goal on project grails-project: Could not resolve dependencies for project grails-project:grails-project:war:4.0-SNAPSHOT
: Failed to collect dependencies for [org.grails:grails-crud:jar:2.1.0 (compile), org.grails:grails-core:jar:2.1.0 (compile), org.grails:grails-spring:jar:2.1.0
(compile), org.grails:grails-test:jar:2.1.0 (compile), org.grails:grails-web:jar:2.1.0 (compile), org.slf4j:slf4j-api:jar:1.5.8 (compile), org.slf4j:slf4j-log4
j12:jar:1.5.8 (compile), log4j:log4j:jar:1.2.14 (provided), junit:junit:jar:4.5 (test), commons-lang:commons-lang:jar:2.4 (compile)]: Failed to read artifact de
scriptor for org.springframework.uaa:org.springframework.uaa.cl ient:jar:1.0.1.RELEASE: Could not transfer artifact org.springframework.uaa:org.springframework.uaa

I do not why Grails return such error, but there is a way to walkaround. 

delete the directory ivy-cache in your Grails folder (linux, ~/.grails/ivy-cache) and re-compile your app.
./grails folder works similar with .m2 folder for maven, it contains all the dependency jars for your Grails application. Empty it and re run grails compile will download all the dependencies from internet for your application.

Another example to do Grails upgrading can not found on http://www.technipelago.se/content/technipelago/blog/grails-upgrade-1.3.7-to-2.0.1 which from by another developer. 

Monday, April 22, 2013

Fix Error: Unsupported major.minor version 51.0

Unsupported major.minor version 51.0
This is a error caused by the jdk you are using to run/debug your app (for both java apps and grails apps).
You should check the version of jdk you are using.

The version number shown describe which version of Java was used to compile the code.
The reported major numbers are:
J2SE 7 = 51,
J2SE 6.0 = 50,
J2SE 5.0 = 49,
JDK 1.4 = 48,
JDK 1.3 = 47,
JDK 1.2 = 46,
JDK 1.1 = 45
To fix the actual problem you should try to either run the Java code with newer version Java JRE or specify target parameter to the Java compiler to instruct the compiler to create code compatible with earlier Java versions.

So if you get error Unsupported major.minor version 51.0, you may can fix it by changing the jdk version to 1.7XXX.