BJ Dela Cruz
BJ Dela Cruz

Reputation: 5354

Java RMI and NoClassDefFoundError

Update: Here is the project.

I have the following directory structure:

RMI
  |
  api.jar
        |--> org.examples.rmi.api.Pi
        |--> org.examples.rmi.api.Task
        |--> org.examples.rmi.api.Compute
  client.jar
        |--> org.examples.rmi.client.ComputePi
  server.jar
        |--> org.examples.rmi.server.ComputeEngine
        |--> org.examples.rmi.api.Pi
        |--> org.examples.rmi.api.Task
        |--> org.examples.rmi.api.Compute

I typed the following commands to start my server:

C:\Users\Public\RMI\server>set CLASSPATH=
C:\Users\Public\RMI\server>start rmiregistry
C:\Users\Public\RMI\server>java -Djava.rmi.server.codebase="file:/C:/Users/Public/RMI/compute.jar" -Djava.rmi.server.hostname=127.0.0.1 -Djava.security.policy=server.policy org.examples.rmi.engine.ComputeEngine

Then I typed this command to start my client:

C:\Users\Public\RMI>java -cp api.jar -jar -Djava.rmi.server.codebase="file:/C:/Users/Public/RMI/api.jar" -Djava.security.policy=client.policy client.jar 127.0.0.1 45

But I get the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: org/examples/rmi/api/Task
        at java.lang.Class.getDeclaredMethods0(Native Method)
        at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
        at java.lang.Class.getMethod0(Unknown Source)
        at java.lang.Class.getMethod(Unknown Source)
        at sun.launcher.LauncherHelper.getMainMethod(Unknown Source)
        at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.examples.rmi.api.Task
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 6 more

But if I add the three classes that are in the api package to the client.jar file, the client works.

I get the same NoClassDefFoundError if I remove the same three classes in the server.jar file, even though I set the classpath to api.jar.

C:\Users\Public\RMI>java -cp api.jar -jar -Djava.rmi.server.hostname=127.0.0.1 -Djava.rmi.server.codebase="file:/C:/Users/Public/RMI/api.jar" -Djava.security.policy=server.policy server.jar

I don't want to put the three classes in the api package in multiple places. I just want to keep them in one place, in the api.jar file. Am I not setting the classpath on the command line correctly?

Your advice will be greatly appreciated. Thanks!

Upvotes: 0

Views: 2154

Answers (1)

user207421
user207421

Reputation: 311031

The -cp argument is ignored when you use the -jar option. You should be using the Class-Path option in the Manifest of client.jar to connect client.jar to api.jar.

Upvotes: 2

Related Questions