Java RMI - ClassNotFound exception - java

I'm creating a Java RMI program, but I'm getting a ClassNotFoundException.
Could you guys help me figure it out? I'm using Eclipse.
Somebody suggested me it was a codebase problem, but how does this relate?
Here are the codes for my Server and Client:
Server:
package server;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import base.Server;
import base.RmiStarter;
public class ServerImplStarter extends RmiStarter{
public ServerImplStarter() {
super(Server.class);
}
#Override
public void doCustomRmiHandling() {
try{
Server engine = new ServerImpl();
Server engineStub = (Server)UnicastRemoteObject.exportObject(engine, 0);
Registry registry = LocateRegistry.createRegistry( 1099 );
registry = LocateRegistry.getRegistry();
registry.rebind("Server", engineStub);
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args){
new ServerImplStarter();
}
}
Client:
package client;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import base.RmiStarter;
import base.Server;
import base.Cell;
public class CellClient extends RmiStarter {
public CellClient() {
super(Server.class);
}
#Override
public void doCustomRmiHandling() {
try{
Registry registry = LocateRegistry.getRegistry();
Server server = (Server)registry.lookup("Server");
Cell c = null;
c = server.getcell();
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) {
new CellClient();
}
}
and the error is this:
java.rmi.UnmarshalException: error unmarshalling return; nested exception is:
java.lang.ClassNotFoundException: server.CellImpl
at sun.rmi.server.UnicastRef.invoke(Unknown Source)
at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(Unknown Source)
at java.rmi.server.RemoteObjectInvocationHandler.invoke(Unknown Source)
at $Proxy0.getcell(Unknown Source)
at client.CellClient.doCustomRmiHandling(CellClient.java:23)
at base.RmiStarter.<init>(RmiStarter.java:19)
at client.CellClient.<init>(CellClient.java:13)
at client.CellClient.main(CellClient.java:31)
Caused by: java.lang.ClassNotFoundException: server.CellImpl
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 java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at sun.rmi.server.LoaderHandler.loadClass(Unknown Source)
at sun.rmi.server.LoaderHandler.loadClass(Unknown Source)
at java.rmi.server.RMIClassLoader$2.loadClass(Unknown Source)
at java.rmi.server.RMIClassLoader.loadClass(Unknown Source)
at sun.rmi.server.MarshalInputStream.resolveClass(Unknown Source)
at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
at java.io.ObjectInputStream.readClassDesc(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at sun.rmi.server.UnicastRef.unmarshalValue(Unknown Source)
... 8 more

Both client and server should have same package name. I had the same error yesterday, i corrected it after lots of searching. Try it and tell me if any other error comes.
Mark this as answer, if you find so.Thank You !!

CellImpl has not been exported or made available via the client's CLASSPATH. It needs to either (a) extend UnicastRemoteObject or be exported via UnicastRemoteObject.exportObject(); or (b) be Serializable and available on the client's CLASSPATH.

Solution for NoClassFound Exception when running RMI Client.
Case:
The server and client files are in different folders.
Example:
Server side: The server interface and server implementation are inside
Project Folder: C:\RMIServer
Package: rmiserver
Client side: The server interface and the client are inside
Project Folder: C:\RMIClient
Package: rmiClient
Problem: Could not find the location of the server interface.
Solution: In the client side,
1. Create a package name rmiServer (the package name should be the same as the server side package).
2. Place the server stub inside this package.
3. The client.java is inside the rmiClient package. Now import the rmiServer package in the client.java file.
import rmiServer.*;
This will solve the classNotFound Exception that happens when we execute the client side RMI.

Related

Connect to Athena using JDBC in a maven project

I'm trying to connect to the Amazon Athena, using jdbc in a maven project, but an exception is being raised. I think that the class is not being found. In Athena's guide it says:
Set the JDBC property, aws_credentials_provider_class, equal to the class name, and include itin your classpath. (1)
Since I'm using eclipse, I thought that the class would be already in the classpath, but apparently not. I tested the code in a simple java project (not maven) and it worked.
AmazonCredentialsProvider.java:
package athena;
import com.amazonaws.auth.AWSCredentials;
public class AmazonCredentials implements AWSCredentials {
#Override public String getAWSAccessKeyId() { return "..."; }
#Override public String getAWSSecretKey() { return "..."; }
}
AmazonCredentialsProvider.java:
package athena;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSCredentialsProvider;
public class AmazonCredentialsProvider implements AWSCredentialsProvider {
#Override public AWSCredentials getCredentials() { return new AmazonCredentials(); }
#Override public void refresh() {}
}
The code snippet of the connection :
Class.forName("com.amazonaws.athena.jdbc.AthenaDriver");
Properties properties = new Properties();
properties.setProperty("user", user);
properties.setProperty("password", password);
properties.setProperty("aws_credentials_provider_class", "athena.AmazonCredentialsProvider");
Connection connection = DriverManager.getConnection("jdbc:awsathena://athena." + region + ".amazonaws.com:443", properties);
The exception:
java.sql.SQLException: Failed to load AWS credentials provider class:
athena.AmazonCredentialsProvider at
com.amazonaws.athena.jdbc.AthenaDriverPropertiesFactory.lambda$parseProperties$2(AthenaDriverPropertiesFactory.java:52)
at
com.amazonaws.athena.jdbc.AthenaDriverPropertiesFactory.setClientConfigProperty(AthenaDriverPropertiesFactory.java:159)
at
com.amazonaws.athena.jdbc.AthenaDriverPropertiesFactory.parseProperties(AthenaDriverPropertiesFactory.java:40)
at
com.amazonaws.athena.jdbc.AthenaDriver.connect(AthenaDriver.java:110)
at java.sql.DriverManager.getConnection(Unknown Source) at
java.sql.DriverManager.getConnection(Unknown Source) at
athena.Athena.(Athena.java:94) at
services.GraphService.verify(GraphService.java:194) at
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at
java.lang.reflect.Method.invoke(Unknown Source) at
org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory.lambda$static$0(ResourceMethodInvocationHandlerFactory.java:76)
at
org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:148)
at
org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:191)
at
org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$VoidOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:183)
at
org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:103)
at
org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:493)
at
org.glassfish.jersey.server.model.ResourceMethodInvoker.lambda$apply$0(ResourceMethodInvoker.java:405)
at
org.glassfish.jersey.server.ServerRuntime$AsyncResponder$2$1.run(ServerRuntime.java:843)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:272) at
org.glassfish.jersey.internal.Errors$1.call(Errors.java:268) at
org.glassfish.jersey.internal.Errors.process(Errors.java:316) at
org.glassfish.jersey.internal.Errors.process(Errors.java:298) at
org.glassfish.jersey.internal.Errors.process(Errors.java:268) at
org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:289)
at
org.glassfish.jersey.server.ServerRuntime$AsyncResponder$2.run(ServerRuntime.java:838)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown
Source) at java.util.concurrent.FutureTask.run(Unknown Source) at
java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at
java.lang.Thread.run(Unknown Source) Caused by:
java.lang.ClassNotFoundException: athena.AmazonCredentialsProvider at
java.net.URLClassLoader.findClass(Unknown Source) at
java.lang.ClassLoader.loadClass(Unknown Source) at
java.lang.ClassLoader.loadClass(Unknown Source) at
java.lang.Class.forName0(Native Method) at
java.lang.Class.forName(Unknown Source) at
com.amazonaws.athena.jdbc.AthenaDriverPropertiesFactory.lambda$parseProperties$2(AthenaDriverPropertiesFactory.java:44)
... 31 more
Does anyone know how to solve it?
Are you using the latest version of the JDBC driver?
If yes, try to follow these instructions:
https://docs.aws.amazon.com/redshift/latest/mgmt/configure-jdbc-connection-with-maven.html

