Java EE Exception: Name java:comp is not bound in this Context - java

I have the Java EE app in Netbeans 7.2.1. Trying to deploy it (building ends OK), I get the error in Tomcat log:
Caused by: javax.naming.NameNotFoundException: Name java:comp is not bound in this Context
at org.apache.naming.NamingContext.lookup(NamingContext.java:770)
at org.apache.naming.NamingContext.lookup(NamingContext.java:153)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at org.springframework.jndi.JndiTemplate$1.doInContext(JndiTemplate.java:154)
at org.springframework.jndi.JndiTemplate.execute(JndiTemplate.java:87)
at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:152)
at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:178)
at org.springframework.jndi.JndiLocatorSupport.lookup(JndiLocatorSupport.java:95)
at org.springframework.jndi.JndiObjectLocator.lookup(JndiObjectLocator.java:105)
at org.springframework.jndi.JndiObjectFactoryBean.lookupWithFallback(JndiObjectFactoryBean.java:201)
at org.springframework.jndi.JndiObjectFactoryBean.afterPropertiesSet(JndiObjectFactoryBean.java:187)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1454)
Looking for code, that causes this error I found the follow:
public Object lookup(String name) throws NamingException {
return getURLOrDefaultInitCtx(name).lookup(name);
}
How is this caused and how can I solve it?

That will in case of Tomcat happen when you have for some reason dropped arbitrary servletcontainer-specific JARs such as jsp-api.jar, servlet-api.jar, catalina.jar, etc in webapp's /WEB-INF/lib. You should remove all servletcontainer-specific JARs from there, they do not belong there. Also, make sure those JARs are not nested within other JARs you have inside WEB-INF/lib.
This is a common starter's mistake in order to "fix" compilation errors on JSP/Servlet libraries which should have been solved differently; namely by configuring the IDE project to be associated with a "Target server" in Netbeans or "Target runtime" in Eclipse. The IDE will then automagically include the server's libraries during compiletime.

Related

WildFly + java Preferences cannot find custom preferences class in classpath

I am deploying an EAR with the below structure to Wildfly 9.0.2
my.ear
|-my.sar (it is a SAR archive)
|-lib
|-myPreferencesImpl.jar
I am able to load the classes in lib/myPreferencesImpl.jar from my.sar (like class.forname(com.my.PreferencesFactory) directly. However, java.util.prefs.Preferences.factory is not able to load com.my.PreferencesFactory. I am passing Djava.util.prefs.PreferencesFactory=com.my.PreferencesFactory as command line parameter during WildFly startup.
Below is the exception thrown.
Caused by: java.lang.ClassNotFoundException: com.my.PreferencesFactory from [Module "org.jboss.as.jmx:main" from local module loader #76a3e297 (finder: local module finder #4d3167f4 (roots: /CSA_wildfly/wildfly/modules,/CSA_wildfly/wildfly/modules/system/layers/base))]
at org.jboss.modules.ModuleClassLoader.findClass(ModuleClassLoader.java:205)
at org.jboss.modules.ConcurrentClassLoader.performLoadClassUnchecked(ConcurrentClassLoader.java:455)
at org.jboss.modules.ConcurrentClassLoader.performLoadClassChecked(ConcurrentClassLoader.java:404)
at org.jboss.modules.ConcurrentClassLoader.performLoadClass(ConcurrentClassLoader.java:385)
at org.jboss.modules.ConcurrentClassLoader.loadClass(ConcurrentClassLoader.java:130)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
at java.util.prefs.Preferences.factory(Preferences.java:254)
Please help me fix this.
UPDATE:
I finally fixed by migrating the startup service using JMX Service to a #Singleton + #Startup EJB. No other changes were required.
When you pass the arg Djava.util.prefs.PreferencesFactory=com.my.PreferencesFactory in command line, Wildfly is going to look for the class in the classes available to it, which is provided by way of modules . During the Wildfly bootup, it's not even going to know that there is a application with that class present, neither can it provide the same class to other deployments on the same container. So if you want the command line arg to work, make sure the deploy the Factory implementation as a Wildfly module.

Spring application fails on startup with NoClassDefFoundError when deployed in tc Server with Insight

I have a Spring-based app (packaged into a WAR) that runs fine in Jetty and "ordinary" Tomcat 7, but produces strange NoClassDefFoundError when it's deployed into tc Server with Spring Insight. The class that it's complaining can't be found is definitely in a JAR in the WEB-INF/lib folder (and I've double-checked that no competing JARs exist in the Tomcat shared lib folder).
Here's the stack trace, showing that Spring thinks it can't locate the class HierarchicalLoop:
java.lang.ClassNotFoundException: com.foo.HierarchicalLoop<com.foo.Loop>
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714) ~[na:na]
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559) ~[na:na]
... 32 common frames omitted
Wrapped by: java.lang.NoClassDefFoundError: com/foo/HierarchicalLoop<com/foo/Loop>
at java.lang.Class.getDeclaredConstructors0(Native Method) ~[na:1.7.0_60]
at java.lang.Class.privateGetDeclaredConstructors(Class.java:2532) ~[na:1.7.0_60]
at java.lang.Class.getConstructor0(Class.java:2842) ~[na:1.7.0_60]
at java.lang.Class.getDeclaredConstructor(Class.java:2053) ~[na:1.7.0_60]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:80) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1094) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
... 26 common frames omitted
Wrapped by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'x12Builder' defined in class path resource [spring/x12-builder-config.xml]: Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: com/foo/HierarchicalLoop<com/foo/X12Loop>
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1101) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
If I disable Insight in the server instance, the app loads fine, and this is reproducible on 3 different machines. As I said, the WAR also loads correctly under Jetty and Tomcat (without Insight). So I'm pretty sure it's narrowed down to something that Insight does.
In my experience, these kinds of mysterious NoClassDefFoundError or ClassNotFoundException errors are often caused by class loader mixups. For example, the container's root classloader trying to load classes from an application JAR. But in this case Insight is a black box to me, I'm not sure what it's doing under the covers. I suspect that maybe the Spring classes are coming from a classloader other than my application's classloader, which would explain why Spring can't see the classes from JARs in the application's lib. But that's little more than educated speculation, and even if it were accurate I have no idea how to resolve it.
Any ideas for troubleshooting or insight into how Insight works are appreciated.
It turns out that this was due to some kind of limitation or bug in Insight. The class that it was complaining about, HierarchicalLoop<Loop> was causing problems with the Insight classloader (TomcatWeavingInsightClassLoader) which made it report it as a ClassNotFoundException. It has something to do with type parameters; here's the two classes that were involved in causing the class loader problems:
public class Loop<ChildType extends Loop> {
//...
}
public class HierarchicalLoop<ChildType extends Loop> extends Loop<ChildType> {
//...
}
This is perfectly valid Java and no other containers/classloaders (Tomcat, Jetty, JUnit) have any problem loading those classes. But Insight does, for some reason.
There are subclasses of HierarchicalLoop but all of them just specify Loop as the ChildType parameter, so the parameter isn't actually needed. So I was able to work around the problem by removing the type parameter from HierarchicalLoop :
public class HierarchicalLoop extends Loop<Loop> {
//..
}
I'm pretty sure this is a bug in Insight; I'd like to report it but have been unable to find any community support or bug reporting channel.

java.lang.NoClassDefFoundError while deploying the War in websphere which uses Couchbase cache

