Java jsonrpc4j jsonrpcServer未找到类定义错误(Eclipse)

bq9c1y66  于 2023-02-28  发布在  Java
关注(0)|答案(1)|浏览(161)

我想通过TCP创建一个基本的JSON-RPC服务器,为此我找到了项目https://github.com/briandilley/jsonrpc4j,当我试图使用jsonRpcServer在Eclipse中创建一个基本的Maven项目时(参见App.java),弹出了异常java.lang.NoClassDefFoundError。
App.java

package com.sxtlcfi.maven;
import com.googlecode.jsonrpc4j.JsonRpcServer;

public class App {
    
    public static void main(String args[]){
        
        // create the jsonRpcServer
        UserServiceImpl userService = new UserServiceImpl();
        
        // actual runtime
        String[] arrOfClassPath = System.getProperty("java.class.path").split(";");
        for (String a : arrOfClassPath) System.out.println(a);
        
        //Issue: https://code.google.com/archive/p/jsonrpc4j/issues/64
        JsonRpcServer jsonRpcServer1 = new JsonRpcServer(userService);
        //Exception in thread "main" java.lang.NoClassDefFoundError: javax/servlet/ServletOutputStream
        //at com.sxtlcfi.maven.App.main(App.java:60)
        //Caused by: java.lang.ClassNotFoundException: javax.servlet.ServletOutputStream
        //  at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
        //  at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
        //  at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
        //  ... 1 more
        
        //System.out.println(UserService.class.getMethods()[0].getName());
        //JsonRpcServer jsonRpcServer2 = new JsonRpcServer(userService, UserService.class);
    }
}

UserService.java

package com.sxtlcfi.maven;
public interface UserService {
    User createUser(String userName, String firstName, String password);
    User createUser(String userName, String password);
    User findUserByUserName(String userName);
    int getUserCount();
}

UserServiceImpl.java

package com.sxtlcfi.maven;

import java.util.ArrayList;

public class UserServiceImpl
    implements UserService {

    ArrayList<User> database = new ArrayList<User>();
    
    public User createUser(String userName, String firstName, String password) {
        User user = new User();
        user.setUserName(userName);
        user.setFirstName(firstName);
        user.setPassword(password);
        
        database.add(user);
        return user;
    }

    public User createUser(String userName, String password) {
        return this.createUser(userName, null, password);
    }

    public User findUserByUserName(String userName) {
        //return database.findUserByUserName(userName);
        for (int i = 0; i < database.size(); i++) {
            if(database.get(i).getUserName() == userName) {
                return database.get(i);
            }
        }
        return null;
    }

    public int getUserCount() {
        //return database.getUserCount();
        return database.size();
    }

}

User.java

package com.sxtlcfi.maven;

public class User {

    private String userName;
    private String firstName;
    private String password;
    
    public void setUserName(String userName) {
        this.userName = userName;
    }
    
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    
    public void setPassword(String password) {
        this.password = password;
    }
    
    public String getUserName() {
        return userName;
    }
    
}

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>
  <groupId>com.sxtlcfi.maven</groupId>
  <artifactId>maven-demo</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <dependencies>
    <!-- https://mvnrepository.com/artifact/junit/junit -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.13.2</version>
      <scope>test</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.github.briandilley.jsonrpc4j/jsonrpc4j -->
    <dependency>
      <groupId>com.github.briandilley.jsonrpc4j</groupId>
      <artifactId>jsonrpc4j</artifactId>
      <version>1.6</version>
    </dependency>
  </dependencies>
</project>
6tr1vspr

6tr1vspr1#

我遇到了完全相同的问题。通过向Gradle添加以下2个依赖项来解决这个问题:

// https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api
implementation("javax.servlet:javax.servlet-api:4.0.1")

// https://mvnrepository.com/artifact/javax.portlet/portlet-api
compileOnly("javax.portlet:portlet-api:3.0.1")

相关问题