About Java EE classpath - classpath

I am developing an online conference system based on Java EE framework using NetBeans for my school. My project contains the Java EE library which has javax.mail package.
I use the javax.mail.authenticator class in my code and everything seems to be OK. However, when I run the project and try to send email with this system, problem occurs, saying it can not find class javax.mail.authenticator.
Then I put the files mail.jar and authenticator.jar in folder WEB-INF/lib, after that it can send email correctly. I don't know why it can not find the class authenticator and why this two jar files should be put there?
PS: I use Tomcat 6 as my web server.

There is a difference between having a class on the Java build path, and on the classpath. By putting mail.jar and authenticator.jar into WEB-INF/lib, you have put them into Tomcat's classpath so that Tomcat can "see" those classes at runtime.
Recommended reading: What is the difference between Class Path and Build Path
P.S. I think you mean Java EE. It hasn't been called J2EE for ~5 years now.

When you deploy an application to a Java EE container, like Tomcat, there are certain places classes and jars are loaded to form an application's classpath. For web applications (a war), classes are loaded from WEB-INF/classes and from jars in the WEB-INF/lib.

Related

CDI imports cannot be resolved in Eclipse

i have written an application on my pc using CDI as the backing bean for jsf, the application works completely fine.
Now i setup everything on a new pc, installed glassfish 4.1, setup eclipse kepler by putting the installed jre as jdk7 from the glassfish folder.
when i make a war for the application on my pc and deploy it onto the glassfish server on the other pc everything works fine.
now i tried taking the project from my pc and imported it onto the new pc. the eclipse on the new pc wont recognize import javax.enterprise.context.SessionScoped; giving compiler error that this annotation is not available.
The import javax.enterprise.context cannot be resolved
it doesnt recognize javax.enterprise.context.* at all. like CDI is not available. i am using JDK 7 which contains JSR 346, so i am completely confused what the issue could be.
if i deploy the application directly through the glassfish admin console using the war file it works perfectly fine but if i try to publish it through eclipse, it gives me unresolved compilation errors
Eclipse did not import ALL glassfish jars, so you need to:
Right Click on the project name, Build Path, Configure build path, under Libraries Click Add External Jars, go to where you have extracted Glassfish and import all jars that exists in:
glassfish4\glassfish\modules
and all sub directories two, like endoresed.
This will fix the problem for CDI and other stuff lik JAX-RS
If I had to guess, your .project and .classpath files are pointing to JAR locations that are specific to your old computer.
This is why people like build tools such as maven, or gradle, you can pick up and reimport a project; it'll resolve all dependencies for you.
Do you happen to use the GlassFish Tools plugin for Eclipse? I found that the GlassFish System Libraries classpath container, which is created implicitly by that plugin and which is added to all projects that specify a GlassFish server as their runtime, misses cdi-api.jar. This is where the javax.enterprise.context.* package is located (and many others).
So I could fix this by adding cdi-api.jar as an External Jar to the build path. After that, you may get a warning like Classpath entry [...]/cdi-api.jar will not be exported or published. Runtime ClassNotFoundExceptions may result., which can be eliminated by using the Quick Fix to Exclude the associated raw classpath entry from the set of potential publish/export dependencies. That JAR file is already contained in the modules folder of GlassFish, obviously.
Maybe this problem only occurs with the GlassFish Web Profile, at least that's what I use.

Glassfish Application specific class loading

I'm trying to deploy an EJB-JAR on Glassfish (4 or 3.1.2.2).
This EJB-JAR has some library dependencies. I would like to deploy these dependencies application specific. No other application running on glassfish should include this specific libraries.
But unfortunately, it doesn't work like described in the following link.
I always get a ClassDefNotFound-Error.
If I copy all libraries in domain-dir/lib/ext, everything works fine. But now, glassfish will include this libraries to ohter applications too. Am I right?
http://docs.oracle.com/cd/E18930_01/html/821-2418/gatej.html
Just place the library jar in the WEB-INF/lib folder of your EJB jar. There's no need to setup any additional configuration (like is specified in that link).

NoClassDefFoundError on Java EE server in eclipse with tomcat

