Wednesday 26 November 2014

java.lang.VerifyError: Bad method call from inside of a branch

Ever encountered this kind of an error with no pages opening up in your cq instance?
Exception in thread "main" java.lang.VerifyError: Bad <init> method call from in
side of a branch
Exception Details:
  Location:
    com/myproject/server/MockWriter.<init>(Ljava/lang/S
tring;)V @71: invokespecial
  Reason:
    Error exists in the bytecode
  Bytecode:
    0000000: b800 164d 04bd 0018 5903 2b53 5910 ff12
    0000010: 04b8 001e 2a5f ab00 0000 0055 0000 0003
    0000020: aad3 b1ff 0000 0022 0000 9b75 0000 0037
    0000030: 60fa 8f17 0000 0040 5f5a 5903 3212 20b8
    0000040: 0024 c000 205f 57b7 0026 a700 2b5f 5a57
    0000050: b700 29a7 0022 5f5a 5903 3212 2bb8 0024
    0000060: c000 2b5f 57b7 002e a700 0dbb 0030 5912
    0000070: 32b7 0033 bf57 2ab6 0037 4e2d 2a5f b500
    0000080: 392d 57b1
  Stackmap Table:
    full_frame(@56,{UninitializedThis,Object[#32],Object[#63]},{Object[#65],Unin
itializedThis})
    full_frame(@77,{UninitializedThis,Object[#32],Object[#63]},{Object[#65],Unin
itializedThis})
    full_frame(@86,{UninitializedThis,Object[#32],Object[#63]},{Object[#65],Unin
itializedThis})
    full_frame(@107,{UninitializedThis,Object[#32],Object[#63]},{Object[#65],Uni
nitializedThis})
    full_frame(@117,{Object[#2],Object[#32],Object[#63]},{Object[#65]})

        at java.lang.Class.getDeclaredConstructors0(Native Method)
        at java.lang.Class.privateGetDeclaredConstructors(Unknown Source)
        at java.lang.Class.getDeclaredConstructors(Unknown Source)
        at org.codehaus.groovy.reflection.CachedClass$2$1.run(CachedClass.java:6
9)
Well, this happens to be a problem with the JVM version.
Known versions with this problem:

1.7.0 update 55
1.7.0 update 65
1.7.0 update 67
1.8.0 update 11

Solution:
Edit the start.bat and add JVM argument "-noverify" to have the code working.

The default JVM options look like this:
::* default JVM options
if not defined CQ_JVM_OPTS set CQ_JVM_OPTS=-Xmx2048m -XX:MaxPermSize=512M -noverify -Djava.awt.headless=true

Hope this helped!

Upgrading to Httpcomponents 4.3.5?

Did you encounter the following error after upgrading to httpcomponents 4.3.5?
java.lang.ClassNotFoundException: javax.naming.InvalidNameException not found by org.apache.httpcomponents.httpclient [923]
at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1500)
at org.apache.felix.framework.BundleWiringImpl.access$400(BundleWiringImpl.java:75)
at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:1923)
at java.lang.ClassLoader.loadClass(Unknown Source)

This happens because Httpclient OSGI bundle 4.3.5 doesnt import the package "javax.naming" by default.
The solution would be to edit the manifest.mf file of the bundle to include the javax.naming package.

The Import-Package section in MANIFEST.MF should look like with the only change is to include javax.naming:
Import-Package: javax.crypto,javax.crypto.spec,javax.naming,javax.net,javax.net.ssl,j
 avax.security.auth.x500,org.ietf.jgss,org.osgi.framework;version="[1.5,
 2)",org.osgi.service.cm;version="[1.3,2)",org.apache.commons.logging;ve
 rsion="[1.1.0,1.2.0)",net.sf.ehcache;resolution:=optional,net.spy.memca
 ched;resolution:=optional,org.apache.http.util;version="[4.3.0,4.4.0)",
 org.apache.http.io;version="[4.3.0,4.4.0)",org.apache.http.pool;version
 ="[4.3.0,4.4.0)",org.apache.http.impl.pool;version="[4.3.0,4.4.0)",org.
 apache.http.impl;version="[4.3.0,4.4.0)",org.apache.http.message;versio
 n="[4.3.0,4.4.0)",org.apache.http.impl.entity;version="[4.3.0,4.4.0)",o
 rg.apache.http.params;version="[4.3.0,4.4.0)",org.apache.http.impl.io;v
 ersion="[4.3.0,4.4.0)",org.apache.http;version="[4.3.0,4.4.0)",org.apac
 he.http.concurrent;version="[4.3.0,4.4.0)",org.apache.http.entity;versi
 on="[4.3.0,4.4.0)",org.apache.http.config;version="[4.3.0,4.4.0)",org.a
 pache.http.protocol;version="[4.3.0,4.4.0)"

Having done this,  it is important to note that httpclients imports more than just javax.naming.
The class org.apache.http.conn.ssl.AbstractVerifier.java imports the following javax.naming classes:

javax.naming.InvalidNameException
javax.naming.NamingException
javax.naming.directory.Attribute
javax.naming.directory.Attributes
javax.naming.ldap.LdapName
javax.naming.ldap.Rdn

If not resolved, such kind of errors may be encountered:
java.lang.NoClassDefFoundError: javax/naming/ldap/LdapName
    at org.apache.http.conn.ssl.AbstractVerifier.extractCNs(AbstractVerifier.java:277)
    at org.apache.http.conn.ssl.AbstractVerifier.getCNs(AbstractVerifier.java:265)
    at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:157)
    at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:140)
    at org.apache.http.conn.ssl.SSLConnectionSocketFactory.verifyHostname(SSLConnectionSocketFactory.java:286)
    at org.apache.http.conn.ssl.SSLConnectionSocketFactory.createLayeredSocket(SSLConnectionSocketFactory.java:276)
    at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:254)
    at org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:123)
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:318)
    at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:363)
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:219)
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:195)
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:86)
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:57)
To resolve all these classes, the OSGi manifest file needs the following package imports as well:
javax.naming.directory
javax.naming.ldap

The Import-Package section in MANIFEST.MF should look like this:

Import-Package: javax.crypto,javax.crypto.spec,javax.net,javax.naming,javax.nami
 ng.directory,javax.naming.ldap
,javax.net.ssl,javax.security.auth.x500,org.ietf.jgss,org.osgi.framework;version="[1.5,
 2)",org.osgi.service.cm;version="[1.3,2)",org.apache.commons.logging;ve
 rsion="[1.1.0,1.2.0)",net.sf.ehcache;resolution:=optional,net.spy.memca
 ched;resolution:=optional,org.apache.http.util;version="[4.3.0,4.4.0)",
 org.apache.http.io;version="[4.3.0,4.4.0)",org.apache.http.pool;version
 ="[4.3.0,4.4.0)",org.apache.http.impl.pool;version="[4.3.0,4.4.0)",org.
 apache.http.impl;version="[4.3.0,4.4.0)",org.apache.http.message;versio
 n="[4.3.0,4.4.0)",org.apache.http.impl.entity;version="[4.3.0,4.4.0)",o
 rg.apache.http.params;version="[4.3.0,4.4.0)",org.apache.http.impl.io;v
 ersion="[4.3.0,4.4.0)",org.apache.http;version="[4.3.0,4.4.0)",org.apac
 he.http.concurrent;version="[4.3.0,4.4.0)",org.apache.http.entity;versi
 on="[4.3.0,4.4.0)",org.apache.http.config;version="[4.3.0,4.4.0)",org.a
 pache.http.protocol;version="[4.3.0,4.4.0)"

Hope this was helpful!