Page 1 of 2 12 LastLast
Results 1 to 20 of 22

Thread: RMI problem

  1. #1
    Dennis's Avatar
    Dennis is offline Member
    Join Date
    May 2010
    Posts
    83
    Rep Power
    0

    Default RMI problem

    I've a problem in java RMI.
    When I run the written below script I get this error:
    HelloClient exception: java.security.AccessControlException: access denied (java.net.SocketPermission 127.0.0.1:1099 connect,resolve)
    (I use eclipse SDK)

    I've already done whats wrote at some sites, create a "security.policy" file and write
    Java Code:
    grant{
        permission java.security.AllPermission;
    };
    in that file, but it didn't work.

    I've got this HelloWorld RMI script from the Internet:

    HelloClient.java
    Java Code:
    import java.rmi.*;
    
    public class HelloClient
    {
    	
    	public static void main(String args[])
    	{
    
    		if(System.getSecurityManager()==null){
    			System.out.println("null");
    			System.setSecurityManager(new RMISecurityManager());
    		}
    		
    		try{
    			HelloInterface obj = (HelloInterface)Naming.lookup("/HelloServer");
    			String message = obj.sayHello();
    			System.out.println(message);
    		}catch(Exception e){
    			System.out.println("HelloClient exception: "+e);
    		}
    	}
    }
    HelloInterface.java
    Java Code:
    public interface HelloInterface extends java.rmi.Remote
    {
    	public String sayHello() throws java.rmi.RemoteException;
    }
    HelloServer.java
    Java Code:
    import java.rmi.*;
    import java.rmi.server.*;
    import java.util.*;
    
    public class HelloServer extends UnicastRemoteObject implements HelloInterface
    {
    	private static final long serialVersionUID = 1L;
    
    	public HelloServer() throws RemoteException
    	{
    		super();
    	}
     
    	public String sayHello() throws RemoteException{
    		return "Hello World!";
    	}
    }
    RegisterIt.java
    Java Code:
    import java.rmi.*;
    
    public class RegisterIt
    {
    	public static void main(String args[])
    	{
    		try{
    			HelloServer obj=new HelloServer();
    			System.out.println("Object instantiated: "+obj);
    			Naming.rebind("/HelloServer",obj);
    			System.out.println("HelloServer bound in registery");
    		}catch(Exception e){
    			System.out.println(e);
    		} 
    	}
    }
    I hope someone can help me.

    Thanks,
    Dennis
    Last edited by Dennis; 05-17-2010 at 06:53 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Have you tried it outside of the IDE?
    What did your command line look like?
    Did it have -Djava.security.policy=... option?

  3. #3
    Dennis's Avatar
    Dennis is offline Member
    Join Date
    May 2010
    Posts
    83
    Rep Power
    0

    Default

    I don't know how I can test it outside of the IDE.
    Its not a applet, JFrame, Frame or another runnable program.
    He only did "System.out.println".

    What do you mean with "-Djava.security.policy=..."?
    It have a "security.policy" file, but thats not working.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    sorry, I don't know anything about your IDE.

    If you copy your classes to a work folder and open a command prompt (or write a batch file) you can issue the java command to execute your programs. One of the options of the java command is what I entered above: -D.... to point to your policy file.

    How does java connect to your security.policy file?

  5. #5
    Dennis's Avatar
    Dennis is offline Member
    Join Date
    May 2010
    Posts
    83
    Rep Power
    0

    Default

    command prompt doesn't work for execute java files at my computer.

    and there is no connection from the class to the policy file.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Sorry, I'm working on Windows and am used to using the command prompt. The IDEs have always been so slow I've never bothered trying to use them.
    What computer/OS are your working with?

    The "connection" I is was refering to was from the JRE. How does the java command see/read your policy file so that it can give the permissions your want?
    The -Djava.security.policy=... option on the command line is one way.

  7. #7
    Dennis's Avatar
    Dennis is offline Member
    Join Date
    May 2010
    Posts
    83
    Rep Power
    0

    Default

    I've a "Toshiba" computer and my OS is "Windows 7".

    My JRE can't setup connection (to my knowledge).
    The java-applet don't have connection with the policy file.

    I can't compile my java files in cmd.
    Last edited by Dennis; 05-18-2010 at 04:26 PM.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    After you compile the java programs, can you go to the folder with the .class files using a command prompt and execute the programs by using the java command?

    I don't see the "java-applet" you mention. Your code is all applications.

    I'll try to get it to work on my machine.

  9. #9
    Dennis's Avatar
    Dennis is offline Member
    Join Date
    May 2010
    Posts
    83
    Rep Power
    0

    Default

    Now I can compile .java files to .class files with cmd.

    I had installed the java JDK, but my path was wrong.

    But when I execute the "HelloClient" I recieve the same error.

    When I use the "-Djava.security.policy=..." command I recieve the same error again.
    Last edited by Dennis; 05-18-2010 at 06:30 PM.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    I copied your code to a folder, compiled it and used the following files to exec it:

    mypolicy.policy:
    Java Code:
    /* AUTOMATICALLY GENERATED ON Tue May 18 11:13:23 CDT 2010*/
    /* DO NOT EDIT */
    
    grant{  /* This one worked */
        permission java.security.AllPermission;
    };
    
    grant {  /* This one did not??? */
      permission java.net.SocketPermission "1099", "accept, connect, listen, resolve";
    };
    StartRegistry.bat:
    Java Code:
    start rmiregistry
    More
    RegisterIt.bat:
    Java Code:
    Echo Must start rmiregistry before starting this program
    Echo End this program when done by: Ctrl+C
    
    java.exe -Djava.security.manager -Djava.security.policy=mypolicy.policy -classpath . -Xmx128M RegisterIt
    MORE
    StartClient.bat
    Java Code:
    java.exe -Djava.security.manager -Djava.security.policy=mypolicy.policy -classpath . -Xmx128M HelloClient
    MORE

  11. #11
    Dennis's Avatar
    Dennis is offline Member
    Join Date
    May 2010
    Posts
    83
    Rep Power
    0

    Default

    When I've done that I got this error:
    HelloApplet exception: Connection refused to host: 192.168.0.2; nested exception is:
    java.net.ConnectException: Connection refused: connect

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Please show the FULL contents of the command prompt console for when you get the error.

    Where is the program: HelloApplet ? You don't show it in the code posted.

  13. #13
    Dennis's Avatar
    Dennis is offline Member
    Join Date
    May 2010
    Posts
    83
    Rep Power
    0

    Default



    There is no HelloApplet.
    Last edited by Dennis; 05-19-2010 at 07:39 PM.

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Don't know what the difference is. Here is the contents of RegisterIt.bat and StartClient.bat
    Do you have a valid .policy file?


    D:\JavaDevelopment\Testing\ForumQuestions3\RMI_Pro blem>java.exe -Djava.security.manager -Djava.security.policy=mypolicy.policy -classpath . -Xmx128M RegisterIt
    Object instantiated: HelloServer[UnicastServerRef [liveRef: [endpoint:[192.168.1.10:1092](local),objID:[-46035b7:128b2408208:-7fff, -1160124962239233764]]]]
    HelloServer bound in registery


    D:\JavaDevelopment\Testing\ForumQuestions3\RMI_Pro blem>java.exe -Djava.security.manager -Djava.security.policy=mypolicy.policy -classpath . -Xmx128M HelloClient
    Hello World!

    D:\JavaDevelopment\Testing\ForumQuestions3\RMI_Pro blem>MORE

  15. #15
    Dennis's Avatar
    Dennis is offline Member
    Join Date
    May 2010
    Posts
    83
    Rep Power
    0

    Default

    I got the same error, and my policy file is good.

    (but when this script work good, then I want it work without cmd.
    Does it is possible to add a policy file to the applet/application self? (without cmd)
    or add Permissions to a applet/application?)

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    my policy file is good
    How do you know this?
    Did you create it with the policytool?

    On Windows to execute any program their is a cmd/command issued somewhere.

    To give Permission to an applet, either sign the jar file or have all users make an entry in their .java.policy.

  17. #17
    Dennis's Avatar
    Dennis is offline Member
    Join Date
    May 2010
    Posts
    83
    Rep Power
    0

    Default

    I've coppied your policy file, I think its good.

    but my rmi-program didn't work.
    Do it work good at your computer?

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Yes, it worked on my computer.

    Open the .policy file in policytool, look at its contents, maybe add something to it and then save it again to be sure it has the correct contents.

  19. #19
    Dennis's Avatar
    Dennis is offline Member
    Join Date
    May 2010
    Posts
    83
    Rep Power
    0

    Default

    I've generated the security.policy with the policy tool, but I got the same error.

    RegisterIt works good.
    Java Code:
    Object instantiated: HelloServer[UnicastServerRef [liveRef: [endpoint:[192.168.1.10:1092](local),objID:[-46035b7:128b2408208:-7fff, -1160124962239233764]]]]
    HelloServer bound in registery
    But with HelloClient I got the error.
    Last edited by Dennis; 05-21-2010 at 06:22 PM.

  20. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Sorry, No more ideas. Your code works for me. Something must be different, but I don't know what.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •