java.rmi.ServerException: RemoteException occurred in server thread (ClassNotFoundException)
Asked Answered
T

5

13

The following method:

    private void startServer() { // snippet that starts the server on the local machine
        try {
             RemoteMethodImpl impl = new RemoteMethodImpl();
             Naming.rebind( "Illusive-Server" , impl );
        } catch(Exception exc) {
            JOptionPane.showMessageDialog(this, "Problem starting the server", "Error", JOptionPane.ERROR_MESSAGE);
            System.out.println(exc);
        }
    }

throws this exception:

java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
        java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is:
        java.lang.ClassNotFoundException: Interfaces.RemoteMethodIntf

When i start my project, i am greeted with the message in JOptionPane saying problem starting the server and then the above exception. What could be the reason for this?

I don't understand why does the last statement of exception says class not found exc when i have imported the right packages.

Taeniasis answered 2/3, 2012 at 9:52 Comment(0)
A
15

There are four cases of this exception.

  1. When exporting: you didn't run 'rmic' and you didn't take the steps described in the preamble to the Javadoc for UnicastRemoteObject to make it unnecessary.

  2. When binding: the Registry doesn't have the stub or the remote interface or something they depend on on its classpath.

  3. when looking up: the client does't have these things on its classpath.

  4. When calling a remote method: you either sent something to the server of a class not present on its CLASSPATH, or received something from the server (including an exception) of a class not on your CLASSPATH: in both cases possibly a derived class or interface implementation of a class or interface mentioned in the remote interface's method signature.

This is case 2. The Registry can't find the named class.

There are several solutions:

  1. Start the Registry with a CLASSPATH that includes the relevant JARs or directories.

  2. Start the Registry in your server JVM, via LocateRegistry.createRegistry().

  3. Use dynamic stubs, as described in the preamble to the Javadoc of UnicastRemoteObject. However you may then still run into the same problem with the remote interface itself or a class that it depends on, in which case 1-3 above still apply to that class/those classes.

  4. Ensure that case (4) above doesn't occur.

  5. Use the codebase feature. This is really a deployment option and IMO something to be avoided at the initial development stage.

Acinus answered 3/3, 2012 at 1:10 Comment(5)
@SuhailGupta See the preamble to java.rmi.server.UnicastRemoteObject. You only need to generate a stub class if you are using Java < 1.5 or you aren't following those guidelines. As you got that exception, clearly one of those two is true.Acinus
I am using java > 1.5 . And my class extends UnicastRemoteObject . Am i somewhat outdated. Here is the small code that i have usedTaeniasis
@SuhailGupta See the documentation I cited. As long as you quote a port number, even zero, when exporting, you don't need a stub.Acinus
@EJP I was able to get mine working by using setting the CLASSPATH using export CLASSPATH=myLib.jar:myLib2.jar. before launching the rmiregistry. But I am working on a production deployment and I noticed your comment on another post about CLASSPATH being for development and a codebase configuration is more suitable for deployment. Is your understanding that CLASSPATH should be cleared for deployment and all server and clients using rmi will use code base in a deployment configuration?Geometrician
@Geometrician That's not what I said at all. I said nothing about CLASSPATH being 'for development', and I said that the codebase feature is a deployment option and shouldn't hold up development. I'm not really a fan of it but it does have its occasional uses. But most RMI systems don't need those uses, and can just use the CLASSPATH.Acinus
R
5
Remote Server Error:RemoteException occurred in server thread; nested exception is:
java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is:
java.lang.ClassNotFoundException: mathInterface

The error very simple to solve to be perform following steps:

  • For example your java file consider D drive
  • Start rmiregistry D drive( example D:\start rmiregistry)then don't start rmiregistry on the other drives, it will yield the above error

(Wherever your file is, start rmiregistry)

Roundel answered 12/9, 2012 at 16:49 Comment(2)
wow, this actually worked for me... though i don't know why? can anyone explain what's happening here?Tuckie
@Tuckie See my answer. This one works because it puts the required class on the Registry's CLASSPATH, but only if that class isn't in a package. If it was, it wouldn't work. -1 for lack of explanation and special case solution.Acinus
R
1

I will try to explain it as better as possible what I did: 1st. I declared the classpath variable like follow:

  1. set classpath=%classpath%
  2. set classpath=C:\compiler
  3. set classpath=C:\compiler\libro\cap07\rmi\hello\Hello.java
  4. set classpath=C:\compiler\libro\cap07\rmi\hello\Server.java
  5. set classpath=C:\compiler\libro\cap07\rmi\hello\Client.java
  • (All in one lineset:

set classpath=%classpath%;C:\compiler;C:\compiler\libro\cap07\rmi\hello\Hello.java;C:\compiler\libro\cap07\rmi\hello\Server.java;C:\compiler\libro\cap07\rmi\hello\Client.java)

  • (I'm not sure if the .java files were nesesary, but I also wrote them for doubts).

2nd. I compilered with the line javac -d C:\compiler Hello.java Server.java Client.java. Where C:\compiler is the root directory like src on Eclipse IDE.

3rd. I ran the start rmiregistry line. (and don´t matter where you run it, it's the same).

4th. I ran:

start java -classpath C:\compiler -Djava.rmi.server.codebase=file:C:\compiler/ libro.cap07.rmi.hello.Server

You already know C:\compiler, but you need define packages address on the last to that the command can find the .class files. Open any .java file and copy the package address without packages sentense. You will see when you open the src directory (in my case C:\compiler), you find all directory sequence created. When this command line is created correctly, no matter where you will run it, C:, D:, src, anywhere it wil run.

5th. And finally, I ran the Client class with:

java -classpath C:\compiler libro.cap07.rmi.hello.Client

In conclusion, if the classpath variable won't created or it's to created wrong or the sentence of 4th point is not addressed well the JVM throws the same or similar error. Search there!

(Sorry my english).

Rempe answered 9/7, 2020 at 1:58 Comment(1)
You don't put .java files into the CLASSPATH. You put directories containing package-structures of .class files, or JAR files. Evidently all you really needed was C:\compiler.Acinus
T
-1

You can launch rmiregistry from anywhere but you have to make sure that the compiled classes are already in your classpath. For example:-

E:\ARMSRemoteUpdater\WebContent\WEB-INF\classes>set classpath=%classpath%;E:\ARMSRemoteUpdater\WebContent\WEB-INF\classes <ENTER>

E:\ARMSRemoteUpdater\WebContent\WEB-INF\classes>c: <ENTER>

C:\>rmiregistry

And the above should work fine.

In general, if you launch rmiregistry from the root location of the compiled classes (above example it is E:\ARMSRemoteUpdater\WebContent\WEB-INF\classes), that will work because . (dot - current directory) is already set in your classpath.

But as soon as you remove . (dot - current directory) from your classpath, the above working condition will also fail.

Hope I have explained in details.

Tophole answered 2/6, 2014 at 11:46 Comment(0)
A
-1

I got this same issue, and a different solution worked for me. I was running two different IntelliJ projects, with a copy of the interface in each project. One of them was in a package, and the other one wasn't, and that was what was causing this error.

Solutions:

  • Make sure the interface copies aren't in a package.
  • Make sure the interface copies have the exact same package name.
Agave answered 14/2, 2019 at 19:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.