I'm trying to build a hello world application utilising GCM for Android notifications. When developing the server part, I'm not entirely sure how to get the gcm-server.jar file into the project. I've added it to the build path and Eclipse finds it perfectly fine, but when I build and deploy the project to a tomcat server (from within eclipse) it's throwing the following error at runtime:
java.lang.NoClassDefFoundError: com/google/android/gcm/server/Sender
I'm assuming I'm doing something wrong when importing the jar. What exactly is the procedure for getting a jar into a dynamic web project using Eclipse?
Going to answer my own question because I just got it working.
Turns out adding the .jar's to the Java build path is not enough. Tomcat has a seperate classpath for each project, and won't see the classes in the jar unless you dump the jar files into the WEB-INF/lib folder (which Eclipse does not do automatically).
You need to include the jar file for json. This question seems to have been repeated a few times.
http://code.google.com/p/json-simple/downloads/detail?name=json-simple-1.1.1.jar

Eclipse doesn't import all Axis2 jars but project still runs on Tomcat?

When I develop an Axis2 web service on Eclipse, I noticed that Eclipse is automatically copying the classes from the lib folder of Axis2 to the lib folder of the new project. However, not all classes from the lib folder of Axis2 are being copied. Interestingly, the web service runs without any problem when deployed to Tomcat via Eclipse even if some the jars from Axis2 were not copied. Also, when I viewed the temp file of Tomcat, Tomcat seems to generate the jars for the listed modules on modules.list of the web service.
Can someone enlighten me regarding what is happening on this? Why Eclipse doesn't copy all the jars from Axis2? Why can the web service run on Tomcat even without the other jars from Axis2? What are those temp files for? When and why is it being generated?
I tried to run the same project on WebSphere and I am encountering a ClassDefNotFound exception because of the missing jars. My problem was solved when I copied all the Axis2 jars that was not copied by Eclipse to my project. But I'm not comfortable with my solution because Tomcat can run my project even without those jars. Is my solution really the right solution? Or am I missing a configuration setting?
This is just for clarification:
My web service is already running in Axis2. My class loading policy is set to PARENT_LAST. I know that since WebSphere has its own Axis2 configuration, the class loading policy must be set to PARENT_LAST so that WebSphere will use the Axis2 from the project itself. Aside from setting the class loading policy, I did something to make my web service run on WebSphere. I describe what I did above. My question is why such method must be taken?
WebSphere has it's own axis2 configuration as part of its Java EE server spec for JAX-WS. Change your class loading policy to PARENT_LAST and check if that solves your problem.
Edit:
As the original post already states: WebSphere is a Java EE server depending on version it supports its the standard Java JAX-WS web services. Actually web services became part of the standard jdk.
If you use JAX-WS like mentioned in Introduction to JAX-WS or building web services then you don't have to add any 3rd party library for getting your web services running. As soon as you use the non JDK implementation like axis2 you have to package it with your application.
IBM didn't just pack the axis2 into their WAS/JDK, they modified it. I'm not sure what Tomcat delivers, however as long as you use JAX-WS it shouldn't matter. With JAX-WS you don't have any direct import of the org.apache.axis packages. If you use these imports you have to supply the libraries and make sure that yours are loaded.

Unable to find interface javax.transaction.UserTransaction;

I am trying to setup a new Netbeans project with JPA. The Java SDK version is 1.5.0_22-b03 and J2EE version is J2EE 1.4.I am using TOPLINK Essentials for JPA.
Going by javadoc javax.transaction.UserTransaction interface comes with J2EE 1.4.
Then why it's not able to find javax.transaction.UserTransaction?
The JPA 1.0 specification is meant to cater to Java EE 5 containers and not J2EE 1.4 containers (unless the same also support EJB 3.0 like the erstwhile OC4J containers from Oracle). Likewise JPA 2.0 caters to Java EE 6 containers.
I would suggest using Netbeans with Java EE 5 or Java EE 6, instead of heading for a wild goose chase on getting your JPA 1.0/2.0 provider to work amicably with a J2EE 1.4 container. This isn't merely about placing some JARs in your compile time classpath; your runtime classpath will require classes that are available only in a Java EE 5/6 container.
Either you have an incorrect classpath or you are missing a required jar. What are you using to build?
You have not correctly included the j2ee-1.4.jar in your project classpath. Otherwise you can reference to it. Perhaps you meet problem in setting classpath of your java project in netbeans.
Please check your jar to ensure it is correctly downloaded.
Maybe this is an old question but since no other answer has been posted, I post what I was able to do to make it work.
I found a file at:
C:\Program Files (x86)\NetBeans 8.1\java\modules\ext\hibernate4
Right-click your project >Libraries >Add jar > find your Netbeans installation directory...
usually => C:\Program Files (x86)\NetBeansXXX
find java\modules\ext\hibernate4 and choose jboss-transaction-apixxxxx.jar
hope it helps everyone else,