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
Related
while Running RMI using java and CMD. java.rmi.server.ExportException is displayed. The Exception is that, port is already in use.
The server interface
import java.rmi.*;
public interface AdditionInterface extends Remote {
public int add(int a, int b) throws RemoteException;
}
The Interface implementation
import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class AdditionClient {
public static void main(String[] args) {
try {
String host="";
Registry registry = LocateRegistry.getRegistry(host);
AdditionInterface hello = (AdditionInterface) registry.lookup("Addition");
int result = hello.add(9, 2);
System.out.println("Result is: " + result);
} catch (Exception ex) {
System.out.println("HelloClient Exception" + ex);
}
}
}
The Server Registry class
package chapter40;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class AdditionServer {
public static void main(String[] args) {
try {
Registry registry = LocateRegistry.getRegistry();
AdditionInterface obj = new Addition();
registry.rebind("Addition", obj);
System.out.println("Addition Server is ready");
} catch (Exception ex) {
System.out.println("Addition Server failed" + ex);
}
}
}
The client program
import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class AdditionClient {
public static void main(String[] args) {
try {
String host="";
Registry registry = LocateRegistry.getRegistry(host);
AdditionInterface hello = (AdditionInterface) registry.lookup("Addition");
int result = hello.add(9, 2);
System.out.println("Result is: " + result);
} catch (Exception ex) {
System.out.println("HelloClient Exception" + ex);
}
}
}
I separated the server from the client classes in two different projects for clarity sake
I searched through various threads and finally got this to work in a way different from [ but also a combination from what I'd seen ]
RUN THE SERVER PROGRAM THUS (from the command prompt):
change the directory to the source folder for the server
cd c:\Users\Heavenly\workspace\RMIServerSide\src
compile the classes
javac chapter40/*.java
start the RMI registry
start rmiregistry
start the registry server class containing the main() method. NOTE THE LINE OF CODE IS SLIGHTLY DIFFERENT
start java -cp . chapter40.AdditionServer
(the server is now set)
RUN THE CLIENT PROGRAM
Change the directory to the source folder
cd c:\Users\Heavenly\workspace\RMIClientSide\src
compile all the classes
javac chapter40/*.java
3.Run the client program
java chapter40.AdditionClient
(everything is running now)
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.
I'm trying to create an rmi application : the client transfer file to the serve. But , when running the code i get security manager exception.
Here is the client side :
import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.util.Scanner;
public class StartFileClient {
public static void main(String[] args) {
if (System.getSecurityManager() == null)
System.setSecurityManager(new RMISecurityManager());
try{
FileClient c=new FileClient("imed");
FileServerInt server=(FileServerInt)Naming.lookup("rmi://localhost/abc");
server.login(c);
System.out.println("Listening.....");
Scanner s=new Scanner(System.in);
while(true){
String line=s.nextLine();
}
}catch(Exception e){
e.printStackTrace();
}
}
}
Here is the server side :
import java.rmi.Naming;
public class StartFileServer {
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
java.rmi.registry.LocateRegistry.createRegistry(1099);
FileServer fs=new FileServer();
fs.setFile("itcrowd.avi");
Naming.rebind("rmi://localhost/abc", fs);
System.out.println("File Server is Ready");
}catch(Exception e){
e.printStackTrace();
}
}
}
Also, i create a file security.policy
grant {
permission java.security.AllPermission;
};
Thanks for helping.
Clearly your policy file isn't being found. You need to specify its location via the java.security.policy system property.
BUT Unless you're using the codebase feature, remove the security manager.
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'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.