RMI with different PCs

I am learning RMI and I made a basic program that uses codebase.
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
public class Server implements sInterface,s2int {
public void go()
{
System.out.println("GO");
}
public void doIt()
{
}
public static void main(String[] args)
{
if(System.getSecurityManager()==null)
{
System.setSecurityManager(new SecurityManager());
}
try
{
System.setProperty("java.rmi.server.hostname","helios");
String s = "SERVER";
Registry r = LocateRegistry.getRegistry();
sInterface stub = (sInterface) UnicastRemoteObject.exportObject(new Server(),0);
r.rebind(s,stub);
}catch(Exception x){x.printStackTrace();}
}
}
Client:
public class Client {
public static void main(String[] args)
{
if(System.getSecurityManager()==null)
{
System.setSecurityManager(new SecurityManager());
}
try{
String name = "SERVER";
Registry r = LocateRegistry.getRegistry(args[0]);
sInterface inf = (sInterface)r.lookup(name);
inf.go();
}catch(Exception x)
{
x.printStackTrace();
}
}
}
Client does not have 's2int' interface and that is downloaded from the codebase.
The commands used to start the server and the client modules are as follows:
java -Djava.rmi.server.codebase=http://helios/~owner/rmi.jar
-Djava.security.policy=server.policy Server
java -Djava.security.policy=client.policy
-Djava.rmi.server.codebase=http://helios/~owner/
-Djava.rmi.server.hostname=helios Client localhost
Now,this works when both the server and client are on the same PC, but when I tried running it on a different PC on the same network, I got a
java.rmi.ConnectException: Connection refused to host: localhost; nested exception is:
java.net.ConnectException: Connection refused: connect
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(Unknown Source)
at sun.rmi.transport.tcp.TCPChannel.createConnection(Unknown Source)
at sun.rmi.transport.tcp.TCPChannel.newConnection(Unknown Source)
at sun.rmi.server.UnicastRef.newCall(Unknown Source)
at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
at Client.main(Client.java:20)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at
sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(Unknown S
ource) at
sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(Unknown S
ource)
... 6 more
I am not very knowledgeable when it comes to networking. Can anyone explain why it's not working?
Your client is looking up the wrong Registry. It needs to lookup the Registry at the server host, not its own localhost.

Websphere administration tool

I'm trying to create an administrative client program for websphere,
but when I'm trying to connect I get the following message.
Maybe I lack some libs (I create my app in notepad).
at TryConnection1.main(TryConnection1.java:37) Caused by: java.lang.ClassNotFoundException: com.ibm.websphere.security.auth.WSL oginFailedException
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)
... 1 more
My code:
import java.util.Properties;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.*;
import com.ibm.websphere.management.*;
import com.ibm.websphere.management.AdminClient;
import com.ibm.websphere.management.AdminClientFactory;
import com.ibm.websphere.management.exception.*;
import com.ibm.websphere.management.exception.ConnectorException;
public class TryConnection1 {
/** * #param args */
public static void main(String[] args) {
Properties connectProps = new Properties();
connectProps.setProperty(AdminClient.CONNECTOR_TYPE, AdminClient.CONNECTOR_TYPE_SOAP);
connectProps.setProperty(AdminClient.CONNECTOR_HOST, "hostgoeshere");
connectProps.setProperty(AdminClient.CONNECTOR_PORT, "portgoeshere");
connectProps.setProperty(AdminClient.USERNAME, "usernamegoeshere");
connectProps.setProperty(AdminClient.PASSWORD, "passgoeshere");
AdminClient adminClient = null;
try {
adminClient = AdminClientFactory.createAdminClient(connectProps);
} catch(ConnectorException e) {
System.out.println("Exception creating admin client: " + e); }
}
}
try to add $WEBSPHERE_HOME/AppServer/runtimes/com.ibm.ws.admin.client_8.5.0.jar, or similar if you're using a different WebSphere version, to your classpath. This is the required jar for WebSphere Admin Client.
You should try to add:
import com.ibm.websphere.security.auth.*;

Possible causes of "java.lang.ClassFormatError: Invalid pc in LineNumberTable"

