I'm having a problem restarting my RMI registry after it has been stopped:
import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.UnicastRemoteObject;
import javax.swing.JOptionPane;
public class CinemaServer
{
private Registry registry;
ClientImpl clientImple; //remote interface implemented class
private static String title="Cinema Pvt Ltd";
public CinemaServer() {
try {
clientImple = new ClientImpl();
registry=LocateRegistry.createRegistry(3311);
registry.rebind("RMI_INSTANCE", clientImple);
} catch (RemoteException e) {
JOptionPane.showMessageDialog(null, "Can't Start RMI Server",title,JOptionPane.ERROR_MESSAGE);
}
}
public void stopServer()
{
try {
registry.unbind("RMI_INSTANCE");
UnicastRemoteObject.unexportObject(clientImple, true);
} catch (NotBoundException e) {
JOptionPane.showMessageDialog(null, "Can't Stop Server",title,JOptionPane.ERROR_MESSAGE);
}
}
}
I start the server with: CinemaServer ser=new CinemaServer();
And when I call ser.stopServer(); it stops.
But I cannot restart it
I'm getting:
java.rmi.server.ExportException: internal error: ObjID already in use
at sun.rmi.transport.ObjectTable.putTarget(Unknown Source)
at sun.rmi.transport.Transport.exportObject(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport.exportObject(Unknown Source)
at sun.rmi.transport.tcp.TCPEndpoint.exportObject(Unknown Source)
at sun.rmi.transport.LiveRef.exportObject(Unknown Source)
...
the call is failing on createRegistry(), not on re-exporting your object. don't create the registry twice.
Related
I am getting the following error:
Exception in thread "main" java.rmi.NotBoundException: Server
at java.rmi/sun.rmi.registry.RegistryImpl.lookup(RegistryImpl.java:234)
at java.rmi/sun.rmi.registry.RegistryImpl_Skel.dispatch(RegistryImpl_Skel.java:133)
at java.rmi/sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:468)
at java.rmi/sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:298)
at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:200)
at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:197)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.rmi/sun.rmi.transport.Transport.serviceCall(Transport.java:196)
at java.rmi/sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:562)
at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:796)
at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:677)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:676)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)
at java.rmi/sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:303)
at java.rmi/sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:279)
at java.rmi/sun.rmi.server.UnicastRef.invoke(UnicastRef.java:380)
at java.rmi/sun.rmi.registry.RegistryImpl_Stub.lookup(RegistryImpl_Stub.java:123)
at client.RMIClient.startClient(RMIClient.java:17)
at client.FahrradClient.main(FahrradClient.java:12)
Can anybody look at my code and tell me where I went wrong? I'd be more than grateful. Would even send a small tip or something lmao I am that desperate.
package server;
import shared.RMI_Interface;
import java.io.IOException;
import java.rmi.AlreadyBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class FahrradServer {
public static void main(String[] args) throws IOException, AlreadyBoundException {
RMI_Interface server = new ConfigImpl();
Registry registry = LocateRegistry.createRegistry(1099);
Runtime.getRuntime().exec("rmiregistry 1099");
registry.bind("Server", server);
System.out.println("Server started");
}
}
package client;
import shared.Fahrrad;
import shared.RMI_Interface;
import java.rmi.*;
import java.rmi.registry.*;
public class RMIClient {
private RMI_Interface server;
public RMIClient() {}
public void startClient() throws RemoteException, NotBoundException {
Registry registry = LocateRegistry.getRegistry("localhost", 1099);
server = (RMI_Interface)registry.lookup("Server");
}
public Fahrrad configureFahrrad(String lenkertyp, String material, String schaltung, String griff ) {
Fahrrad result = null;
try {
result = server.configureFahrrad(lenkertyp, material, schaltung, griff);
} catch (RemoteException e) {
e.printStackTrace();
throw new RuntimeException("Could not contact server");
}
return result;
}
}
package server;
import shared.Fahrrad;
import shared.RMI_Interface;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class ConfigImpl implements RMI_Interface {
public void ConfigImpl() throws RemoteException {
UnicastRemoteObject.exportObject(this, 0);
}
#Override
public Fahrrad configureFahrrad(String lenkertyp, String material, String schaltung, String griff ) throws RemoteException {
Fahrrad f = new Fahrrad();
f.setLenkertyp(lenkertyp);
f.setGriff(griff);
f.setMaterial(material);
f.setSchaltung(schaltung);
if (!lenkertyp.equals( "Faltbarlenker") && !lenkertyp.equals( "Rennradlenker") && !lenkertyp.equals( "Bullhornlenker")) {
throw new IllegalArgumentException("Ungültiger Lenktypinput");
}
if (!material.equals( "Aluminium") && !material.equals( "Stahl") && !material.equals( "Kunststoff")){
throw new IllegalArgumentException("Ungültiges Material!");
}
if (!schaltung.equals("Kettenschaltung") && !schaltung.equals( "Tretlagerschaltung") && !lenkertyp.equals( "Nebenschaltung")) {
throw new IllegalArgumentException("Ungültige Schaltung!");
}
if (!griff.equals( "Kunststoffgriff") && !griff.equals( "Ledergriff") && !lenkertyp.equals( "Schaumstoffgriff")) {
throw new IllegalArgumentException("Ungültige Schaltung!");
}
if (lenkertyp.equals("Faltbarlenker") || lenkertyp.equals("Rennradlenker")) {
if (!material.equals( "Aluminium") && !material.equals( "Kunststoff")) {
throw new IllegalArgumentException(lenkertyp + " kann nur aus Aluminium oder Kunststoff bestehen.");
}
}
if (material.equals("Stahl")) {
if (!schaltung.equals("Kettenschaltung")) {
throw new IllegalArgumentException("Materialtyp "+ material + " kann nur Kettenschaltung haben!");
}
}
if (material.equals("Kunststoff")) {
if (griff.equals("Kunststoffgriff")) {
throw new IllegalArgumentException("Nur Kunststoffmaterial kann Kunststoffgriff haben!");
}
}
if (lenkertyp.equals( "Bullhornlenker") || lenkertyp.equals("Faltbarlenker")) {
if (griff.equals("Ledergriff")) {
throw new IllegalArgumentException("Nur Rennladlenker können Ledergriffe haben!");
}
}
return f;
}
}
package server;
import shared.RMI_Interface;
import java.io.IOException;
import java.rmi.AlreadyBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class FahrradServer{
public static void main(String[] args) throws IOException, AlreadyBoundException {
RMI_Interface server = new ConfigImpl();
Registry registry = LocateRegistry.createRegistry(1099);
Runtime.getRuntime().exec("rmiregistry 1099");
registry.bind("Server", server);
System.out.println("Server started");
}
}
package shared;
public class Fahrrad {
public String lenkertyp;
public String material;
public String schaltung;
public String griff;
public void setLenkertyp(String lenkertyp){
this.lenkertyp=lenkertyp;
}
public String getLenkertyp() {
return lenkertyp;
}
public void setMaterial(String material){
this.material=material;
}
public String getMaterial() {
return material;
}
public void setSchaltung(String schaltung) {
this.schaltung=schaltung;
}
public String getSchaltung() {
return schaltung;
}
public void setGriff(String griff){
this.griff=griff;
}
public String getGriff() {
return griff;
}
}
package shared;
import java.rmi.Remote;
import java.rmi.RemoteException;
// Creating Remote interface for our application
public interface RMI_Interface extends Remote {
public Fahrrad configureFahrrad(String lenkertyp, String material, String schaltung, String griff ) throws RemoteException;
}
I know the code isn't perfect. I will change it but the most important thing is to get the client and server run independently. My client sadly doesn't run.
The steps to follow when creating a RMI application.
Create the "remote" interface.
Your interface RMI_Interface is OK. Nothing to do here.
Create a class that implements the remote interface.
I made the constructor empty. (The rest of the code is unchanged.)
public ConfigImpl() throws RemoteException {
// Empty.
}
Create a RMI server. Here I made a few changes.
package server;
import java.rmi.AlreadyBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import shared.RMI_Interface;
public class FahrradServer {
public static void main(String[] args) {
try {
ConfigImpl server = new ConfigImpl();
RMI_Interface stub = (RMI_Interface) UnicastRemoteObject.exportObject(server, 0);
Registry registry = LocateRegistry.createRegistry(1099);
registry.bind("Server", stub);
System.out.println("Server started");
}
catch (AlreadyBoundException | RemoteException x) {
x.printStackTrace();
}
}
}
Note that calling method createRegistry() will launch the rmiregistry so no need for this line of your code:
Runtime.getRuntime().exec("rmiregistry 1099");
Run the server, i.e. java server.FahrradServer
(I assume you know the correct command that you need to actually launch your server.)
Note that the server program will run forever – unless you kill it.
Write the client. The client will run in a separate JVM so the client needs a main() method so that you can launch it via the java command.
package client;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import shared.Fahrrad;
import shared.RMI_Interface;
public class RMIClient {
private RMI_Interface server;
public void startClient() throws RemoteException, NotBoundException {
Registry registry = LocateRegistry.getRegistry(1099);
server = (RMI_Interface) registry.lookup("Server");
}
public Fahrrad configureFahrrad(String lenkertyp,
String material,
String schaltung,
String griff) throws RemoteException {
return server.configureFahrrad(lenkertyp, material, schaltung, griff);
}
public static void main(String[] args) {
RMIClient client = new RMIClient();
try {
client.startClient();
Fahrrad f = client.configureFahrrad("Faltbarlenker",
"Aluminium",
"Kettenschaltung",
"Kunststoffgriff");
System.out.println("Fahrrad: " + f);
}
catch (RemoteException | NotBoundException x) {
x.printStackTrace();
}
}
}
Since the return value of the remote interface method is a class that you wrote, that class must implement interface Serializable, i.e.
public class Fahrrad implements java.io.Serializable
Now you can launch your RMI client, e.g. java client.RMIClient
Refer to the RMI trail of Oracle's java tutorials.
I am very new to Java and the RMI system. I am following a tutorial but I am unsure why I keep getting the following errors[1]
[1]: https://i.stack.imgur.com/xeYTn.png
I have attached the code (taken directly from the tutorial here: https://docs.oracle.com/javase/1.5.0/docs/guide/rmi/hello/hello-world.html)
I have tried:
removing any lines with 'package'
changing classpath variables
reinstalling java and javac
setting classpath in the 'rmiregistry &' command
Any help would be appreciated
edit: forgot to attach the code.
Hello.java
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Hello extends Remote {
String sayHello() throws RemoteException;
}
Client.java
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class Client {
private Client() {
}
public static void main(String[] args) {
String host = (args.length < 1) ? null : args[0];
try {
Registry registry = LocateRegistry.getRegistry(host);
Hello stub = (Hello) registry.lookup("Hello");
String response = stub.sayHello();
System.out.println("response: " + response);
} catch (Exception e) {
System.err.println("Client exception: " + e.toString());
e.printStackTrace();
}
}
}
Server.java
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class Server implements Hello {
public Server() {
}
public String sayHello() {
return "Hello, world!";
}
public static void main(String args[]) {
try {
Server obj = new Server();
Hello stub = (Hello) UnicastRemoteObject.exportObject((Remote) obj, 0);
// Bind the remote object's stub in the registry
Registry registry = LocateRegistry.getRegistry();
registry.bind("Hello", stub);
System.err.println("Server ready");
} catch (Exception e) {
System.err.println("Server exception: " + e.toString());
e.printStackTrace();
}
}
}
You have to run the command rmiregistry & in the folder the code is compiled into. In this case, "files"
the "getting started" tutorial didn't mention that.
As I'm trying to achieve my first RMI program, I'm having troubles testing it.
I compiled my programs and tried to launch my server but an error then occurs.
About my server: 1 interface + 1 class implementing it + my server class
Interface
package src;
import java.rmi.*;
public interface Information extends Remote {
// methods than can be called remotly
public String getInformation() throws RemoteException;
}
Class Implementing it
package src;
import java.rmi.server.UnicastRemoteObject;
import java.rmi.RemoteException;
public class InformationImpl extends UnicastRemoteObject implements Information {
protected InformationImpl() throws RemoteException {
super();
}
public String getInformation() throws RemoteException {
System.out.println("method getInformation()");
return "hello";
}
}
My server class :
package src;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.UnknownHostException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
public class LanceServeur {
public static void main(String[] args) {
try {
LocateRegistry.createRegistry(1099);
InformationImpl informationImpl = new InformationImpl();
Naming.rebind("MyServer", informationImpl);
System.out.println("Server launched");
}
catch (Exception e) {
e.printStackTrace();
}
}
}
And my client class :
package src;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.Remote;
import java.rmi.RemoteException;
public class lanceClient {
public static void main(String[] args) {
System.out.println("Lancement du client");
try {
Remote r = Naming.lookup("rmi://localhost:1099/TestRMI");
System.out.println(r);
if (r instanceof Information) {
String s = ((Information) r).getInformation();
System.out.println("chaine renvoyee = " + s);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
} catch (NotBoundException e) {
e.printStackTrace();
}
System.out.println("end of client");
}
}
If you want to see the correct lines, check my github it's easier :
https://github.com/TLprojet/MUD_Java
About the error, here it is:
java.rmi.ConnectIOException: Exception creating connection to: 0.0.21.214; nested exception is:
java.net.SocketException: Invalid argument or cannot assign requested address
at java.rmi/sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:631)
at java.rmi/sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:209)
at java.rmi/sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:196)
at java.rmi/sun.rmi.server.UnicastRef.newCall(UnicastRef.java:338)
at java.rmi/sun.rmi.registry.RegistryImpl_Stub.rebind(RegistryImpl_Stub.java:147)
at java.rmi/java.rmi.Naming.rebind(Naming.java:177)
at src.LanceServeur.main(LanceServeur.java:16)
Caused by: java.net.SocketException: Invalid argument or cannot assign requested address
at java.base/java.net.PlainSocketImpl.socketConnect(Native Method)
at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:399)
at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:242)
at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:224)
at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:403)
at java.base/java.net.Socket.connect(Socket.java:591)
at java.base/java.net.Socket.connect(Socket.java:540)
at java.base/java.net.Socket.<init>(Socket.java:436)
at java.base/java.net.Socket.<init>(Socket.java:213)
at java.rmi/sun.rmi.transport.tcp.TCPDirectSocketFactory.createSocket(TCPDirectSocketFactory.java:40)
at java.rmi/sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:613)
... 6 more
hi i am using this codes for rmi
RmiServer.java
import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;
import java.net.*;
public class RmiServer extends java.rmi.server.UnicastRemoteObject
implements ReceiveMessageInterface
{
int thisPort;
String thisAddress;
Registry registry; // rmi registry for lookup the remote objects.
// This method is called from the remote client by the RMI.
// This is the implementation of the gReceiveMessageInterfaceh.
public void receiveMessage(String x) throws RemoteException
{
System.out.println(x);
}
public RmiServer() throws RemoteException
{
try{
// get the address of this host.
thisAddress= (InetAddress.getLocalHost()).toString();
}
catch(Exception e){
throw new RemoteException("can't get inet address.");
}
thisPort=3232; // this port(registryfs port)
System.out.println("this address="+thisAddress+",port="+thisPort);
try{
// create the registry and bind the name and object.
registry = LocateRegistry.createRegistry( thisPort );
registry.rebind("rmiServer", this);
}
catch(RemoteException e){
throw e;
}
}
static public void main(String args[])
{
try{
RmiServer s=new RmiServer();
}
catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
}
RmiClient.java
import java.rmi.*;
import java.rmi.registry.*;
import java.net.*;
public class RmiClient
{
static public void main(String args[])
{
ReceiveMessageInterface rmiServer;
Registry registry;
String serverAddress=args[0];
String serverPort=args[1];
String text=args[2];
System.out.println("sending "+text+" to "+serverAddress+":"+serverPort);
try{
// get the �gregistry�h
registry=LocateRegistry.getRegistry(
serverAddress,
(new Integer(serverPort)).intValue()
);
// look up the remote object
rmiServer=
(ReceiveMessageInterface)(registry.lookup("rmiServer"));
// call the remote method
rmiServer.receiveMessage(text);
}
catch(RemoteException e){
e.printStackTrace();
}
catch(NotBoundException e){
e.printStackTrace();
}
}
}
ReceiveMessageInterface.java
import java.rmi.*;
public interface ReceiveMessageInterface extends Remote
{
public void receiveMessage(String x) throws RemoteException;
}
This works fine normally , but when the a computer is connected to internet through mobile or it shares internet from other pc it doesn't work
I get this error.
java.net.connectexception connection timeout
when i tried to telnet it fails to connect but when i try to run this program that pc
to my pc it works.
Please let me know how to solve this issue.
Sounds like a firewall or proxy server issue.
i am having a problem creating RMIserver class because i keep getting this error : access denied (java.net.SocketPermission 127.0.0.1:1099 connect,resolve)
now i read on google something about cerating new policy file but i dont really understand how to do so, can please someone help me ?
here is my server code :
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.RMISecurityManager;
import java.rmi.server.UnicastRemoteObject;
import java.rmi.registry.*;
import java.security.Permission;
import java.security.Security;
public class RmiServer extends UnicastRemoteObject
implements RmiServerIntf
{
public static final String MESSAGE = "Hello world";
public RmiServer() throws RemoteException
{
}
public String getMessage()
{
return MESSAGE;
}
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
RmiServer obj = new RmiServer();
// Bind this object instance to the name "RmiServer"
Naming.rebind("//localhost/RmiServer", obj);
System.out.println("PeerServer bound in registry");
}
catch (Exception e)
{
System.err.println("RMI server exception:" + e);
e.printStackTrace();
}
}
}
You are using a SecurityManager (why?) but your security policy doesn't grant you the permission specified in the exception. You don't need a SecurityManager at all unless you are planning to use the RMI codebase feature: are you?
You really need to read the manual of RMI, and how to create a policy file:
http://docs.oracle.com/javase/tutorial/rmi/running.html