Java RMI ServerException - java.lang.ClassNotFoundException: org.prog.rmi.RmiServer_Stub - java

I have inherited some Java RMI client/server code, and while it runs fine on one machine, I haven't been able to get it to run in my dev environment.
The problem is when I run the server using the following java.exe -Djava.security.policy=conf\server.policy -SRC;. -Djava.library.path=. org.prog.rmi.RmiServer
I get the following error:
java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is:
java.lang.ClassNotFoundException: org.prog.rmi.RmiServer_Stub (no security manager: RMI class loader disabled)
at sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:396)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:250)
at sun.rmi.transport.Transport$1.run(Transport.java:159)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:155)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:535)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:790)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:649)
...
My server.policy file is
grant {
permission java.security.AllPermission;
};
And my java code:
package org.prog.rmi;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.RMISecurityManager;
import java.rmi.server.UnicastRemoteObject;
import java.rmi.registry.*;
public class RmiServer extends UnicastRemoteObject
implements RmiServerIntf {
private BatchApi bapi;
private String iniFileLocation;
private String layoutOption;
private int addressCount = 0;
private RefInt apiHandle = new RefInt();
public RmiServer(String iniFileLocation,String layoutOption) throws RemoteException
{ super();
this.iniFileLocation = iniFileLocation;
this.layoutOption = layoutOption;
initAPI();
startupAPI();
openAPI();
}
public static void main(String args[])
{
System.out.println("RMI server started");
// Create and install a security manager
if (System.getSecurityManager() == null)
{
System.setSecurityManager(new RMISecurityManager());
System.out.println("Security manager installed.");
}
else
System.out.println("Security manager already exists.");
try //special exception handler for registry creation
{
LocateRegistry.createRegistry(1099);
System.out.println("java RMI registry created.");
}
catch (RemoteException e)
{
//do nothing, error means registry already exists
System.out.println("java RMI registry already exists.");
}
try
{
//Instantiate RmiServer
for (String arg: args){
System.out.println(arg);
}
RmiServer obj = new RmiServer(args[0],args[1]);
// Bind this object instance to the name "RmiServer"
Naming.rebind("//127.0.0.1/RmiServer", obj);
System.out.println("PeerServer bound in registry");
}
catch (Exception e)
{
System.err.println("RMI server exception:");
e.printStackTrace();
}
}
}
I've seen solutions relating to the java.rmi.server.codebase but didn't have any luck setting this either

You haven't regenerated the stub with rmic, or the Registry doesn't have access to it via its classpath.

After some further investigation and following RMI tutorials it appeared that there was a problem with the RMI registration server on port 1099.
When I stared the RMI registration server on another port (e.g. 2005) and changed these lines of code
LocateRegistry.createRegistry(2005);
and
Naming.rebind("//127.0.0.1:2055/RmiServer", obj);
This ran sucessfully without errors and my client was able to connect.
I hope this answer helps others with this error. Let me know if anyone needs any more detailed information.

Related

Error running RMI application in Visual Studio Code [duplicate]

