ServletException: No servlet class has been specified - java

I have what should be an extremely simple "Hello World" servlet, but I cannot get it to work. I am using Eclipse, Tomcat 8, Java 7, and Servlet 3.1.
I have looked at many tutorials and questions, but they have not completely helped. Most tutorials I have seen talk about creating servlets by extending HttpServlet. I got those to work. Now I'd like to try the cleaner annotation approach.
I've been referring to this tutorial, but it isn't quite complete and seems to have some incorrect or incomplete examples:
Packaging and Deploying RESTful Web Services
Why is com.testing.service.MyApplication not being loaded?
Any help on getting this thing to run would be immensely appreciated!
Here are my files:
MyApplication.java
package com.testing.service;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
#ApplicationPath("app")
public class MyApplication extends Application {
public Set<Class<?>> getClasses() {
Set<Class<?>> s = new HashSet<Class<?>>();
s.add(HelloWorldResource.class);
return s;
}
}
HelloWorldResource.java
package com.testing.service;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
#Path("/helloworld")
public class HelloWorldResource {
#SuppressWarnings("unused")
private static final long serialVersionUID = 1L;
#GET
#Produces("text/plain")
public String sayHello() {
return "Hello World!";
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>service</display-name>
<servlet>
<servlet-name>com.testing.service.MyApplication</servlet-name>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.testing.service.MyApplication</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
</web-app>
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.testing</groupId>
<artifactId>service</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<name>Rest Test</name>
<dependencies>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0.1</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Whenever I run Tomcat, it displays the following error:
INFO: Marking servlet com.testing.service.MyApplication as unavailable
Feb 05, 2015 3:28:55 PM org.apache.catalina.core.StandardContext loadOnStartup
SEVERE: Servlet /service threw load() exception
javax.servlet.ServletException: No servlet class has been specified for servlet com.testing.service.MyApplication
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1099)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1041)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4944)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5230)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1399)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)

I found the answer in the comments of a related question. The answer was provided by Alvin Thompson. Unfortunately, I don't have enough reputation to up-vote your answer.
If you're using a standard Tomcat install (or some other servlet
container), you need to include a REST implementation since Tomcat
doesn't come with one. If you're using Maven, add this to the
dependencies section:
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.bundles</groupId>
<artifactId>jaxrs-ri</artifactId>
<version>2.13</version>
</dependency>
...
</dependencies>
Then just add an application config class to your project. If you
don't have any special configuration needs aside from setting the
context path for the rest services, the class can be empty. Once this
class is added, you don't need to configure anything in web.xml (or
have one at all).
Originally posted here:
How to set up JAX-RS Application using annotations only (no web.xml)?

Related

Deploy servlet with IntelliJ IDEA to local Tomcat server

Trying to deploy simple servlet to Tomcat server. After select Run Tomcat... I'm redirected to http://localhost:8080/hi_war_exploded/ with webpage with one word - $END$. Logfiles reports no error.
I was expecting to see hw folder with my application in tc9\webapps, but found nothing.
What does the $END$ means? Where is my application on TomCat server? How to put my servlet to TomCat server?
Servlet:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
// Extend HttpServlet class
public class hw extends HttpServlet {
private String message;
public void init() throws ServletException {
// Do required initialization
message = "Hello World";
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Set response content type
response.setContentType("text/html");
// Actual logic goes here.
PrintWriter out = response.getWriter();
out.println("<h1>" + message + "</h1>");
}
public void destroy() {
// do nothing.
}
}
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>hw</servlet-name>
<servlet-class>hw</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hw</servlet-name>
<url-pattern>/hw</url-pattern>
</servlet-mapping>
</web-app>
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Hello</groupId>
<artifactId>hw</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>LATEST</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>LATEST</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
IntelliJ IDEA doesn't copy the webapp files into TOMCAT\webapps.
It modifies Tomcat configuration in CATALINA_BASE and deploys the artifact directly from its output directory to avoid copying the files which can take a lot of extra time, especially for the large projects.
hi_war_exploded is the context configured in Tomcat Run/Debug configuration, Deployment tab.
In the root of this context you have the default index.jsp page generated by IntelliJ IDEA on project creation.
When you open http://localhost:8080/hi_war_exploded/ URL, Tomcat serves index.jsp from the Web Resource root of your application.
$END$ is a part of the the new JSP file template. When you create a new JSP file in a project, cursor is placed at this location.
When the project wizard generates the Web Application project and places index.jsp file from the template, it doesn't expand the $END$ macro, so it appears in the JSP file. It's actually a known bug in IntelliJ IDEA.
Your servlet is available at http://localhost:8080/hi_war_exploded/hw URL.
To make it available at http://localhost:8080/hw URL instead you need to change the Application context to / as shown on this screenshot:

Java - IntelliJ Community Maven Web App

I'm trying out Java coming over from .NET and want to make a simple page that does the following:
Implements RESTful API
Connects to a MySQL Database
Implements Role-based Security
I tried out Eclipse but wasn't met with much success, plus the code completion was very slow compared to VS2017. So I'm trying out IntelliJ Community. I've built a Maven project, but my URL is not running, also it looks like I'm not getting any code completion in my .java files. It did not build the app directories automatically.
My POM:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.johnnytest.webapp</groupId>
<artifactId>jerseyexample</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>jerseyexample Maven Webapp</name>
<url>http://maven.apache.org</url>
<repositories>
<repository>
<id>snapshot-repository.java.net</id>
<name>Java.net Snapshot Repository for Maven</name>
<url>https://maven.java.net/content/repositories/snapshots/</url>
<layout>default</layout>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.26</version>
</dependency>
</dependencies>
<build>
<finalName>jerseyexample</finalName>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<url>http://localhost:8080/</url>
<!--<username>joe</username>-->
<!--<password>joe</password>-->
</configuration>
</plugin>
</plugins>
</build>
</project>
My web.xml:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>jersey-servlet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.johnnytest.app</param-value>
<load-on-startup>1</load-on-startup>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>jersey-servlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
My Java file:
package com.johnnytest.app;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
#Path("/test")
public class MainApp {
#GET
#Path("/{param}")
public Response getMessage(#PathParam("param") String message) {
String output = "Jersey says " + message;
return Response.status(200).entity(output).build();
}
}
Also it's running Tomcat 7, and I've installed 9 on my system. Is there any way to get it to run on my existing Tomcat install? When I go to any URL on localhost:8080 I just get a blank page in Safari. Running on OSX Sierra.
Launch at Command line
if you didn't do it already, download Apache Maven 3.5.0 and add the /bin directory to the system PATH variable.
Enter the directory where the pom.xml file is and run the following:
mvn clean package
mvn tomcat7:deploy
mvn tomcat7:start
The first command will build the code and produce a Java WAR archive in the subdirectory target/.
The second command will deploy the WAR archive to Tomcat application server.
The third command will start Tomcat and expose the server as per configuration (localhost:8080).
Don't worry about the command tomcat7, even if you're using Tomcat 9 it should be fine.
Launch within IntellJ
Maven management in IntelliJ is not really optimal, but on the right side you should see a Maven Projects pane (if you don't, select "Tools -> View tool buttons" menu). Click and expand.
Under Lifecycle you will find the standard Maven goals, so double clicking on clean and then package will produce the WAR file in a similar manner as explained in the previous section.
Under Plugins you will find the Maven goals ruled by the Tomcat plugin. Expand and double click tomcat7:deploy and tomcat7:start.
A quick parallel with ASP.NET
If you come from .NET, just think of Tomcat as the Java version of IISExpress that comes bundled with VisualStudio. It is a application server where you deploy the application code and which emulates a webserver service for development.
There are many parallels and also many discrepancies. In general, an application lifecycle in .NET is much more contained, you rarely need anything else beyond VisualStudio and MSBuild, I think. In Java, many things have been created and evolved through the decades thanks to the community: we have different build systems (Maven, Ant, Gradle), the best tooling is command line and there are multiple ways to achieve the same goal.
For example, you are using the Jersey REST library with an underlying app server below (Tomcat), but you could also deploy to a NIO server (Grizzly). If you use extended frameworks like Spring, you generally don't even worry about these things because switching from one way to the other is just a matter of importing a module in your pom.xml instead of another.
Hope this helped you and gave you some directions.

Spring routing gets 404 error because of invalid routing

For an assignment i'm practising Java's Spring MVC for creating a web application. I've build this whole project in the IDE Intellij Ultimate 2016.2.5.
I've created a Maven project for this, imported the correct and asked dependencies for this and build it.
The IDE build the following directory structure:
├───src
│ └───bas
│ └───animalkingdom
│ ├───config
│ ├───controllers
├───test
│ └───bas
│ └───animalkingdom
└───web
├───META-INF
├───resources
└───WEB-INF
└───pages
The config package is where my configuration class is, extending from WebMvcConfigurerAdapter:
package bas.animalkingdom.config;
import ...
#Configuration
#ComponentScan("bas.animalkingdom")
#EnableWebMvc
public class Config extends WebMvcConfigurerAdapter {
#Bean
public UrlBasedViewResolver setupViewResolver() {
UrlBasedViewResolver resolver = new UrlBasedViewResolver();
resolver.setPrefix("/WEB-INF/pages/");
resolver.setSuffix(".jsp");
resolver.setViewClass(JstlView.class);
return resolver;
}
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
}
I thought the #ComponentScan had to point to the main source directory where all your source files are.
I was told that I also need a class extending from the WebApplicationInitializer. I got this one from my school
package bas.animalkingdom.config;
import ...
public class WebInitializer implements WebApplicationInitializer {
#Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(Config.class);
ctx.setServletContext(servletContext);
ServletRegistration.Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
servlet.addMapping("/");
servlet.setLoadOnStartup(1);
}
}
This one is also in the config package.
The Config class is set as the Spring Application Context in the project structure settings in my IDE.
In the root directory is the web folder. In the folder WEB-INF is a empty web.xml file, which I was told I didn't need because the settings would be loaded via the configuration class. It looks like this:
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
</web-app>
In the root directory of the web folder is a index.jsp file.
In the bas.animalkingdom.controllers package are my controllers. For testing purposing, I only created one:
package bas.animalkingdom.controllers;
import ...
#Controller("AnimalC")
public class AnimalController {
#RequestMapping(value = "/animals", method = RequestMethod.GET)
public String getAnimals(ModelMap modelMap) {
Animal animal = new AfricanElephant(new Male(), "Body Covering", "Ename", " acolor", 123, 321);
modelMap.put("animal", animal);
return "animals";
}
}
With this controller I expected that I can go to the localhost/animals URL, and that it would load up the animals.jsp file located in my web\WEB-INF\pages\ package.
My code has no compile errors in it.
When I run my TomCat server, and open my browser to go to the localhost with the corresponding host, the index.jsp file just loads with no problem. This file is located in the web\ package.
When I go to the localhost:(port)/animals, I just get a 404 page, with the message that the page could not be found.
What does cause this? I've defined the controller which sets that route right?
Also, when looking up other Spring MVC tutorials, they all use a different packaging, does this work as well?
All your java classes and jsps are fine, problem is your module structure. There are basically 2 approaches to your problem, the conventional (recommended) and unconventional way. Let's start with the unconventional way, which is quicker and not recommended:
A. The Unconventional:
Add to your maven-war-plugin, the warSourceDirectory tag as shown below:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<warSourceDirectory>${basedir}/web</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
The disadvantage of using this approach is that other plugins may give you unexpected problems down the road, for example the maven-jetty-plugin will not run out of the box with this approach. It defaults to looking in src/main/webapp, although it is configurable. Maven life is easier if you follow the conventional approach.
B. Conventional Approach:
The solution lies in having a conventional maven structure for your module, make sure to do the following:
Delete Your web.xml file. If maven package failed because of missing web.xml file, add the below plugin in the build section of your pom.xml :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
Create folder structure /src/main/java and put all your java packages into this java directory. If your packages are not formatted properly, you can right click folder java and go to Mark Directory as -> Source Root.
Create folder structure /src/test/java and put all your test packages in the java directory.
Create a folder structure /src/main/webapp and put all the contents of folder web into the webapp directory.
After doing this you can test your application. You can try using jetty plugin to deploy your web app with the below configuration:
<build>
<finalName>spring-mvc</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.2.11.v20150529</version>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
<webApp>
<contextPath>/</contextPath>
</webApp>
<httpConnector>
<port>8080</port>
</httpConnector>
</configuration>
</plugin>
</plugins>
</build>

