JBoss eap 6 JNDI Issue - jboss

I am trying out the following tutorial which creates a simple EJB and then accesses via a Java SE client:
http://www.tutorialspoint.com/ejb/ejb_create_application.htm
This tutorial uses ant but I am using maven. Now, in my client, I am getting the following error while creating the initial context:
javax.naming.NoInitialContextException: Cannot instantiate class:
org.jnp.interfaces.NamingContextFactory [Root exception is java.lang.ClassNotFoundException:
org.jnp.interfaces.NamingContextFactory]
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:674)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:307)
at javax.naming.InitialContext.init(InitialContext.java:242)
at javax.naming.InitialContext.<init>(InitialContext.java:216)
at EJBDriver.main(EJBDriver.java:21)
I have added the following dependencies to my pom.xml:
<groupId>org.jboss.spec</groupId>
<artifactId>jboss-javaee-6.0</artifactId>
<groupId>org.jboss.as</groupId>
<artifactId>jboss-as-ejb-client-bom</artifactId>
Finally, the jndi properties:
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost:4447
How to solve this issue?

I solved the problem as follows
1- If the EJB classes are in different projects, than the project with EJB classes needs to be added to the classpath of the project containing the client. For Eclipse:
Right Click on project->Build Path->Configure Build Path-> Projects tab->Add->Select the desired project->OK->OK
2- The jboss-client.jar file needs to be added to the class path of the EJB client project as well.
Right Click on project->Build Path->Configure Build Path-> Projects tab->Librariers->Add External JAR
Browse to the jboss-client.jar file(In windows its located in JBoss_Installation/bin/client)
Select the file and click OK twice.

jboss 6 should use the following properties:
naming factory= org.jboss.naming.remote.client.InitialContextFactory
provider url= remote://localhost:4447

Related

javax.persistence jar is not loaded into tomcat

I'm trying to run JPA servlet on Tomcat 8.
From my servlet I call method of a class named QueryDB.class located in another Eclipse project (JPA project, not WEB project).
But tomcat throws
SEVERE: Servlet.service() for servlet [MyRESTService] in context with path [/AutomationWeb] threw exception [org.glassfish.jersey.server.ContainerException: java.lang.NoClassDefFoundError: javax/persistence/Persistence] with root cause
java.lang.ClassNotFoundException: javax.persistence.Persistence
at java.net.URLClassLoader$1.run(Unknown Source)
...
Exception is thrown at line EntityManagerFactory emf = Persistence.createEntityManagerFactory( "AutomationDB_JPA" ); in QueryDB.class
I'm using Eclipse Mars with Maven. I have EclipseLink in my pom.xml defined:
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.6.1</version>
</dependency>
I have following jars in my Application's WEB-INF/lib:
eclipselink-2.6.1.jar
javax.persistence-2.1.0.jar , it has javax/persistence/Persistence.class inside
There is no other SEVERE messages in the tomcat log.
What's wrong with this setup?
I also tried adding the persistence jar to apache-tomcat-8.0.28\lib , but got the same error.
Update: I moved Persistence.createEntityManagerFactory call to the servlet itself and got no classloader exception. So the question changes: why classloader used for QueryDB.class is different from servlet's classloader? Also, why classloader used in QueryDB.class does not see the Persistence.class, which is seen by the servlet's classloader?
Ok I've found a solution.
Excluded my JPA project from tomcat VM classpath
Converted my JPA project to the Utility project
Modified Deployment Assembly in my Web project properties so that it
will accept JPA project as .jar archive into WEB-INF/lib
I have no classloader exception anymore.

Importing External JAR to Websphere Liberty? [NoClassDefFoundError]