I'm programming a simple rmi application, and I have a problem.
If I run the registry in the same directory, it works; but if I change the directory from which I run the registry, it does not.
The registry would work generically from another host, but only the change of directory stop his functionality.
I'm working on this problem by 3 days without solution, I change also every possible configuration of the codebase parameter but nothing.
I describe the situation and code with the directory:
fileserver.java :
`package testrmi2;
import java.rmi.*;
public interface fileserver extends Remote {
public void scrivifile(String nomefile, String arg) throws RemoteException;
}
`
fileserverimpl.java:
package testrmi2;
import java.io.*;
import java.rmi.*;
import java.rmi.server.*;
public class fileserverimpl extends UnicastRemoteObject implements fileserver{
public fileserverimpl() throws RemoteException {
super();
}
public void scrivifile(String nomefile, String arg) throws RemoteException {
try {
FileWriter myfile = new FileWriter(nomefile);
myfile.write(arg);
myfile.close(); }
catch (Exception e) {System.out.println(e);}
}
public static void main (String arg[]) {
try {
fileserverimpl s = new fileserverimpl();
if (System.getSecurityManager() == null) {
System.setSecurityManager(new RMISecurityManager());
}
String codebase = System.getProperty("classpath");
System.out.println("Trying to access code base: " + codebase+"\n\nuse is "+System.getProperty("useCodebaseOnly"));
Naming.rebind("//127.0.0.1:2005/fileserverimpl", s);
System.out.println("Server attivato.");
} catch (Exception e) {System.out.println("errore inizializzazione server\n\n"+e.getMessage()+"\n\n\n");
}}}
client.java:
package testrmi2;
import java.rmi.*;
import java.io.*;
public class client {
public static void main (String arg[]) {
fileserver myserver;
String nomefile=" ";
String testo=" ";
System.out.println("Scrivi il nome del file");
nomefile=ReadString();
System.out.println("Scrivi il testo");
testo=ReadString();
try {
myserver = (fileserver) Naming.lookup("//127.0.0.1:2005/fileserverimpl");
myserver.scrivifile(nomefile, testo);
} catch (Exception e) {System.out.println(e);}
}
public static String ReadString() {
BufferedReader stdIn =new BufferedReader(new InputStreamReader(System.in));
String s=" ";
try{
s=stdIn.readLine();
}
catch(IOException e) {System.out.println(e.getMessage()); }
return s;
}
}
and the policy file is:
grant {
// Allow everything for now
permission java.security.AllPermission;
};
all this files are on the directory:
/Users/franco/Desktop/prova
to compiling it I goes in the /Users/franco/Desktop/prova directory and do in terminal :
javac -cp . -d . *.java
rmic rmic testrmi2.fileserverimpl
jar cvf testrmi2.jar testrmi2/fileserver.class testrmi2/fileserverimpl_Stub.class
after I run registry in another terminal with the following commands but in another directory:
export classpath=""
rmiregistry 2005 &
Finally I would to run the filesereveimpl.class goes with terminal in the /Users/franco/Desktop/prova directory and write :
java -classpath /Users/franco/Desktop/prova/ -Djava.rmi.server.codebase=file:/Users/franco/Desktop/prova/testrmi2.jar -Djava.security.policy=testrmi2/policy testrmi2.fileserverimpl &
But the results are:
Trying to access code base: null
use is null
errore inizializzazione server
RemoteException occurred in server thread; nested exception is:
java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is:
java.lang.ClassNotFoundException: testrmi2.fileserverimpl_Stub
I also try to public he jar on a local webserver xampp and try to run with the following:
java -classpath . -Djava.rmi.server.codebase=http://127.0.0.1/testrmi2/ -Djava.security.policy=testrmi2/policy testrmi2.fileserverimpl &
or with :
java -classpath . -Djava.rmi.server.codebase=http://127.0.0.1/testrmi2.jar -Djava.security.policy=testrmi2/policy testrmi2.fileserverimpl &
but I have the same results.
Try to set the classpath var before to execute the rmregistry:
export classpath="/Users/franco/Desktop/prova/"
rmiregistry 2005 &
There are three cases.
You got that exception in the server when exporting/constructing the remote object. Solution: run rmic to generate the stub.
You got it in the server when binding/rebinding. Solution: make the stub class available to the Registry's CLASSPATH, or run the Registry in the server JVM via LocateRegistry.createRegistry().
You got it in the client, in lookup(). Solution: make the stub class available to the client's CLASSPATH.
These also apply to the remote interface itself, and any application classes it depends on, and so on recursively until closure.
Solution for stubs to all three: take the measures outlined in the Javadoc preamble to UnicastRemoteObject, so you don't need a stub at all.

Java gRPC Server Not Starting

