I am trying to compile and deploy a simple web app from command line.
servlet-api.jar from Apache Tomcat does not compile my java file, but javax.servlet-api-4.0.1 from the maven central repository compiles it successfully. Even so, I get an error when I deploy the app and try to use it in the browser.
I am using:
javac 11.0.8
Apache Tomcat 10.0 (servlet-api.jar 5.0)
Java file:
package com.example.controllers;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class BeerSelect extends HttpServlet {
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("Beer Selection Advice <br>");
String c = request.getParameter("color");
out.println("<br>Got beer color " + c);
}
}
When I try to compile it with the servlet-api.jar I get:
public class BeerSelect extends HttpServlet {
^
symbol: class HttpServlet
src\com\example\controllers\BeerSelect.java:9: error: cannot find symbol
public void doPost(HttpServletRequest request,
^
symbol: class HttpServletRequest
location: class BeerSelect
src\com\example\controllers\BeerSelect.java:10: error: cannot find symbol
HttpServletResponse response)
^
symbol: class HttpServletResponse
location: class BeerSelect
src\com\example\controllers\BeerSelect.java:11: error: cannot find symbol
throws IOException, ServletException {
^
symbol: class ServletException
location: class BeerSelect
src\com\example\controllers\BeerSelect.java:3: error: package javax.servlet does not exist
import javax.servlet.*;
^
src\com\example\controllers\BeerSelect.java:4: error: package javax.servlet.http does not exist
import javax.servlet.http.*;
^
6 errors
However, javax.servlet-api-4.0.1 compiles the file successfully. Note: I've already tested and ruled out command-line command as a possible cause of the problem.
When I place the .class file in the corresponding Tomcat directory, start the server and try to interact with the app, I get the following exception:
Exception
jakarta.servlet.ServletException: Error instantiating servlet class [com.example.controllers.BeerSelect]
Root Cause
java.lang.NoClassDefFoundError: javax/servlet/http/HttpServlet
Root Cause
java.lang.ClassNotFoundException: javax.servlet.http.HttpServlet
I tried placing the javax.servlet-api-4.0.1 in the Tomcat/lib directory, but then I get:
Exception
jakarta.servlet.ServletException: Class [com.example.controllers.BeerSelect] is not a Servlet
Root Cause
java.lang.ClassCastException: class com.example.controllers.BeerSelect cannot be cast to class jakarta.servlet.Servlet (com.example.controllers.BeerSelect is in unnamed module of loader org.apache.catalina.loader.ParallelWebappClassLoader #7862f56; jakarta.servlet.Servlet is in unnamed module of loader java.net.URLClassLoader #4b4523f8)
Not sure the last makes any sense, but I ran out of ideas.
Any help is more than welcome!
When I place the .class file in the corresponding Tomcat directory, start the server and try to interact with the app, I get the following exception:
java.lang.ClassNotFoundException: javax.servlet.http.HttpServlet
I tried placing the javax.servlet-api-4.0.1 in the Tomcat/lib directory, but then I get:
java.lang.ClassCastException: class com.example.controllers.BeerSelect
cannot be cast to class jakarta.servlet.Servlet
The jakarta.servlet.Servlet is part of Servlet API version 5.0 which in turn is part of Jakarta EE version 9. Your servlet is actually extending from javax.servlet.Servlet which in turn is part of an older JEE version which is actually not supported by your target runtime (Tomcat 10.x).
You have 2 options:
Replace the javax.servlet.* imports in your code by jakarta.servlet.* ones.
import jakarta.servlet.*;
import jakarta.servlet.http.*;
Then you can just compile against the libraries from a Servlet 5.0 based target runtime.
Or, downgrade the servlet container from Servlet API version 5.0 to a previous version, at least the one still having the javax.servlet.* package name. Tomcat 9.x is the latest one still having the old package.
The technical reason is that during the step from Java/Jakarta EE 8 to Jakarta EE 9 all javax.* packages have been renamed to jakarta.* packages. So there is no backwards compatibility anymore since Jakarta EE 9.
So, when following any servlet tutorials which still use the old javax.servlet.* package while you're using Tomcat 10 or newer, then you need to manually swap out the package used in code examples for the jakarta.servlet.* one. Generally it'll work just fine.
See also:
Apache Tomcat - versions
Tomcat casting servlets to javax.servlet.Servlet instead of jakarta.servlet.http.HttpServlet (this answer contains complete examples of proper pom.xml declarations for Tomcat 10+, Tomcat 9-, JEE 9+ and JEE 8-, just in case you're using Maven).
I have tested the and managed to compile the java file on Apache Tomcat 9.0.39.
At this point in time, Apache Tomcat 10 is in development and there is no stable version:
http://tomcat.apache.org/whichversion.html
Downgrading your Apache Tomcat version should fix the problem.
Sree Kumar wrote this comment on another duplicate question which solved the problem for me:
Right-click on project -> Build Path -> Configure Build Path... -> Libraries (tab) -> Select Classpath -> Add Library -> Server Runtime -> Select the one you must have added for Tomcat 10.
Related
I have been reading posts in StackOverflow for almost 1 hour and no solutions worked for me.
I am doing a beginner Spring Course using Eclipse and JDK11. And I am having the following error in the terminal:
An error occurred during initialization of boot layer
java.lang.module.FindException: Module proyectoSpringUno not found
this is my java file:
package es.pildoras.IoC;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class UsoEmpleados {
public static void main(String[] args) {
ClassPathXmlApplicationContext contexto = new ClassPathXmlApplicationContext("applicationContext.xml");
Empleados Juan = contexto.getBean("miEmpleado",Empleados.class);
System.out.println(Juan.getTareas());
contexto.close();
}
}
I have an error on the import that says:
The type org.springframework.context.support.ClassPathXmlApplicationContext is not accessible
I know that this error happens because I am using the java module system and if I use java 8, all these issues will disappear, but I am not interested in that solution since I want to learn how to use Spring with newer Java JDKs.
At the moment, I have all my libs added into the Classpath but not into the Modulepath.
Solution I tried:
Adding the libs to the modulepath.
Result: this doesn't work. When I add the libs to the modulepath, Eclipse deletes the libs from the classpath ( probably to avoid duplicates). Now I don't have any issues in the module-info (eclipse inserted automodules) or in the import, but when I launch my app, I have the following error:
Error occurred during initialization of boot layer
java.lang.module.FindException: Unable to derive module descriptor for C:\Users\Stephane\Desktop\curso Spring\ProyectoSpringUno\libs\spring-context-indexer-5.3.6-sources.jar
Caused by: java.lang.module.InvalidModuleDescriptorException: Provider class org.springframework.context.index.processor.CandidateComponentsIndexer not in module
this is the module info:
module ProyectoSpringUno {
exports es.pildoras.IoC;
requires spring.context; // this is automaticly added by eclipse when i add libs to the modulepath
}
Resuming
When I add my dependencies to the modulepath, Eclipse don't work because he wants it in the classpath.
When I add my dependencies to the classpath, eclipse say I need to move some dependencies to the modulepath or the module-info will not work.
And eclipse don't let me add them to both modulepath and classpath because they are duplicates.
Any idea on how to fix this?
2 jars in WEB-INF/lib of Websphere 8.5.5.5 are having same directory structure
org.json.* .
json.jar Compiled withJDK 1.6
commonlib.jar compiled with JDK 1.7
Iam receiving below exception during the JVM startup in one environment while another environment has same jars and same classpath but there its working properly. WAS is running Java 1.6 due to which getting UnsupportedClassVersionError
6/23/16 15:59:32:091 CDT] 0000005e InjectionProc W CWNEN0047W: Resource annotations on the fields of the jmaki.xhp.XmlHttpProxyServlet class will be ignored. The annotations could not be obtained because of the exception : java.lang.UnsupportedClassVersionError: JVMCFRE003 bad major version; class=org/json/JSONObject, offset=6
When i specifically add the second jar to the class path of the second environment it gives the above error, otherwise it works properly Is it due to the classpath order?
class load: org.json.JSONException from: file:/hosting/configs/WebSphereD24/AppServer/installedApps/nodea/sear/swar/WEB-INF/lib/json.jar
class load: org.json.JSONObject$Null from: file:/hosting/configs/WebSphereD24/AppServer/installedApps/nodea/sear/swar/WEB-INF/lib/json.jar
How does WebSphere decide the jar to load the class=org/json/JSONException from . This class is present in both the jars... How come its loading from json.jar in one and common.jar in another.
The order of WEB-INF/lib JARs is unspecified, so the order will typically be defined by the file system. You should remove duplicate classes rather than trying to rely on any particular order.
I am using 12c web server. I make a .java file and compile it using jdk6_29 which is present in 12c. In java file I use annotation #WebServlet but when i compile it, it give errors class WebServlet not find...please tell me what is problem.
While running the application I was getting the following errors.
java.lang.NoClassDefFoundError: Could not initialize class org.apache.axis2.description.AxisService
AxisService class is in axis2-kernel-1.6.2.jar file. Some of the classes from this jar are working fine without any issues, but some classes are throwing NoClassDefFoundError from this jar file at runtime. AxisService class is present in axis2-kernel-1.6.2.jar, even it throws error.
This is working fine in local machine. But error getting in Oracle r12 server.I have already set the class path for the jar file.
I am Using Java version is 1.6 and Apache axis2.1.6.2.
Had the same issue. I had included only jars that I needed to compile the application.
When I included everything from \axis2-1.6.2\lib\ folder, this exception were gone.
I had a similar problem using Tomcat and Axis2 and after a week finding out the error I realized there was a axis configuration problem. especifically my aplication can't instantiate the class which accedded to persistence layer. I include this parameter line:
<parameter name="ServiceTCCL">composite</parameter>
in services.xml file which is used by axis2 to work.
For more information see the comments in http://wso2.com/node/1131
My compiler is not able to find the HttpServletRequest getServletContext() method.
I am not doing anything too complicated:
public static void setMySortedSet(HttpServletRequest request, SortedSet<String> set)
{
setMySortedSet(request.getServletContext(), set);
}
Some troubleshooting I have tried:
Discovered the method was created in 2.3, so I included a JAR that reflects that (and have it in my Eclipse build path)
I include the JAR in my build.xml classpath.
When I using Eclipse the method is found but when I try to build the classes I see this:
compile:
[javac] Compiling 1 source files to C:\...\workspace\proj\build\WEB-INF\classes
[javac] C:\...\workspace\proj\src\main\Helper.java:26: cannot find symbol
[javac] symbol : method getServletContext()
[javac] location: interface javax.servlet.http.HttpServletRequest
[javac] return getURISet(request.getServletContext());
[javac] ^
[javac] Note: C:\...\workspace\proj\src\main\Helper.java uses unchecked or unsafe operations.
[javac] Note: Recompile with -Xlint:unchecked for details.
[javac] 1 error
Any ideas of what I could be missing? I appreciate any responses.
The getServletContext() method is introduced in Servlet 3.0, not 2.3. But if you want to get the ServletContext then an alternative method to get it is:
ServletContext context = request.getSession().getServletContext();
if (username != "" & username != null ) {
context.setAttribute("savedUserName", username);
}
writer.println("Context Parameter : " + (String)context.getAttribute("savedUserName"));
This way you can get the stored Request Parameter Value in different browser....
According to the Javadoc the ServletRequest#getServletContext() method is introduced in Servlet 3.0, not 2.3. You need to install and integrate a Servlet 3.0 compatible container such as Tomcat 7, Glassfish 3, etc in Eclipse and set the Target Runtime of your Dynamic Web Project to that container. When you do that properly, then you do not need to manually fiddle with build paths or build.xml at all, Eclipse will handle it for you automatically. You also do not need to download loose JAR files of an arbitrary servletcontainer of a different make/version and put it in your buildpath. It would only lead to future classpath and portability troubles.
See also:
How do I import the javax.servlet API in my Eclipse project?
Maven dependency for Servlet 3.0 API?
I've had the same trouble recently. In fact it started happening after adding some new jars. Ant found HttpServletRequest class in selenium-server.jar which alphabetically comes first before servlet-api.jar (which was supposed to be used).
So i just renamed selenium-server.jar to x-selenium-server.jar and everything started building OK, as it used to.
This is not a problem with your java compiler. javax is provided by servlet container itself and you must include servlet container jar files to your project setup.
javax.servlet.http and all classes related servlet context and servlet programming is related to your Servlet Container only. So stop worrient about anything else and check if Tomcat libraries are being included in your WEB-APP class path.
If not add them and everything will be fine.
Right Click on your project > Properties > Add Libraries > Server Runtime
and choose your server that is associated with your application.
You are done, this will include Servlet Container libraries to your project and HttpServletRequest & HttpServletResponse classes will be resolved.
Hope it helps, more information about Servlet Architecture and context can be found Here.