I want to use this JSON parser in my JAX-RS application that runs on Websphere Liberty.
I have done the following:
1) Right-click on Project -> Properties -> Java Build Path -> Add External Jar -> C:\javalib\quick-json.jar
2) Added C:\javalib to enviroment variable CLASSPATH
3) Added fileset xml to serverl.xml
<library id="ExternalLibs">
<fileset dir="C:\javalib" includes="*.jar"/>
</library>
4) Unchecked 'Enable project specific settings' in 'Java Compiler'
5) Cleaned the Project
[EDIT]
I was initially creating a new instance and then I turned it into an #ApplicationScoped bean and injected it. Now I get this error:
The JNDI lookup failed for JNDI name java:module/ConsoleREST with the following Exception CNTR4007E: An error occurred creating the websphere.jaxrs.service.ConsoleREST interface for the ConsoleREST enterprise bean in the WebApiConsole.war module in the WebApiConsole application. The enterprise bean look up failed using the java:module/ConsoleREST JNDI name. Exception: com/json/parsers/JsonParserFactory.
CNTR4007E: An error occurred creating the websphere.jaxrs.service.ConsoleREST interface for the ConsoleREST enterprise bean in the WebApiConsole.war module in the WebApiConsole application. The enterprise bean look up failed using the java:module/ConsoleREST JNDI name. Exception: com/json/parsers/JsonParserFactory
The first step was enough to get it to compile. Now I'm getting what I learned to be a runtime error. I would appreciate help!
You also need a <classloader> element in your application definition in the server.xml, which references the shared library. For example,
<application id="myapp" name="My App" type="war" location="somewhere.war">
<classloader commonLibraryRef="ExternalLibs" />
</application>
Alternatively, if your application is the only user of the library, you could package it in the WEB-INF/lib folder of your war. (Putting it in WebContent/lib in Eclipse should accomplish this.)

How to exclude Eclipse plugin library jars from Tomcat classpath

I have a Spring MVC project that uses Maven imported into Eclipse Kepler SR1 JEE and deployed to Tomcat 7 within Eclipse. Everything was fine until I recently added elasticsearch as a dependency to the Maven pom file and now when I start Tomcat I get an exception
java.lang.ClassCastException: org.springframework.web.servlet.DispatcherServlet cannot be cast to javax.servlet.Servlet
Launching the server in verbose mode, I see that its because the servlet API is now being loaded from 2 places:
the Tomcat installation folder where it normally comes from
Loaded javax.servlet.Servlet from file:/C:/ProgramFiles/apache-tomcat-7.0.33/lib/servlet-api.jar
but also from one of the Eclipse plugins (Groovy) I have installed
Loaded javax.servlet.Servlet from file:/C:/ProgramFiles/eclipse/plugins/org.codehaus.groovy_1.8.6.xx-20130703-1600-e43-RELEASE/lib/servlet-api-2.4.jar
I can't figure out why this is happening. If I rename the servlet jar file in the plugin's lib folder (i.e. change the extension from .jar to something else so that its not recognized), Tomcat starts up without an exception but that's a hack and so I'd like to know what the cause is and if I'm missing some configuration to exclude plugin library jars from the Tomcat classpath.
Thanks.

How to resolve javax/mail/MessagingException?

I am using eclipse indigo to run my tomcat server, when I am launching tomcat server, the tomcat server successfully up and running, but with error shown in the console.
ERROR - ContextLoader[177]: Context initialization failed
org.springframework.beans.factory.BeanDefinitionStoreException: Error registering bean with name 'com.huahsin68.MyBoc' defined in class path resource [my-spring.xml]: Class that bean class [com.huahsin68.MyBocImp] depends on not found; nested exception is java.lang.NoClassDefFoundError: javax/mail/MessagingException
java.lang.NoClassDefFoundError: javax/mail/MessagingException
It seems like the MessagingException wasn't found. I have check in the Java Build Path > Libraries, I notice that javax.mail_1.4.0.v200105080615.jar was there. This jar file is locate under eclipse > plugins folder. Is there anything solution to rectify this problem?
THanks #!
With Maven, you can add the following dependency:
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.5</version>
</dependency>
Try the following:
Download the java mail jars from: http://www.oracle.com/technetwork/java/javamail/index-138643.html
Add the jars to your WEB-INF/lib folder
Add the jars to Java Build Path > Libraries
That should do it.
For gradle/android users, in build.gradle (Module app):
compile 'javax.mail:mail:1.4.1'
I had a similar problem, running tomcat stand-alone (that is, not through Eclipse). I copied mail-1.4.jar to my tomcat/lib directory. This worked for me.
For those conning late in this ..
I got the same error resolved by adding below jar.
geronimo-javamail_1.4_mail-1.8.3.jar