Maven Jetty Plugin - Servlets Aren't Working

I have the following servlet coded in /src/main/java/examples/web/SimpleServlet.java:
package examples.web;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SimpleServlet extends HttpServlet {
public void goGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
PrintWriter writer = res.getWriter();
writer.println("SimpleServlet Executed!");
writer.flush();
writer.close();
}
}
And the following web.xml defined in src/main/webapp/WEB-INF/web.xml:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>SimpleServlet</servlet-name>
<servlet-class>examples.web.SimpleServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SimpleServlet</servlet-name>
<url-pattern>/simpleservlet</url-pattern>
</servlet-mapping>
</web-app>
And the following pom.xml defined in /pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>examples.web</groupId>
<artifactId>simple-webapp</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>simple-webapp Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>simple-webapp</finalName>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
When I run mvn jetty:run I get output indicating that everything is working correctly. When I load localhost:8080/simple-webapp I am served src/main/webapp/index.jsp, so I know Jetty is at least partially working.
And now for the question: Although I am able to get to index.jsp, I cannot get to /simple-webapp/SimpleServlet -- I just get a 404 error from Jetty. Because of my configuration in web.xml, I was expecting to see SimpleServlet Executed! from SimpleServlet. What am I doing wrong?
Here is the relevant output from mvn jetty:run:
[INFO] Configuring Jetty for project: simple-webapp Maven Webapp
[INFO] Webapp source directory = C:\Users\Ken\Workspace\Eclipse\Maven\simple-webapp\src\main\webapp
[INFO] Reload Mechanic: automatic
[INFO] Classes = C:\Users\Ken\Workspace\Eclipse\Maven\simple-webapp\target\classes
2011-11-23 16:59:56.220:INFO::Logging to STDERR via org.mortbay.log.StdErrLog
[INFO] Context path = /simple-webapp
[INFO] Tmp directory = determined at runtime
[INFO] Web defaults = org/mortbay/jetty/webapp/webdefault.xml
[INFO] Web overrides = none
[INFO] web.xml file = C:\Users\Ken\Workspace\Eclipse\Maven\simple-webapp\src\main\webapp\WEB-INF\web.xml
[INFO] Webapp directory = C:\Users\Ken\Workspace\Eclipse\Maven\simple-webapp\src\main\webapp
The URLs are case-sensitive. The defined URL pattern does not match the value that you are requesting:
<url-pattern>/simpleservlet</url-pattern>
Try http://localhost:8080/simple-webapp/simpleservlet (lowercase servlet name).

