0

Facing issue with RMI Registry.

Inititally my code was developed using java 6 or lower version but recently we have upgrade our java to java 8 and came to understand that it has some tighter Security policies.

Due to this my code is throwing a permission errors while establishing the RMI registry connection

I tried giving grant permission to port in java.policy file. Port used 1099 which default RMI Registry port.

Please find below code

package com.aurionpro.reuters;

import com.aurionpro.reuters.service.UpdateCurrencyRates;
import java.io.IOException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.Properties;

public final class RMIServiceClient {
   private final String hostname;
   private final int port;
   private final String serviceName;
   private UpdateCurrencyRates currencyRates;
   private Registry registry = null;

   public RMIServiceClient(String appPropFileName, String productName) {
      Properties properties = new Properties();
      ClassLoader loader = Thread.currentThread().getContextClassLoader();

      try {
         properties.load(loader.getResourceAsStream(appPropFileName));
      } catch (IOException var6) {
         var6.printStackTrace();
      }

      this.hostname = properties.getProperty("rmiclient." + productName + ".registry.host");
      this.port = Integer.parseInt(properties.getProperty("rmiclient." + productName + ".registry.port"));
      this.serviceName = properties.getProperty("rmiclient." + productName + ".registry.serviceName");
   }

   public UpdateCurrencyRates getCurrencyRatesService() throws RemoteException, NotBoundException {
      SecurityManager securityManager = new SecurityManager();
      System.setSecurityManager(securityManager);
      this.registry = LocateRegistry.getRegistry(this.hostname, this.port);
      this.currencyRates = (UpdateCurrencyRates)this.registry.lookup(this.serviceName);
      return this.currencyRates;
   }
}
4
  • 3
    You do know that the latest LTS release of Java is Java 21, right? As long as you have decided to go through a Java-version upgrade, it seems short sighted to step up only to 11-year-old Java 8. Commented Jun 13 at 11:55
  • 1
    As for the exception, I'm inclined to guess that either you did not set the policy correctly, or else you set it in the wrong policy file. Possible variations on that would include the policy file you modified not getting deployed with the application, or it getting replaced when the application is (re)deployed. You haven't provided enough information for us to evaluate any of that. Commented Jun 13 at 12:11
  • Does this help? stackoverflow.com/questions/2427473/… Commented Jun 14 at 21:30
  • 1
    How about not setting a SecurityManager? It has been marked, “deprecated, for removal” and the most recent Java version already removed the support; System.setSecurityManager(…) will throw an UnsupportedOperationException in Java 24. Even if it’s not deprecated in Java 8, the reasons for its deprecation apply to this version as well. Commented Jun 23 at 9:24

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.