ClassNotFoundException Using XWork2

274 Views Asked by At

I am trying to set up a pretty old library and mess around with it (XWork2) but I am running into the following error at runtime.

"C:\Program Files\Java\jdk-14.0.1\bin\java.exe" -Dfile.encoding=windows-1252 -jar C:\Users\dalto\Documents\code\poc\xwork-2-fun\target\xwork-2-fun-1.0.0.jar
Exception in thread "main" java.lang.NoClassDefFoundError: com/opensymphony/xwork2/DefaultActionProxyFactory
    at dalton.neely.Application.main(Application.java:16)
Caused by: java.lang.ClassNotFoundException: com.opensymphony.xwork2.DefaultActionProxyFactory
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
    ... 1 more

Process finished with exit code 1

Here is the pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://maven.apache.org/POM/4.0.0"
         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>dalton.neely</groupId>
    <artifactId>xwork-2-fun</artifactId>
    <version>1.0.0</version>

    <properties>
        <maven.compiler.source>14</maven.compiler.source>
        <maven.compiler.target>14</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/com.opensymphony/xwork -->
        <dependency>
            <groupId>com.opensymphony</groupId>
            <artifactId>xwork</artifactId>
            <version>2.1.3</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.2.0</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>dalton.neely.Application</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Here is the main class

package dalton.neely;

import com.opensymphony.xwork2.*;

import java.util.HashMap;
import java.util.Map;

public class Application {
    public static void main(String[] args){
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("id", "0123456789");

        Map<String, Object> context = new HashMap<>();
        context.put(ActionContext.PARAMETERS, paramMap);

        ActionProxy proxy = new DefaultActionProxyFactory().createActionProxy("", "viewBook", context);

        try {
            String result = proxy.execute();
            if("success".equals(result)){
                ViewBookAction action = (ViewBookAction) proxy.getAction();
                System.out.println(action.getBook().getTitle());
            } else if("notFound".equals(result)){
                System.out.println("The book was not found");
            } else {
                throw new RuntimeException("Im Lazy");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Here is the Action Class

package dalton.neely;

import com.opensymphony.xwork2.Action;

public class ViewBookAction implements Action {
    private Book book;
    private final String id;

    public ViewBookAction(Book book, String id) {
        this.book = book;
        this.id = id;
    }

    public String execute() throws Exception {
        this.book = null;
        if(book == null){
            return "notFound";
        } else {
            return "success";
        }
    }

    public Book getBook() {
        return book;
    }

    public String getId() {
        return id;
    }
}

And here is the book class

package dalton.neely;

import java.util.Set;

public class Book {
    private final String id;
    private final String title;
    private final Set<String> authors;

    public Book(String id, String title, Set<String> authors) {
        this.id = id;
        this.title = title;
        this.authors = authors;
    }

    public String getId() {
        return id;
    }

    public String getTitle() {
        return title;
    }

    public Set<String> getAuthors() {
        return authors;
    }
}

I am going off this page for this information https://svn.apache.org/repos/asf/struts/attic/xwork/trunk/docs/wikidocs/XWork%20Features.html

1

There are 1 best solutions below

0
Thiraviyakumar On

Dependent jar should be there in the package (or in classpath). Instead of using maven-jar-plugin, could you try with maven-assembly-plugin to build the jar-with-dependencies.