Today I started coding a project that uses sqlite and when I tried to test it I received the java.lang.ClassFormatError: Invalid pc in LineNumberTable. Hope you can help me with it, because I'm just lost. I'v searched about this error, and came across some weird solutions to some weird causes. As I understand, none of them was causing my problem.
I'm using "sqlitejdbc-v056" wich is inserted in my classpath.
Edit: My JDK version is 1.7.0_03-b05
StackTrace:
Exception in thread "main" java.lang.ClassFormatError: Invalid pc in LineNumberTable in class file Controllers/FuncionariosController
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
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)
at Main.main(Main.java:13)
Code giving the error:
public class Main {
public static void main(String[] args) {
DatabaseController db=new DatabaseController();
FuncionariosController f=new FuncionariosController(db);
...
}
}
Class FuncionarioController:
package Controllers;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import Models.Funcionario;
public class FuncionariosController {
private DatabaseController db;
public FuncionariosController(DatabaseController db){
this.db=db;
}
public void render(String cargo){
...
}
public List<Funcionario> selectAll(){
...
}
public Funcionario select(String login){
...
}
}
Thanks in advance,
I had a simmilar problem.
It occured that it was caused by switch that haven't got cases code.
While the error occured it looked somtheing like this:
switch(e.getActionCommand()) {
case ACTION_OPEN_PID:
break;
case ACTION_OPEN_PPID:
break;
default:
}
After adding simple console output to the cases the error was gone.
switch(e.getActionCommand()) {
case ACTION_OPEN_PID:
System.out.println(ACTION_OPEN_PID);
break;
case ACTION_OPEN_PPID:
System.out.println(ACTION_OPEN_PPID);
break;
default:
}
Thanks to Luiggi Mendoza I solved it by using JDK 1.6 instead of 1.7. That did the trick, but I had to comment out a lot of code to compile it. Not that good a solution....

ServerException is being thrown while binding RMI service

I'm trying to build simple echo server as RMI application (step by step as it's described in Sun tutorial).
All source code is shown below, it's very simple:
package test;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface EchoServer extends Remote {
public String sendString (String str) throws RemoteException;
}
package test;
import java.rmi.server.UnicastRemoteObject;
import java.rmi.RemoteException;
public class EchoServerImpl extends UnicastRemoteObject implements EchoServer {
public EchoServerImpl () throws RemoteException {
super ();
}
public String sendString (String str) throws RemoteException {
return str.toUpperCase();
}
}
package test;
import java.rmi.Naming;
public class RMIServer {
public RMIServer () {
try {
EchoServer eServer = new EchoServerImpl ();
Naming.rebind ("rmi://localhost:1099/EchoService", eServer); // <--exception
} catch (Exception e) {
e.printStackTrace ();
}
}
public static void main(String[] args) {
new RMIServer ();
}
}
I compile code with rmic and I see file EchoServerImpl_Stub.class. After that I run 'rmiregistry' and then I start RMIServer. And at that moment I see ServerException:
java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is:
java.lang.ClassNotFoundException: test.EchoServerImpl_Stub
at sun.rmi.server.UnicastServerRef.oldDispatch(Unknown Source)
at sun.rmi.server.UnicastServerRef.dispatch(Unknown Source)
at sun.rmi.transport.Transport$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:255)
at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:233)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:359)
at sun.rmi.registry.RegistryImpl_Stub.rebind(Unknown Source)
at java.rmi.Naming.rebind(Naming.java:160)
at test.RMIServer.(RMIServer.java:10)
at test.RMIServer.main(RMIServer.java:17)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:90)
Caused by: java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is:
java.lang.ClassNotFoundException: test.EchoServerImpl_Stub
at sun.rmi.registry.RegistryImpl_Skel.dispatch(Unknown Source)
at sun.rmi.server.UnicastServerRef.oldDispatch(Unknown Source)
at sun.rmi.server.UnicastServerRef.dispatch(Unknown Source)
at sun.rmi.transport.Transport$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: test.EchoServerImpl_Stub
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 java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at sun.rmi.server.LoaderHandler.loadClass(Unknown Source)
at sun.rmi.server.LoaderHandler.loadClass(Unknown Source)
at java.rmi.server.RMIClassLoader$2.loadClass(Unknown Source)
at java.rmi.server.RMIClassLoader.loadClass(Unknown Source)
at sun.rmi.server.MarshalInputStream.resolveClass(Unknown Source)
at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
at java.io.ObjectInputStream.readClassDesc(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
... 12 more
I think it's a common problem and it's probably connected with wrong deploying. How to fix it?
I think it's your rmiregistry classpath. The test.EchoServer interface needs to be accessible to the rmiregistry itself. Set your CLASSPATH environment variable before you run up the rmiregistry e.g.
C:\>set CLASSPATH="MyBinFolder"
C:\>rmiregistry
Also, rmic is a hangover from before Java 5. It isn't necessary to run it anymore. Just run your server and client as normal and the proxies will be created dynamically.
Regards,
MK

Categories

Resources