I am facing the below error while deploying the war file in Websphere-8.5.5
Caused by: java.lang.NoClassDefFoundError: org/apache/http/params/SyncBasicHttpParams
at com.couchbase.client.ViewConnection.createConnections(ViewConnection.java:120)
at com.couchbase.client.ViewConnection.<init>(ViewConnection.java:100)
at com.couchbase.client.CouchbaseConnectionFactory.createViewConnection(CouchbaseConnectionFactory.java:184)
at com.couchbase.client.CouchbaseClient.<init>(CouchbaseClient.java:247)
at com.couchbase.client.CouchbaseClient.<init>(CouchbaseClient.java:179)
at com.infosys.aham.services.utils.cache.CouchbaseCacheManager.instantiateCouchbaseManager(CouchbaseCacheManager.java:145)
at com.infosys.aham.services.utils.cache.CouchbaseCacheManager.<init>(CouchbaseCacheManager.java:55)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:56)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:39)
at java.lang.reflect.Constructor.newInstance(Constructor.java:527)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147)
... 87 more
I am using Couchbase-Client -1.1.9 and http-core-4.1.3
The http-core jar is bundled in the war file /web-inf/lib folder.
Also while looking in the websphere EnterprizeApplication-->application -->Manage Module --> moduleName--> ClassLoaderViewer --> LoaderView
The http-core-4.1.3 is there in the table (jar is present in the classpath).
(I also tried with the http-core 4.1.1 jar.)
The application is still not able to find the class present in the given jar.
I am new to websphere, please help me out with this.
Thanks in advance.
The http-core classes are already included in WebSphere. To use different implementation create isolated shared library with conflicting libraries and map it to the application.

Jar dependency exceptions on weblogic servers for an application

I am getting an exception while deploying on Weblogic Server:
[2012-01-13 01:17:11,496] ERROR [org.springframework.web.servlet.DispatcherServlet] Context
initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name
'org.springframework.validation.beanvalidation.LocalValidatorFactoryBean#0': Invocation of init method
failed; nested exception is java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException
at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.at
org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
at
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
at
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
at
org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.at
org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.
I have some jars as below:
log4j-1.2.14.jar
spring-asm-3.0.3.RELEASE.jar
spring-beans-3.0.3.RELEASE.jar
spring-context-3.0.3.RELEASE.jar
spring-core-3.0.3.RELEASE.jar
spring-expression-3.0.3.RELEASE.jar
spring-web-3.0.3.RELEASE.jar
spring-webmvc-3.0.3.RELEASE.jar
servlet-2.3.jar
validation-api-1.0.0.GA.jar
commons-logging-1.1.1.jar
hibernate-validator-4.0.2.GA.jar
slf4j-api-1.5.6.jar
slf4j-log4j12-1.5.6.jar
standard.jar
commons-fileupload-1.1.1.jar
ESAPI-1.4.4.jar
I am guessing the issues can be but need guidance to solve it:
After researching the issue, 2 likely scenarios came to the forefront:
Hibernate Validator Library has a bug resulting in a dependency on JDK 6
Hibernate Validator has a dependency on the JAXBException Class, and the class is not present in the existing Weblogic Environment
Upon further research, the hibernate-validator-4.0.2.jar build states a clear dependency on JDK 5.
Digging deeper into the Hibernate Validator code, it appears that version 4.0.2 relies on JAXB API 2.1.
Weblogic servers are on java 1.5 and code is also compiled on java 1.5 then why the exceptions seems to pop up??
You're being bitten by JAr incompatibilities. Your project has one version of the JAR, but WebLogic has another. The two don't match. (This is what OSGi was born to solve.)
You'll need to tell WebLogic which JAR you prefer. There's a setting in the weblogic.xml for "prefer WAR file JARs." I'm sorry, I can't recall the exact tag name; please Google for it.

Getting ServletException and NoClassDefFoundError during webapp startup

I'm getting the following errors:
javax.servlet.ServletException: Error instantiating servlet class auth.Login
and
java.lang.NoClassDefFoundError: javax/persistence/Persistence
How do I ensure my project's configuration is properly setup?
To me, this look correct.
I assume that java.lang.NoClassDefFoundError: javax/persistence/Persistence is the root cause of the servlet instantiation error, so we can just ignore the servlet instantiation error for now. A NoClassDefFoundError means that the class mentioned in the message was present in the webapp's compile time classpath, but not in the webapp's runtime classpath.
Those JARs which you have there in /lib folder should actually go in /WEB-INF/lib. This is part of the webapp's runtime classpath. Further you should also remove those three jsp* and servlet* JAR files. They do not belong in the webapp library. They belong in the servletcontainer library. If you did this to fix compile errors, it has to be fixed differently.
I think runtime classpath does not have servlet-api.jar and that is causing java.lang.NoClassDefFoundError: javax/persistence/Persistence
Secondly as said earlier your libraries should be under WEB-INF/lib folder to fix the errors

Categories