0

I have this problem with connecting my server and client with rmi in java.

Im using VSCode, client is maven project with tomcat and server is basic java project.

Error: "error unmarshalling return; nested exception is: java.lang.ClassNotFoundException: main.java.service.ProductService"

rmi interface on both sides is named ProductService.java and looks like this on server:

package main.java.service;

import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.ArrayList;

import main.java.data.Product;


public interface ProductService extends Remote {
    public ArrayList<Product> getAllProducts() throws RemoteException;
    public ArrayList<Product> getAllBurgers() throws RemoteException;
    public ArrayList<Product> getAllDrinks() throws RemoteException;
    public ArrayList<Product> getAllDeserts() throws RemoteException;
    public Product getProduct(int id) throws RemoteException;
    public void addProduct(Product product) throws RemoteException;
    public void updateProduct(Product product) throws RemoteException;
    public void deleteProduct(int id) throws RemoteException;
}

ProductService.java on client:

 package service;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.ArrayList;
import obj.Product;

public interface ProductService extends Remote {
    public ArrayList<Product> getAllProducts() throws RemoteException;
    public Product getProduct(int id) throws RemoteException;
    public void addProduct(Product product) throws RemoteException;
    public void updateProduct(Product product) throws RemoteException;
    public void deleteProduct(int id) throws RemoteException;
}

On client im trying a lookup on this servlet and then the error shows up:

package servlety;
import java.io.*;
import java.rmi.Naming;
import java.rmi.*;
import java.rmi.RMISecurityManager;
import java.rmi.Remote;
import java.util.ArrayList;
import service.ProductService;

import jakarta.servlet.*;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.*;
import obj.Product;

@WebServlet("/CategoryServlet2")
public class CategoryServlet extends HttpServlet {
     ArrayList<Product> productList;
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
        String category = request.getParameter("category");

         try {
      if (System.getSecurityManager() == null) {
System.setSecurityManager(new RMISecurityManager());
}
  ProductService productService = (ProductService) Naming.lookup("rmi://localhost:8585/restaurace");

                productList = productService.getAllProducts();

            request.setAttribute("productList", productList);

        } catch (Exception e) {
                    System.out.println(e.getMessage());
                }

        request.setAttribute("category", category);

        request.getRequestDispatcher("/"+category+".jsp").forward(request, response);
    }
}

on server side in implementation there is ProductServiceImpl.java and it looks like this:

 package main.java.data;

import java.rmi.*;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.*;  
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import main.java.service.ProductService;
import main.java.data.Product;
public class ProductServiceImpl extends UnicastRemoteObject  implements ProductService {

    public static void main(String[] args) {
     try{
              if (System.getSecurityManager() == null) {
System.setSecurityManager(new RMISecurityManager());
}
            Registry registry = LocateRegistry.createRegistry(8585);
            ProductServiceImpl productService = new ProductServiceImpl();
            registry.rebind("restaurace", (ProductService) productService);
            
            System.out.println("Server started");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private Connection connection;
    public ProductServiceImpl() throws RemoteException {
        super();
  
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/restaurace", "root", "");
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
    }
    
    @Override
    public Product getProduct(int id) throws RemoteException {
        ProductDAO productDao = new ProductDAO(connection);
        Product product = null;
        try {
            product = productDao.getProduct(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return product;
    }
    
    @Override
    public void addProduct(Product product) throws RemoteException {
        ProductDAO productDao = new ProductDAO(connection);
        try {
            productDao.addProduct(product);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    
    @Override
    public void updateProduct(Product product) throws RemoteException {
        ProductDAO productDao = new ProductDAO(connection);
        try {
            productDao.updateProduct(product);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    
    @Override
    public void deleteProduct(int id) throws RemoteException {
        ProductDAO productDao = new ProductDAO(connection);
        try {
            productDao.deleteProduct(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    
    @Override
    public ArrayList<Product> getAllProducts() throws RemoteException {
        try {
            ProductDAO productDAO = new ProductDAO(connection);
            return productDAO.getAllProducts();
        } catch (Exception e) {
            // TODO: handle exception
            return null;
        }
    }
}

ProductService on both sides is in these packages:

\Desktop\pokladna1\pokladna\src\main\java\service\ProductService.java - client
\Desktop\pokladna1\RMI server\src\main\java\service\ProductService.java - server
ezzky
  • 1
  • 1
  • It has to be not only named the same but be in the same package (which is a part of naming). Use a shared project or shared folder, don't repeat the source code. That is a recipe for divergence. – user207421 Jun 27 '23 at 05:17

0 Answers0