Java Dynamic web project classpath - java

I have a project which in the meantime runs locally on Tomcat but I'm planning to deploy to some server in the future.
I have a few questions:
I'm using tomcat-jdbc.jar. How should I include that jar in the project? copy it to WEB-INF/lib or add a library reference to tomcat? is the latter portable? Can I use this jar even if the server I'm deploying to is using jetty?
When I added the JRE, eclipse asked me to point it to the JRE path. The line that was added in the classpath was
classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"
How does eclipse figure out from this line where the JRE is at?
When the project is deployed to the server, how would the project hierarchy look like?
My guess is:
<project name>
----<build> (.class files)
----<WebContent>
--------<META-INF>
------------MANIFEST.MF
--------<WEB-INF>
------------<lib>
----------------external_jar.jar
------------web.xml
---------index.html
Is this correct? if so, how will the runtime know where to find the JRE? or the tomcat-jdbc.jar which is in the Tomcat installation folder?

Your application needs the following three types of "resources"
System Resources: JRE classes, some extensions/libraries provided by the server you deploy on.
Your dependencies: Any libraries you use, say common-utils, poi etc. These JAR files go in your web-inf/lib folder
Your classes. These are deployed with the WAR file at web-inf/classes
So, to answer your questions:
If you are deploying to Tomcat, the tomcat-jdbc.jar will be provided. Take care of the version though. If your prod server will be tomcat but dev is say Jetty, then you need to provide this jar in your local IDE, but not export it in the WAR file. But if you are developing on tomcat and say deploying on some other server, then this jar has to be bundled with your war file (web-inf/lib folder). Dev and Prod servers need not be same, but take care of the JRE version and dependency on Prod server provided libraries.
JRE is a configurable setting for your server and also your IDE (Eclipse)
Project hierarchy is correct, but you will most probably deploy as WAR file, so your build folder is exported in web-inf/classes. You can verify by opening the WAR file with any zip editor.
Regarding the portability of tomcat-jdbc.jar
Unfortunately this depends on the tomcat library and version. There might be more dependencies of this jar file which might cause problems later on. I would recommend not relying on this jar unless you plan to deploy on tomcat.

you should test your application with the same server you're going to use in production.
to see and set the jre properties eclipse->preferences->java->Installed JREs.
If you export a war file, all files in WebContent will be in the war and the .class files from src folder will be in WEB-INF/classes.
When you uses a server like tomcat, it uses the configuration you setted on it.

If you want the application to always reference your jar, put it in the web-inf lib.
As Daniel has mentioned below,eclipse gets the JREs from the installed JREs under the preferences tab. You can have multiple JREs installed and configured in your eclipse and then select individually for a project and also select default.
3.Your project hierarchy is correct. The runtime will get the JRE from the JAVA_HOME environment variable set on the server.

1) Pool connections, it's a service provided by Application Server (Tomcat in this case). IMHO you have to avoid bind your application with specific implementation, in that case use generic javax.sql.DataSource for expample, and then "inject" or lookup the implmementation from the server. Then if you use Jetty, configure what you want as connection pool implementation https://wiki.eclipse.org/Jetty/Howto/Configure_JNDI_Datasource
So dont´t include in your web-inf/lib tomcat-jdbc.jar.
2) The "org.eclipse.jdt.launching.JRE_CONTAINER" it's a internal variable of eclipse and the value is what you configure on eclipse properties. It's used for eclipse to compile and run your app.
3) in your project structure ".class" files, must go in "WEB-INF/classes". That it's defined by servlet specification. Eclipse automatically generate the correct structure if you select yor project and with right click run "Export" --> "War file". Or you can use maven.

Related

Hibernate works in Java Main Class but does not works if conneted to JSF form via Managedbean [duplicate]

How should I add JAR libraries to a WAR project in Eclipse without facing java.lang.ClassNotFoundException or java.lang.NoClassDefFoundError?
The CLASSPATH environment variable does not seem to work. In some cases we add JAR files to the Build Path property of Eclipse project to make the code compile. We sometimes need to put JAR files inside /WEB-INF/lib folder of the Java EE web application to make the code to run on classes inside that JAR.
I do not exactly understand why CLASSPATH does not work and in which cases we should add JARs to Build Path and when exactly those JARs should be placed in /WEB-INF/lib.
The CLASSPATH environment variable is only used by the java.exe command and even then only when the command is invoked without any of the -cp, -classpath, -jar arguments. The CLASSPATH environment variable is ignored by IDEs like Eclipse, Netbeans and IDEA. See also java.lang.ClassNotFoundException in spite of using CLASSPATH environment variable.
The Build Path is only for libraries which are required to get the project's code to compile. Manually placing JAR in /WEB-INF/lib, or setting the Deployment Assembly, or letting an external build system like Maven place the <dependency> as JAR in /WEB-INF/lib of produced WAR during the build, is only for libraries which are required to get the code to deploy and run on the target environment too. Do note that you're not supposed to create subfolders in /WEB-INF/lib. The JARs have to be placed in the root.
Some libraries are already provided by the target JEE server or servletcontainer, such as JSP, Servlet, EL, etc. So you do not need put JARs of those libraries in /WEB-INF/lib. Moreover, it would only cause classloading trouble. It's sufficient to (indirectly) specify them in Build Path only. In Eclipse, you normally do that by setting the Targeted Runtime accordingly. It will automatically end up in Build Path. You do not need to manually add them to Build Path. See also How do I import the javax.servlet / jakarta.servlet API in my Eclipse project?
Other libraries, usually 3rd party ones like Apache Commons, JDBC drivers and JEE libraries which are not provided by the target servletcontainer (e.g. Tomcat doesn't support many JEE libraries out the box such as JSF, JSTL, CDI, JPA, EJB, etc), need to end up in /WEB-INF/lib. You can just copy and paste the physical JAR files in there. You do not necessarily need to specify it in Build Path. Only perhaps when you already have it as User Library, but you should then use Deployment assembly setting for this instead. See also ClassNotFoundException when using User Libraries in Eclipse build path.
In case you're using Maven, then you need to make absolutely sure that you mark libraries as <scope>provided</scope> if those are already provided by the target runtime, such as JEE, Servlet, EL, etc in case you deploy to WildFly, TomEE, etc. This way they won't end up in /WEB-INF/lib of produced WAR (and potentially cause conflicts with server-bundled libraries), but they will end up in Eclipse's Build Path (and get the project's code to compile). See also How to properly install and configure JSF libraries via Maven?
Those JARs in the build path are referenced for the build (compile) process only. If you export your Web Application they are not included in the final WAR (give it a try).
If you need the JARs at runtime you must place them in WEB-INF/lib or the server classpath. Placing your JARs in the server classpath does only make sense if several WARs share a common code base and have the need to access shared objects (e.g. a Singleton).
If you are using Maven:
Open the project properties, and under Deployment Assembly click Add...
Then select Java Build Path Entries and select Maven Dependencies
Resolved by setting permissions.
Had related issue using PySpark and Oracle jdbc. The error does not state that the file cannot be accessed, just that the class cannot be loaded.
So if anyone still struggles, check the permissions. Some might find it obvious tho'.
I want to give the answer for the folowing link question ClassNotFoundException oracle.jdbc.driver.OracleDriver only in servlet, using Eclipse
Ans: In Myeclipse go to Server-->left click on Myeclipse Tomcat7-->Configure Server Connector-->(Expand)Myeclipse Tomcat7--> Paths-->Prepend to classpath-->Add jar (add oracle14 jar)-->ok