Exception Deploying EJB Jar to Glassfish

This is my first foray into using JMS. I have a successfully created/deployed a war file that contains a servlet that I can use to upload files. When a file is uploaded it sends a message to a JMS queue. Next I wrote a listener to retrieve the uploaded messages from the queue, but when I try to deploy it, I get this error:
SEVERE: Invalid ejb jar [file-listener-ejb-1.0.jar]: it contains zero ejb.
Note:
1. A valid ejb jar requires at least one session, entity (1.x/2.x style), or message- driven bean.
2. EJB3+ entity beans (#Entity) are POJOs and please package them as library jar.
3. If the jar file contains valid EJBs which are annotated with EJB component level annotations (#Stateless, #Stateful, #MessageDriven, #Singleton), please check server.log to see whether the annotations were processed properly.
at com.sun.enterprise.deployment.util.EjbBundleValidator.accept(EjbBundleValidator.java:76)
...<snip>...
It's a very simple project with one class, built using Maven. The class looks like this:
package my.package;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
#MessageDriven(mappedName = "jms/FileUploadedQueue", activationConfig = {
#ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
#ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue") })
public class FileListener implements MessageListener
{
private static Logger log = LoggerFactory.getLogger(FileListener.class);
public FileListener()
{
// empty constructor
}
public void onMessage(Message message)
{
try
{
log.info("Received message: " + ((TextMessage)message).getText());
}
catch (JMSException ex)
{
String error = "Received error code '"
+ ex.getErrorCode()
+ "' retrieving message from queue jms/FileUploadedQueue.";
Exception linkedEx = ex.getLinkedException();
if (linkedEx != null)
{
log.error(error += "Linked exception: ", linkedEx);
}
else
{
log.error(error, linkedEx);
}
}
}
}
My pom.xml looks like this:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>my.package</groupId>
<artifactId>uploaded-file-listener</artifactId>
<version>1.0</version>
<packaging>ejb</packaging>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.4.2</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
<dependency>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
<version>1.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<compilerArguments>
<bootclasspath>${settings.localRepository}/javax/javaee-endorsed-api/6.0/javaee-endorsed-api-6.0.jar${path.separator}${sun.boot.class.path}</bootclasspath>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ejb-plugin</artifactId>
<version>2.3</version>
<configuration>
<ejbVersion>3.1</ejbVersion>
</configuration>
</plugin>
</plugins>
</build>
</project>
This builds a jar file which when I try to deploy to my Glassfish 3.1 server (via the admin console) results in the above error.
Since I have the #MessageDriven annotation on my class, I'm not sure what I'm doing wrong. Unfortunately, the server.log file does not contain any more details about the error.
Should I be packaging the jar in an ear and deploying that?
----------EDIT----------
I created an ear which includes the ejb jar, and I get the same error when I deploy the ear to Glassfish. So, I think it must be something to do with the annotation. However, I've looked at multiple examples/tutorials and I can't see what's wrong.
Any insights/suggestions would be most welcome!!
----------EDIT TWO----------
Contents of MANIFEST.MF files:
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: <name>
Build-Jdk: 1.6.0_24
Contents of application.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE application PUBLIC
"-//Sun Microsystems, Inc.//DTD J2EE Application 1.3//EN"
"http://java.sun.com/dtd/application_1_3.dtd">
<application>
<display-name>FileListener-ear</display-name>
<module>
<ejb>file-listener-ejb-1.0.jar</ejb>
</module>
</application>
----------EDIT THREE----------
Contents of ejb-jar file:
<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd"
version="3.1">
<display-name>FileListener</display-name>
<enterprise-beans>
<message-driven>
<ejb-name>FileListener</ejb-name>
<ejb-class>my.package.FileListener</ejb-class>
<transaction-type>Container</transaction-type>
</message-driven>
</enterprise-beans>
</ejb-jar>
Does your jar file contain an ejb-jar.xml file? If it was missing, then it could explain why the whole thing explodes upon deploy
Deploying on GF 4.1 our EAR that consists of an EJB and WAR and several JAR projects suddenly needs an application.xml.
Many deploys before it was not necessary.
Did you try to check the compatibility check box while deployment and see if deploys fine. We also got such error preventing the deployment and was resolved by checking the compatibility check box while deployment.

Categories

Resources