4

I am trying to run a simple Struts2 application but it always throw the following exception:

java.lang.ClassNotFoundException: org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

I have followed the step by step tutorial in Create Struts 2 Web Application Using Maven To Manage Artifacts and To Build The Application, but I cannot make it work.

I also managed to find other Struts2 tutorial which uses maven but all failed. It throws the same exception when I try to run the application.

I used the following:

  • Eclipse
  • Maven
  • Tomcat
  • Struts2

According to other discussions, the dependencies jar files(e.g. struts2-core) must be put on the lib folder but as for me, I am using Maven so all the dependencies are already included in my build path under Maven Dependencies.

I even tried running the sample project given in Struts2 website (Basic_Struts_2_Maven_2_3_1_2.zip) but I still have no luck. Building it is successful but running it on server always fail.

Am I missing anything?

Here's the web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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">
<display-name>Basic_Struts2_Maven</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
     <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>manning</groupId>
    <artifactId>Basic_Struts2_Mvn</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>Basic Struts 2</name>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <build>
        <finalName>Basic_Struts2_Mvn</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>2.3.1.2</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.14</version>
        </dependency>
    </dependencies>
</project>

struts.xml:

<?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
    <struts>
        <constant name="struts.devMode" value="true" />
        <package name="basicstruts2" extends="struts-default">
            <action name="index">
                <result>/index.jsp</result>
            </action>
        </package>
    </struts>

.classpath:

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" output="target/classes" path="src/main/java"/>
    <classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources"/>
    <classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
    <classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources"/>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/>
    <classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/>
    <classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.web.container"/>
    <classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/>
    <classpathentry kind="output" path="target/classes"/>
</classpath>
16
  • did yop added jar files to classpath Commented Mar 21, 2013 at 10:48
  • can you show web.xml pls Commented Mar 21, 2013 at 10:49
  • libraries should be in the WEB-INF/lib Commented Mar 21, 2013 at 10:53
  • @RomanC Would it be okay to have the jars declared twice in my build path? It's already in my build path under Maven Dependencies right now. If I put it in my lib folder, it would have another entry in my build path. Commented Mar 21, 2013 at 11:18
  • 2
    @vida do you deploy from Eclipse? If so try to build war from cmd and then deploy it to tomcat - without Eclipse Commented Mar 21, 2013 at 15:18

3 Answers 3

4

Use this command to convert Maven project to Dynamic Web Project for Eclipse:

mvn eclipse:eclipse -Dwtpversion=3.4.2

you may change whatever WTP version you use

.classpath should be like this

<classpath>
  <classpathentry kind="src" path="src/main/java" including="**/*.java"/>
  <classpathentry kind="src" path="src/main/resources" excluding="**/*.java"/>
  <classpathentry kind="output" path="target/classes"/>
  <classpathentry kind="var" path="M2_REPO/asm/asm/3.3/asm-3.3.jar"/>
  <classpathentry kind="var" path="M2_REPO/asm/asm-commons/3.3/asm-commons-3.3.jar"/>
  <classpathentry kind="var" path="M2_REPO/asm/asm-tree/3.3/asm-tree-3.3.jar"/>
  <classpathentry kind="var" path="M2_REPO/commons-fileupload/commons-fileupload/1.2.2/commons-fileupload-1.2.2.jar" />
  <classpathentry kind="var" path="M2_REPO/commons-io/commons-io/2.0.1/commons-io-2.0.1.jar"/>
  <classpathentry kind="var" path="M2_REPO/commons-lang/commons-lang/2.5/commons-lang-2.5.jar"/>
  <classpathentry kind="var" path="M2_REPO/org/freemarker/freemarker/2.3.18/freemarker-2.3.18.jar"/>
  <classpathentry kind="var" path="M2_REPO/javassist/javassist/3.11.0.GA/javassist-3.11.0.GA.jar"/>
  <classpathentry kind="var" path="M2_REPO/junit/junit/3.8.1/junit-3.8.1.jar"/>
  <classpathentry kind="var" path="M2_REPO/ognl/ognl/3.0.4/ognl-3.0.4.jar"/>
  <classpathentry kind="var" path="M2_REPO/org/apache/struts/struts2-core/2.3.1.2/struts2-core-2.3.1.2.jar"/>
  <classpathentry kind="lib" path="C:/Program Files/Java/jdk1.6.0_32/lib/tools.jar"/>
  <classpathentry kind="var" path="M2_REPO/org/apache/struts/xwork/xwork-core/2.3.1.2/xwork-core-2.3.1.2.jar"/>
  <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
</classpath>

After build the WAR file should deploy to %CATALINA_BASE%\webapps.

Sign up to request clarification or add additional context in comments.

4 Comments

I am currently using a maven plugin for eclipse. I believe it configures the project so that it will run on Eclipse.
Did you try to do what I suggested?
@vida Eclipse Maven support is... crapy ;-)
@RomanC yup. I edited my .classpath file. It worked when using a command line :)
2

Dear this the part of Struts 2 Core 2.2.3.1 API.

And I tried to run you linked project it is properly working for me. there is no issue.

5 Comments

I've tried both the latest (2.3.12) and some of the other older versions but none of it worked so far.
As I told you the link you share is perfectly working you need to cross check you project for it.
May I know the versions of your Eclipse, Tomcat and Maven? Are you using a maven plugin for Eclipse or a stand alone maven? Thank you very much.
I am using Eclipse Juno and eclipse maven plugin (maven 2) and tomcat 6
I'm using m2e. It's the same as yours, I guess. I just used command line instead. Thanks anyways.
0

I think you specified like this

<dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-core</artifactId>
    <version>X.X.X.X</version>
</dependency>

try to add

<dependency>
     <groupId>org.apache.struts</groupId>
     <artifactId>struts2-core</artifactId>
     <version>2.1.6</version>
    </dependency>

3 Comments

@vida try to add the dependency i spwcified and let me know problem is solved or not
Why not to use latest Struts2 version.
@Aleksandr M no problem we can use.But to know the problem i gave that code.I have previously used that code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.