Can I make a WAR depend on a JNDI entry in JBoss 5.1?

As part of an upgrade from JBoss 4.0.4 to 5.1, I am trying to get a WAR to deploy after an EAR is successfully deployed. JBoss 5.x does not support PrefixDeploymentSorter like 4.x did, which means that I have to use <depends> in the WAR's jboss-web.xml.
It seems I cannot depend on the EAR itself, so I pick the last deployed EJB instead. This EJB provides a JNDI entry that the WAR needs.
Here's the EJB as it deploys when the WAR is absent from the deploy directory:
2010-03-25 10:47:30,348 INFO [org.jboss.ejb3.session.SessionSpecContainer] (main) Starting jboss.j2ee:ear=my-ear.ear,jar=mypackage-ejb.jar,name=MyFacadeBean,service=EJB3
2010-03-25 10:47:30,350 INFO [org.jboss.ejb3.EJBContainer] (main) STARTED EJB: my.package.MyFacadeBean ejbName: MyFacadeBean
2010-03-25 10:47:30,371 INFO [org.jboss.ejb3.proxy.impl.jndiregistrar.JndiSessionRegistrarBase] (main) Binding the following Entries in Global JNDI:
my/MyFacade/local - EJB3.x Default Local Business Interface
my-ear/MyFacadeBean/local-my.package.MyFacade - EJB3.x Local Business Interface
And here's the depends snippet from jboss-web.xml:
<depends>jboss.j2ee:ear=my-ear.ear,jar=mypackage-ejb.jar,name=MyFacadeBean,service=EJB3</depends>
My problem is: The WAR starts to deploy immediately after "STARTED EJB:", i.e. before MyFacadeBean is bound to JNDI, which causes bean deployments to fail:
2010-03-25 10:47:39,068 ERROR [my.facade.FacadeFactory] (main) MyFacade not bound
2010-03-25 10:47:39,069 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[my.host.no].[/]] (main) StandardWrapper.Throwable
java.lang.ExceptionInInitializerError
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:164)
at my.freemarker.servlet.FreemarkerController.setupPojoServiceFactory(FreemarkerController.java:621)
[...]
Caused by: java.lang.RuntimeException: javax.naming.NameNotFoundException: MyFacade not bound
at my.facade.FacadeFactory.getFacade(FacadeFactory.java:61)
After the WAR has finished its deployment, MyFacade happily (mockingly?) continues its deployment and binds the JNDI entries.
If I hot-deploy the WAR after the EAR has deployed, everything works as intended.
I even thought of depending on a dummy EJB in the EAR, and using <module-order>strict</module-order> in jboss-app.xml to force it to load as the last module. But alas, JBoss 5.x does not support that either. Doh!
Is there a way to depend on the JNDI entry itself? Are there other ways of solving this?
Here's how you can achieve this on JBoss 5.1.x.
First, add a file called aliases.txt into the META-INF directory of your EAR. This file should just contain a single line with an arbitrary name / identifier for your EAR. For example, if you have my-ear.ear, your META-INF/aliases.txt file could contain 'my-ear'. It just needs to be something that won't clash with any other aliases declared by other apps deployed on the same server.
Next, add a jboss-dependency.xml file to the META-INF directory of your WAR, containing the following (subsituting 'my-ear' for the alias you created above):
<dependency xmlns="urn:jboss:dependency:1.0">
<item whenRequired="Real" dependentState="Create">my-ear</item>
</dependency>
This will ensure the EAR is deployed before the WAR.
Also, if you try to deploy the WAR without the EAR being present, JBoss will log a clear deployment error message telling you about the missing dependency.