usage and significance of .Classpath in Application EAR

I am currently working on an EAR application which has a WEB project and EJB project in it.
To work on eclipse with local set up i do so many build path setups and all.
All these buildpaths show up in .classpath of the project.
Now when i export the EAR and deploy on Server everything works on server.
My doubt is how does server know about my local system paths which are present in .classpath of my project.
Does it mean .classpath has no significance at runtime?
Please explain.
.classpath file is eclipse specific, it will not be bundled with the EAR. Eclipse uses it to set the classpath for the project.
An EAR has a well defined structure and Servlet container understands that structure, using it's knowledge it prods around in the EAR file and extracts classes to load in the runtime.
Obviously server doesn't have any knowledge of your local environment. An EAR (or WAR or JAR) file is a simple archive file (with some specification or the file order and all). You can unzip it using any utility (something like 7zip). Check the structure of the packed EAR, that will give you a better understanding of what goes to your server.

Missing spring-web.jar when deploying with WTP to Websphere

I've build a spring-based web project in Eclipse using Maven. Dependencies and class path are correct and the deployment assembly also lists spring-web*.jar correctly. However, when I deploy it using WebSphere 8 from within Eclipse the mentioned JAR file is missing from the created libs folder under .metadata.plugins\org.eclipse.wst.server.core\tmp2*\WEB-INF\lib . This JAR however is listed under the module section as a submodule for the container. I wasn't able to find out, why this JAR is listed there and why it is not copied to the LIB folder.
It works with a Tomcat server, it works when I export the WAR and deploy it manually and it also works when I manually copy the JAR to the lib folder.
Q1: Is this specific to Websphere?
Q2: Why is SPRING-WEB listed as submodule and what effect has this?
Q3: How can I automatically deploy it correctly?
This seems to be a bug in WAS as reported here
You can either copy the missing JARs manually or you can use the "Run server with resources on server" option in the "Publishing settings for WebSphere Application Server" server settings.

How does Java EE application server find the classpath?

Are .classpath and .project files as created by Eclipse, only used by Eclipse internally? Are these files being used by JDK? How do Java EE application servers find classes and path?
In between, when I create the ear, war using ant, I will use ant options to look out for the libraries for the project compilation.
Then does the application server use .classpath for compilation?
Java EE application servers don't need the classpath which we set in Eclipse for the war (web project) to run because while packaging the war, we stuff all the required jar's in the lib folder of the war file. When the application server starts , it loads all the jars from the lib folder without the need for any classpath declaration.
Also, in case any external dependencies are needed, most Java EE servers provide facility to define external jar dependencies.
And yes, .project, .settings folder etc. are all needed by Eclipse to handle the classpath and other settings of your workspace.
if we put it simply
.classpath and .project are eclipse metadata files and are used by eclipse only. They are meaning less for other applications.

eclipse tomcat: take the classpath from the project

I have a java web project in eclipse and want to define a tomcat server.
It seems that in the tomcat server I must define again my classpath. How can I tell tomcat to just use the classpath from my project (shouldn't this be obvious?).
Unfortunately my jars are scattered all around and it is an headache to add them one by one to my tomcat configuration and maintain this.
Here you go :
Run -> Run Configurations... -> Classpath
You don't have to tell tomcat to look for jars scattered all around the places. This can be easily taken care by eclipse. Configure your eclipse build path properly. Create Libraries (in eclipse) and group jar together. Try to export the war and check if eclipse is packaging all the required jars in WEB-INF/lib.
No, Tomcat and web apps have a pretty well-defined CLASSPATH. You shouldn't have to specify anything if you package your app properly:
All the packages and .class files in WEB-INF/classes are in CLASSPATH.
So are all the JARs in WEB-INF/lib
You should figure out how to put your JARs in the right place - that's WEB-INF/lib of your WAR file. Maybe Ant or Maven can help you.

Categories