I am learning about Java RMI and have an example code to show how RMI can be used to pass objects to another virtual machines over a network.
Here are the classes and code I am using;
//interface
import java.rmi.*;
public interface MyRemote extends Remote
{
public String sayHello() throws RemoteException;
}
//My Remote Implementation Class
import java.rmi.*;
import java.rmi.server.*;
public class MyRemoteImpl extends UnicastRemoteObject implements MyRemote
{
public String sayHello()
{
return "Server says, 'Hey' ";
}
public MyRemoteImpl() throws RemoteException
{
}
public static void main (String[] args)
{
try
{
MyRemote service = new MyRemoteImpl();
Naming.rebind("Remote Hello", service);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
//My Remote Client Class
import java.rmi.*;
public class MyRemoteClient
{
public static void main (String [] args)
{
new MyRemoteClient().go();
}
public void go()
{
try{
MyRemote service = (MyRemote) Naming.lookup("rmi://127.0.0.1/Remote Hello");
String s = service.sayHello();
System.out.println(s);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
I keep getting java.net.MalformedURLException: invalid URL String: rmi://127.0.0.1/Remote Hello which I know is due to the 'Naming.lookup("rmi://127.0.0.1/Remote Hello")' code in the last class.
I have installed tomcat and tried placing the files in a directory, trying to get it to run on my local machine but I have had no joy.
I have created a JAR file of my project and tried placing that in a directory also and still no luck. I have heard about war files but currently the book I am learning from hasnt come to that yet...
Any tips on how I can get around this without the exception being called? Am I placing the files incorrectly in Tomcat? I have placed them in my C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps directory.
Any help or tips will be greatly appreciated.
UPDATE
Thanks for the advice, the String exception seems to have gone away but now I am lumbered with this.
java.rmi.ConnectException: Connection refused to host: 127.0.0.1; nested exception is:
java.net.ConnectException: Connection refused: connect
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:619)
at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:216)
at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:202)
at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:340)
at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
at java.rmi.Naming.lookup(Naming.java:101)
at MyRemoteClient.go(MyRemoteClient.java:13)
at _SHELL57.run(_SHELL57.java:10)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:623)
at bluej.runtime.ExecServer$3.run(ExecServer.java:774)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:69)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:316)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:177)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:164)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:154)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:352)
at java.net.Socket.connect(Socket.java:569)
at java.net.Socket.connect(Socket.java:519)
at java.net.Socket.(Socket.java:416)
at java.net.Socket.(Socket.java:199)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:40)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:146)
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:613)
2 things:
Change the name of the remote object to something without a space.
Are you running the server code first?
What does Tomcat have to do with RMI? Tomcat is a servlet/JSP engine that listens for HTTP requests on port 8080 by default.
RMI is a totally different protocol that has nothing to do with HTTP. The RMI daemon uses port 1099 by default.
I'd recommend looking at this.
Check your /etc/hosts file at the server. It should map localhost to 127.0.0.1 and your real hostname to your real IP address. If you have a configuration where you need the other way round, set the system property java.rmi.server.hostname to the correct IP address at the server JVM before exporting the remote object.
Related
I've tried to used RMI, here is server side. at first it worked without any exception, but now after three times whenever i try to run the below code, i will get some errors
The code is:
import java.rmi.server.UnicastRemoteObject;
/**
* Created by elyas on 12/11/14 AD.
*/
public class LogicImplement extends UnicastRemoteObject implements Logic
{
public LogicImplement() throws Exception
{
java.rmi.registry.LocateRegistry.createRegistry(6060);
java.rmi.Naming.rebind("Object1",this);
}
#Override
public int sum(int a, int b) throws Exception
{
int result = a + b;
System.out.println("ana sum executed");
return result;
}
public static void main(String[] args)
{
try
{
LogicImplement logicImplement = new LogicImplement();
} catch (Exception e)
{
e.printStackTrace();
}
}
}
The error is like this: i've tried to change the Object1 to for example Object2, but again i will get error, also i change the port number...
what is solution?
java.rmi.ConnectException: Connection refused to host: 127.0.0.1; nested exception is:
java.net.ConnectException: Connection refused
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:619)
at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:216)
at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:202)
at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:341)
at sun.rmi.registry.RegistryImpl_Stub.rebind(Unknown Source)
at java.rmi.Naming.rebind(Naming.java:177)
at LogicImplement.<init>(LogicImplement.java:12)
at LogicImplement.main(LogicImplement.java:27)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at java.net.Socket.<init>(Socket.java:425)
at java.net.Socket.<init>(Socket.java:208)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:40)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:147)
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:613)
... 12 more
java.net.ConnectException: Connection refused
There could be couple of reasons for this exception:
You have not started your rmiregistry in background.
You are trying to connect to the wrong port number.
Your firewall maybe blocking the connections.
The Answer was very simple, By default, the registry runs on port 1099. To start the registry on a different port, specify the port number on the command line. Do not forget to unset your CLASSPATH environment variable. for more information check this link: Running the Example Programs
** So for fixing this code i must change the port number form 6060 to 1099
notice that: if 1099 is used by other services you have to test 1100, and if 1100 is used too, you have yo use 1101 and so on. :-)
Since this was a port number issue, there is another way to start the registry on the port number you want:
java.rmi.registry.Registry rmiRegistry = java.rmi.registry.LocateRegistry.
createRegistry(6060); // Creates a registry on 6060
rmiRegistry.rebind("Object1",this); // Binds to registry created on 6060
You haven't started the RMI Registry.
When you get past this, if it still happens when calling the remote method, see item A.1 of the RMI FAQ.
I realize this question has been asked many times and answered many times (often from EJP, who clearly knows his stuff!), but I am still struggling.
I have "stolen" a simple RMI Adding Server and Client.
(Thank you,
http://www.scs.ryerson.ca/mes/courses/cps530/programs/rmi/Schildt/addTwoNumbers.html who then gives credit to Chapter 24 of "Java 2: The Complete Reference" by P.Naughton and H.Schildt.)
No matter what I try I still get this "Connection timed out" Exception.
Can anyone see what I've done wrong?
I suspect that there's something going on with the firewall, but if so, I don't know what to tell my IT guys to fix and change.
Thanks in advance!
Interface:
public interface AddServerIntf extends Remote {
double add(double d1, double d2) throws RemoteException;
}
Server:
public class SimpleRMIExServer {
public static void main(String[] args) throws Exception {
System.out.println("Class name " + SimpleRMIExServer.class.getSimpleName());
System.out.println("Path " + System.getProperty("user.dir"));
System.setProperty("java.rmi.server.codebase",
"file:///D:\\SimpleRMIExample\\lib\\SimpleRMIExample.jar");
Registry registry;
try {
registry = LocateRegistry.createRegistry(1099);
} catch (RemoteException remoteException) {
registry = LocateRegistry.getRegistry(1099);
}
AddServerImpl addServerImpl = new AddServerImpl();
registry.bind("AddServer", addServerImpl);
System.out.println("Located Registry " + registry.toString());
for (String boundName : registry.list()) {
System.out.println("Name bound: " + boundName);
}
Naming.rebind("SERVERNAME", addServerImpl);
System.out.println("Registry: " + registry);
System.out.println("");
System.out.println("Remote: " + addServerImpl);
}
}
public class AddServerImpl extends UnicastRemoteObject implements AddServerIntf {
public AddServerImpl() throws RemoteException {
}
#Override
public double add(double d1, double d2) throws RemoteException {
return d1 + d2;
}
}
Client:
public class SimpleRMIExClient {
public static void main(String[] args) throws Exception {
final Registry registry = LocateRegistry.getRegistry("SERVERNAME", 1099);
AddServerIntf addServerIntf = (AddServerIntf) registry.lookup("AddServer");
System.out.println("The first number is: " + 10);
double d1 = Double.valueOf(10).doubleValue();
System.out.println("The second number is: " + 15);
double d2 = Double.valueOf(15).doubleValue();
System.out.println("The sum is: " + addServerIntf.add(d1, d2)); //Exception occurs here.
}
}
Output From Server:
D:\>java -jar SimpleRMIExServer.jar
Class name SimpleRMIExServer
Path D:\
Located Registry RegistryImpl[UnicastServerRef [liveRef: [endpoint:[XX.XX.XX.XX:
1099](local),objID:[0:0:0, 0]]]]
Name bound: AddServer
Registry: RegistryImpl[UnicastServerRef [liveRef: [endpoint:[XX.XX.XX.XX:1099](l
ocal),objID:[0:0:0, 0]]]]
Remote: AddServerImpl[UnicastServerRef [liveRef: [endpoint:[XX.XX.XX.XX:54119](l
ocal),objID:[2a7202aa:14492e5ea89:-7fff, -5034491972061237368]]]]
Output From Client:
debug:
The first number is: 10
The second number is: 15
Exception in thread "main" java.rmi.ConnectException: Connection refused to host: XX.XX.XX.XX; nested exception is:
java.net.ConnectException: Connection timed out: connect
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:619)
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:619)
at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:216)
at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:202)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:129)
at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(RemoteObjectInvocationHandler.java:194)
at java.rmi.server.RemoteObjectInvocationHandler.invoke(RemoteObjectInvocationHandler.java:148)
at com.sun.proxy.$Proxy0.add(Unknown Source)
at com.sun.proxy.$Proxy0.add(Unknown Source)
at simplermiexclient.SimpleRMIExClient.main(SimpleRMIExClient.java:35)
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at java.net.Socket.<init>(Socket.java:425)
at java.net.Socket.<init>(Socket.java:208)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:40)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:147)
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:613)
... 7 more
Java Result: 1
BUILD SUCCESSFUL (total time: 1 minute 33 seconds)
Additionally, I used the command netstat -a and it showed:
LocalAddress ForeignAddress State
0.0.0.0:1099 SERVERNAME:0 LISTENING
[::]:1099 SERVERNAME:0 LISTENING
XX.XX.XX.XX:53373 SERVERNAME:1099 ESTABLISHED
(Among many other lines)
I have also seen:
LocalAddress ForeignAddress State
XX.XX.XX.XX:54138 SERVERNAME:1099 TIME_WAIT
Edit:
I tried adding in:
System.setProperty("java.rmi.server.hostname", "SERVERNAME");
to the server right after setting the codebase System property, based on EJP's recommendation to so many others to read Java RMI FAQ A.1.
When I did, this was the stack trace:
debug:
The first number is: 10
The second number is: 15
Exception in thread "main" java.rmi.ConnectException: Connection refused to host: SERVERNAME; nested exception is:
java.net.ConnectException: Connection timed out: connect
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:619)
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:619)
at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:216)
at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:202)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:129)
at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(RemoteObjectInvocationHandler.java:194)
at java.rmi.server.RemoteObjectInvocationHandler.invoke(RemoteObjectInvocationHandler.java:148)
at com.sun.proxy.$Proxy0.add(Unknown Source)
at com.sun.proxy.$Proxy0.add(Unknown Source)
at simplermiexclient.SimpleRMIExClient.main(SimpleRMIExClient.java:35)
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at java.net.Socket.<init>(Socket.java:425)
at java.net.Socket.<init>(Socket.java:208)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:40)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:147)
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:613)
... 7 more
Java Result: 1
BUILD SUCCESSFUL (total time: 1 minute 15 seconds)
Answering my own question:
I have figured out the answer to my problem. I love to see others do that, it's good to be able to say it myself.
My final solution was a firewall problem, but I'd like to provide suggestions to anyone out there for HOW I solved my problem.
Break it down. Take my code above that really isn't my code and make a much simpler version of the RMI server and test it.
Start small and work your way up. Start with localhost make sure that works, then test on a separate PC (I had a virtual PC), then test on your server.
GET HELP! I searched the Internet A LOT. Thank you, EJP! Also, once I finally got it working on the Virtual PC, but not on the server, then I go my IT guys involved and they found a difference between the two. "There was a firewall setting on the PC that was related to Java that was not set on the server." - IT Guy (See: http://windows.microsoft.com/en-us/windows/communicate-through-windows-firewall#1TC=windows-7)
I realize what I've said here is nothing new and I know I've heard it many times, but sometimes it takes hearing it a thousand times to finally accept. Although it hasn't worked with my kids yet. ;-)
Good Luck.
(Answered by a question edit. Converted to a community wiki answer. See Question with no answers, but issue solved in the comments (or extended in chat) )
The OP wrote:
Answering my own question: I have figured out the answer to my problem. I love to see others do that, it's good to be able to say it myself.
My final solution was a firewall problem, but I'd like to provide suggestions to anyone out there for HOW I solved my problem.
Break it down. Take my code above that really isn't my code and make
a much simpler version of the RMI server and test it.
Start small and work your way up. Start with localhost make sure
that works, then test on a separate PC (I had a virtual PC), then
test on your server.
GET HELP! I searched the Internet A LOT. Thank you, EJP! Also, once
I finally got it working on the Virtual PC, but not on the server,
then I go my IT guys involved and they found a difference between
the two. "There was a firewall setting on the PC that was related to
Java that was not set on the server." - IT Guy (See:
http://windows.microsoft.com/en-us/windows/communicate-through-windows-firewall#1TC=windows-7
)
I realize what I've said here is nothing new and I know I've heard it many times, but sometimes it takes hearing it a thousand times to finally accept. Although it hasn't worked with my kids yet. ;-)
Good Luck.
We have several server side components in our architecture. Each component uses JMX to expose various internal attributes. Initialization is done as follows:
try {
Registry registry = null;
for(int i = _serverInfo.getJMXStartPort(); i <= _serverInfo.getJMXEndPort(); i++) {
try {
registry = LocateRegistry.createRegistry(i);
if(registry != null) {
_statusPort = i;
logger.info("Using JMX port: "+_statusPort);
break;
}
} catch (Exception e) {
_statusPort++;
}
}
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
_abstractServiceController = new AbstractServiceController(this);
ObjectName mbeanName = new ObjectName("MyServer:name=MyServer Service");
mbs.registerMBean(_abstractServiceController, mbeanName);
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://:"+_statusPort+"/jmxrmi");
JMXConnectorServer cs = JMXConnectorServerFactory.newJMXConnectorServer(url, System.getenv(), mbs);
cs.start();
} catch (Throwable e) {
logger.error("Unable to register MBean with JMX");
e.printStackTrace();
}
I guess I have two questions.
Does this look right?
The bigger question is, while this runs fine on java 1.6 (each subsequent server on a host uses the next available port, since LocateRegistry.createRegistry(i) throws an exception if the port is unavailable), not so on 1.7. As a result, we get the following exception when the second server attempts to JMXConnectorServer.start(). Does anyone know if the behavior changed for createRegistry? If so, is there something else we should do?
2013-02-07 15:34:28,451 INFO [main] Using JMX port: 9500
2013-02-07 15:34:28,929 ERROR [main] Unable to register MBean with JMX
java.io.IOException: Cannot bind to URL [rmi://:9500/jmxrmi]: javax.naming.NameAlreadyBoundException: jmxrmi [Root exception is java.rmi.AlreadyBoundException: jmxrmi]
at javax.management.remote.rmi.RMIConnectorServer.newIOException(RMIConnectorServe.java:826)
at javax.management.remote.rmi.RMIConnectorServer.start(RMIConnectorServer.java:431)
at com.theatre.services.framework.AbstractService.run(AbstractService.java:306)
at com.theatre.services.reporttree.TreeServerImpl.run(TreeServerImpl.java:690)
at com.theatre.services.framework.Launcher.main(Launcher.java:99)
Caused by: javax.naming.NameAlreadyBoundException: jmxrmi [Root exception is java.rmi.AlreadyBoundException: jmxrmi]
at com.sun.jndi.rmi.registry.RegistryContext.bind(RegistryContext.java:139)
at com.sun.jndi.toolkit.url.GenericURLContext.bind(GenericURLContext.java:226)
at javax.naming.InitialContext.bind(InitialContext.java:419)
at javax.management.remote.rmi.RMIConnectorServer.bind(RMIConnectorServer.java:643)
at javax.management.remote.rmi.RMIConnectorServer.start(RMIConnectorServer.java:426)
... 3 more
Caused by: java.rmi.AlreadyBoundException: jmxrmi
at sun.rmi.registry.RegistryImpl.bind(RegistryImpl.java:131)
at sun.rmi.registry.RegistryImpl_Skel.dispatch(Unknown Source)
at sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:390)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:248)
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)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:273)
at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:251)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:377)
at sun.rmi.registry.RegistryImpl_Stub.bind(Unknown Source)
at com.sun.jndi.rmi.registry.RegistryContext.bind(RegistryContext.java:137)
... 7 more
Does this look right?
No. Creating a Registry can fail for several reasons, not just because the port is in use.
registry cannot be null after createRegistry(), so testing for it is pointless.
If you're trying to find a free port, just open (and close) a ServerSocket(). Then create the Registry on that port if it worked.
The bigger question is, while this runs fine on java 1.6 (each subsequent server on a host uses the next available port, since LocateRegistry.createRegistry(i) throws an exception if the port is unavailable), not so on 1.7.
See above. Creating a Registry can also fail if there is already one running on that port, in any JDK. In earlier JDKs it would fail if there was one running on any port in the same JVM.
I am trying to run following program:
package jndi;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
public class LDAPRead {
public static void main(String[] args) {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=jaydeetechnology");
try{
System.out.println("creating initial directory context");
DirContext ctx = (DirContext) new InitialContext(env);
System.out.println("search for john hunt");
Attributes attrs = ctx.getAttributes("cn=John Hunt , ou=JayDeeTechnology");
System.out.println("find the surname and print it");
System.out.println("sn: "+attrs.get("sn").get());
ctx.close();
}catch(NamingException e){
e.printStackTrace();
}
}
}
but I am getting 'Connection refused' error. Could any one please help me if I am missing something?
creating initial directory context
javax.naming.CommunicationException: localhost:389 [Root exception is java.net.ConnectException: Connection refused: connect]
at com.sun.jndi.ldap.Connection.<init>(Connection.java:222)
at com.sun.jndi.ldap.LdapClient.<init>(LdapClient.java:130)
at com.sun.jndi.ldap.LdapClient.getInstance(LdapClient.java:1592)
at com.sun.jndi.ldap.LdapCtx.connect(LdapCtx.java:2664)
at com.sun.jndi.ldap.LdapCtx.<init>(LdapCtx.java:305)
at com.sun.jndi.ldap.LdapCtxFactory.getUsingURL(LdapCtxFactory.java:187)
at com.sun.jndi.ldap.LdapCtxFactory.getUsingURLs(LdapCtxFactory.java:205)
at com.sun.jndi.ldap.LdapCtxFactory.getLdapCtxInstance(LdapCtxFactory.java:148)
at com.sun.jndi.ldap.LdapCtxFactory.getInitialContext(LdapCtxFactory.java:78)
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:235)
at javax.naming.InitialContext.initializeDefaultInitCtx(InitialContext.java:318)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:348)
at javax.naming.InitialContext.internalInit(InitialContext.java:286)
at javax.naming.InitialContext.<init>(InitialContext.java:211)
at jndi.LDAPRead.main(LDAPRead.java:31)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:383)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:245)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:232)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:377)
at java.net.Socket.connect(Socket.java:539)
at java.net.Socket.connect(Socket.java:488)
at java.net.Socket.<init>(Socket.java:385)
at java.net.Socket.<init>(Socket.java:199)
at com.sun.jndi.ldap.Connection.createSocket(Connection.java:364)
at com.sun.jndi.ldap.Connection.<init>(Connection.java:199)
... 14 more
I am using RSA 8.0
From the Adobe LDAP Troubleshooting pages:
Error: javax.naming.CommunicationException: [server]:[port] [Root exception is java.net.ConnectException: Connection refused: connect]
Cause: The port name you have specified for the LDAP/AD server is incorrect.
I'd say your using the wrong hostname, the wrong port number, or haven't started you LDAP installation on that server yet.
Try looking in the LDAP server's logs, perhaps you can learn a bit more from there.
'Connection refused' has the same meaning here as everywhere else. Nothing was listening at the IP:port you tried to connect to. Either the service hadn't started or you got the IP or port wrong.
This question already has answers here:
What is a connection timeout during a http request
(2 answers)
Closed last month.
I get an exception when I run this code. Why?
Exception in thread "main" java.net.ConnectException: Connection timed out: connect
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
public class MainClass {
public static void main(String[] args) throws Exception {
System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");
URL url = new URL("https://www.verisign.com/");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
in.close();
}
}
Exception:
Exception in thread "main" java.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:525)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:550)
at com.sun.net.ssl.internal.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:141)
at sun.net.NetworkClient.doConnect(NetworkClient.java:163)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:394)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:529)
at sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:272)
at sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:329)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:172)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:801)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:158)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1049)
at com.sun.net.ssl.internal.www.protocol.https.HttpsURLConnectionOldImpl.getInputStream(HttpsURLConnectionOldImpl.java:204)
at java.net.URL.openStream(URL.java:1010)
at https.ssl.MainClass.main(MainClass.java:13)
We can't diagnose your networks for you. You need to do it yourself, or get your local admins to look at.
Things you should check before you bug your admins:
can you ping the host?
can you connect to http://www.verisign.com using a web browser?
can you connect to https://www.verisign.com using a web browser?
can you connect to http://www.verisign.com using your program?
can you connect to anything using your program?
The chances are that your problem is firewall related. My first guess would be that you don't have the correct environment variables or Java system properties set to tell the JVM to use a local proxy server for outgoing HTTP / HTTPS requests.
If it is not a problem with your settings, you will need to get help from someone local who can help you diagnose the problem.