I am trying to implement gRPC service in my code with condition to use one instance of the server class the code building successfully but i got a runtime error.
How can I fix this error?
Exception in thread "main" java.lang.NullPointerException: bindableService
at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:897)
at io.grpc.internal.AbstractServerImplBuilder.addService(AbstractServerImplBuilder.java:120)
at io.grpc.internal.AbstractServerImplBuilder.addService(AbstractServerImplBuilder.java:56)
at com.mypackage.cu.comms.CURequestHandlerService.<init>(Unknown Source)
at com.mypackage.cu.comms.CURequestHandlerService.start(Unknown Source)
at com.mypackage.cu.entities.ControlUnit.initSystem(Unknown Source)
at com.mypackage.cu.entities.ControlUnit.run(Unknown Source)
at com.mypackage.cu.entities.ControlUnit.main(Unknown Source)
the genrated grpc java class is CURequestHandlerGrpc.
the server class CURequestHandlerService:
import io.grpc.Server;
import io.grpc.ServerBuilder;
import io.grpc.stub.StreamObserver;
\\ ..
public class CURequestHandlerService extends CURequestHandlerGrpc.CURequestHandlerImplBase {
private final Server server;
private static CURequestHandlerService INSTANCE = null;
private CURequestHandlerService(int port) {
this.server = ServerBuilder.forPort(port)
.addService(INSTANCE)
.build();
}
public static void start(int port) {
if (INSTANCE == null)
INSTANCE = new CURequestHandlerService(port);
try {
INSTANCE.server.start();
} catch (IOException e) {
System.out.println("Can't start GRPC server!");
}
}
in main class
CURequestHandlerService.start(16440);
I am using Ant since it is an old project and i maintaining it.
I add all grpc dependencies jars to dependencies directory.
my dependencies jars:
amqp-client-5.7.3.jar
failureaccess-1.0.1.jar
grpc-all-1.27.0.jar
grpc-api-1.27.0.jar
grpc-auth-1.27.0.jar
grpc-context-1.27.0.jar
grpc-core-1.27.0.jar
grpc-netty-1.27.0.jar
grpc-netty-shaded-1.27.0.jar
grpc-protobuf-1.27.0.jar
grpc-protobuf-lite-1.27.0.jar
grpc-stub-1.27.0.jar
grpc-testing-1.27.0.jar
guava-28.2-jre.jar
json-smart-1.2.jar
lettuce-4.3.0.Final-shaded.jar
libthrift-0.9.3.jar
opencensus-api-0.20.0.jar
opencensus-contrib-grpc-metrics-0.20.0.jar
perfmark-api-0.21.0.jar
protobuf-java-3.11.3.jar
protobuf-java-util-3.11.3.jar
protoc-gen-grpc-java-1.27.0-linux-x86_64.exe
slf4j-api-1.7.22.jar
slf4j-simple-1.7.22.jar
I found out the problem it is not a dependency issue after reading the server example in grpc-java repository. I underhanded that you must split the server class from the implementation class which extends form the Grpc generated class.
server class
/**
* Grpc instance class that used to initiate the server and call implementation class
*/
public class GrpcServer {
private final Server server;
private static GrpcServer INSTANCE = null;
private GrpcServer(int port) {
this.server = ServerBuilder.forPort(port)
.addService(new CURequestHandlerService())
.build();
}
public static void start(int port) {
if (INSTANCE == null)
INSTANCE = new GrpcServer(port);
try {
INSTANCE.server.start();
System.out.println("GRPC server started");
} catch (IOException e) {
System.out.println("Can't start GRPC server!");
}
}
}
implementation class
**
* Request Handler class contains the implementation for all functions initiated in proto files
* The implemented function
* */
public class CURequestHandlerService extends CURequestHandlerGrpc.CURequestHandlerImplBase{
public synchronized void Method1(Messages.Method1Input input,
StreamObserver<Messages.Method1Output> output) {
//...
output.onNext(outputResponse);
output.onCompleted();
}

rmi java.lang.ClassNotFoundException: testrmi2.fileserverimpl_Stub

I'm programming a simple rmi application, and I have a problem.
If I run the registry in the same directory, it works; but if I change the directory from which I run the registry, it does not.
The registry would work generically from another host, but only the change of directory stop his functionality.
I'm working on this problem by 3 days without solution, I change also every possible configuration of the codebase parameter but nothing.
I describe the situation and code with the directory:
fileserver.java :
`package testrmi2;
import java.rmi.*;
public interface fileserver extends Remote {
public void scrivifile(String nomefile, String arg) throws RemoteException;
}
`
fileserverimpl.java:
package testrmi2;
import java.io.*;
import java.rmi.*;
import java.rmi.server.*;
public class fileserverimpl extends UnicastRemoteObject implements fileserver{
public fileserverimpl() throws RemoteException {
super();
}
public void scrivifile(String nomefile, String arg) throws RemoteException {
try {
FileWriter myfile = new FileWriter(nomefile);
myfile.write(arg);
myfile.close(); }
catch (Exception e) {System.out.println(e);}
}
public static void main (String arg[]) {
try {
fileserverimpl s = new fileserverimpl();
if (System.getSecurityManager() == null) {
System.setSecurityManager(new RMISecurityManager());
}
String codebase = System.getProperty("classpath");
System.out.println("Trying to access code base: " + codebase+"\n\nuse is "+System.getProperty("useCodebaseOnly"));
Naming.rebind("//127.0.0.1:2005/fileserverimpl", s);
System.out.println("Server attivato.");
} catch (Exception e) {System.out.println("errore inizializzazione server\n\n"+e.getMessage()+"\n\n\n");
}}}
client.java:
package testrmi2;
import java.rmi.*;
import java.io.*;
public class client {
public static void main (String arg[]) {
fileserver myserver;
String nomefile=" ";
String testo=" ";
System.out.println("Scrivi il nome del file");
nomefile=ReadString();
System.out.println("Scrivi il testo");
testo=ReadString();
try {
myserver = (fileserver) Naming.lookup("//127.0.0.1:2005/fileserverimpl");
myserver.scrivifile(nomefile, testo);
} catch (Exception e) {System.out.println(e);}
}
public static String ReadString() {
BufferedReader stdIn =new BufferedReader(new InputStreamReader(System.in));
String s=" ";
try{
s=stdIn.readLine();
}
catch(IOException e) {System.out.println(e.getMessage()); }
return s;
}
}
and the policy file is:
grant {
// Allow everything for now
permission java.security.AllPermission;
};
all this files are on the directory:
/Users/franco/Desktop/prova
to compiling it I goes in the /Users/franco/Desktop/prova directory and do in terminal :
javac -cp . -d . *.java
rmic rmic testrmi2.fileserverimpl
jar cvf testrmi2.jar testrmi2/fileserver.class testrmi2/fileserverimpl_Stub.class
after I run registry in another terminal with the following commands but in another directory:
export classpath=""
rmiregistry 2005 &
Finally I would to run the filesereveimpl.class goes with terminal in the /Users/franco/Desktop/prova directory and write :
java -classpath /Users/franco/Desktop/prova/ -Djava.rmi.server.codebase=file:/Users/franco/Desktop/prova/testrmi2.jar -Djava.security.policy=testrmi2/policy testrmi2.fileserverimpl &
But the results are:
Trying to access code base: null
use is null
errore inizializzazione server
RemoteException occurred in server thread; nested exception is:
java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is:
java.lang.ClassNotFoundException: testrmi2.fileserverimpl_Stub
I also try to public he jar on a local webserver xampp and try to run with the following:
java -classpath . -Djava.rmi.server.codebase=http://127.0.0.1/testrmi2/ -Djava.security.policy=testrmi2/policy testrmi2.fileserverimpl &
or with :
java -classpath . -Djava.rmi.server.codebase=http://127.0.0.1/testrmi2.jar -Djava.security.policy=testrmi2/policy testrmi2.fileserverimpl &
but I have the same results.
Try to set the classpath var before to execute the rmregistry:
export classpath="/Users/franco/Desktop/prova/"
rmiregistry 2005 &
There are three cases.
You got that exception in the server when exporting/constructing the remote object. Solution: run rmic to generate the stub.
You got it in the server when binding/rebinding. Solution: make the stub class available to the Registry's CLASSPATH, or run the Registry in the server JVM via LocateRegistry.createRegistry().
You got it in the client, in lookup(). Solution: make the stub class available to the client's CLASSPATH.
These also apply to the remote interface itself, and any application classes it depends on, and so on recursively until closure.
Solution for stubs to all three: take the measures outlined in the Javadoc preamble to UnicastRemoteObject, so you don't need a stub at all.

JavaAgent in Lotus Notes 6.5 using axis api gives Exception "No implementation defined for org.apache.commons.logging.LogFactory"

I needed to write a JavaAgent in a Lotus Notes 6.5 DB to access a web service. I used Axis Apache API for this purpose. I created A Java agent and added the jar files of axis in the agent by using Edit Project button.
Below is the agent code:
import lotus.domino.*;
import javax.xml.*;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import javax.xml.namespace.QName;
import java.net.URL;
public class JavaAgent extends AgentBase {
public void NotesMain() {
try {
Session session = getSession();
AgentContext agentContext = session.getAgentContext();
String endpoint = "http://ws.apache.org:5049/axis/services/echo";
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(endpoint) );
call.setOperationName(new QName("http://soapinterop.org/", "echoString"));
String ret = (String) call.invoke( new Object[] { "Hello!" } );
System.out.println("Sent 'Hello!', got '" + ret + "'");
} catch(Exception e) {
e.printStackTrace();
}
}
}
And below is the exception thrown:
java.lang.ExceptionInInitializerError: org.apache.commons.discovery.DiscoveryException: No implementation defined for org.apache.commons.logging.LogFactory
at org.apache.commons.discovery.tools.SPInterface.newInstance(SPInterface.java:197)
at org.apache.commons.discovery.tools.DiscoverClass.newInstance(DiscoverClass.java:579)
at org.apache.commons.discovery.tools.DiscoverSingleton.find(DiscoverSingleton.java:418)
at org.apache.commons.discovery.tools.DiscoverSingleton.find(DiscoverSingleton.java:378)
at org.apache.axis.components.logger.LogFactory$1.run(LogFactory.java:84)
at java.security.AccessController.doPrivileged(Native Method)
at org.apache.axis.components.logger.LogFactory.getLogFactory(LogFactory.java:80)
at org.apache.axis.components.logger.LogFactory.<clinit>(LogFactory.java:72)
at org.apache.axis.configuration.EngineConfigurationFactoryFinder.<clinit>(EngineConfigurationFactoryFinder.java:94)
at org.apache.axis.client.Service.<init>(Service.java:111)
at JavaAgent.NotesMain(JavaAgent.java:17)
at lotus.domino.AgentBase.runNotes(Unknown Source)
at lotus.domino.NotesThread.run(NotesThread.java:218)
I thried to follow some links on the internet like, But i was not able to get exactly what it was asking to do. eg: http://www-10.lotus.com/ldd/nd6forum.nsf/55c38d716d632d9b8525689b005ba1c0/40d033fba3897f4d85256cd30034026a?OpenDocument
Any help will be great. All i wanted to do is write an agent so that i can access a web service, say temperature conversion web service on w3schools. http://www.w3schools.com/webservices/tempconvert.asmx?op=FahrenheitToCelsius
I googled with your error message and this is the first hit:
http://croarkin.blogspot.fi/2010/08/commons-logging-headaches-with-axis.html
It suggests using a commons-logging.properties file with:
org.apache.commons.logging.Log = org.apache.commons.logging.impl.Log4JLogger
org.apache.commons.logging.LogFactory = org.apache.commons.logging.impl.LogFactoryImpl
or putting this to your code:
#BeforeClass
public static void beforeClass() {
System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.Log4JLogger");
System.setProperty("org.apache.commons.logging.LogFactory", "org.apache.commons.logging.impl.LogFactoryImpl");
}
Probably you've already tried this because it's the first hit with google but just in case...

Java JAR problem

My Java is very rusty and I'm having a bit of a headache here.
Here's my server.java:
import java.io.IOException;
import net.tootallnate.websocket.WebSocket;
import net.tootallnate.websocket.WebSocketServer;
public class server extends WebSocketServer
{
public server(int port)
{
super(port, Draft.AUTO);
}
public void onClientOpen(WebSocket conn)
{
System.out.println("onopen");
/*try
{
this.sendToAll(conn+" entered the room!");
}
catch(IOException ex)
{
ex.printStackTrace();
}*/
}
public void onClientClose(WebSocket conn)
{
System.out.println("onclose");
}
public void onClientMessage(WebSocket conn,String message)
{
System.out.println("onmessage"+message);
}
public static void main(String[] args)
{
int port = 8000;
try
{
port=Integer.parseInt(args[0]);
}
catch(Exception ex)
{
}
server s = new server(port);
s.start();
System.out.println("ChatServer started on port: "+s.getPort());
}
}
I need to include a jar file: ./Java-WebSocket/dist/WebSocket.jar (available from http://github.com/TooTallNate/Java-WebSocket/)
It compiles OK:
prompt> javac -classpath ./Java-WebSocket/dist/WebSocket.jar server.java
However, when I go to run:
prompt> java -classpath ./Java-WebSocket/dist/WebSocket.jar server
Exception in thread "main" java.lang.NoClassDefFoundError: server
Caused by: java.lang.ClassNotFoundException: server
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: server. Program will exit.
I hope someone can help.
Thanks in advance,
java -classpath .:./Java-WebSocket/dist/WebSocket.jar server
^^
Or on Windows:
java -classpath .;./Java-WebSocket/dist/WebSocket.jar server
^^
The classpath defaults to the current directory. You need to point to your classes as well as the library.
Isn't server placed into a package ? If it is, specify the full class name.
e.g. prompt> java -classpath ./Java-WebSocket/dist/WebSocket.jar app.core.server
I would also uppercase the class name, though.
Have you tried using the full class name (your.package.server)?

Categories

Resources