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.
Related
Good evening,
I have been facing this error for a couple of days by now, and despite looking for a solution all over the web, I coul'd fix this error.
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
public class LDAPtest {
public static void main(String[] args) {
try {
String keystorePath = "C:/Program Files/Java/jdk-13.0.2/lib/security/cacerts";
System.setProperty("javax.net.ssl.keyStore", keystorePath);
System.setProperty("javax.net.ssl.keyStorePassword", "changeit");
Hashtable<String, String> ldapEnv = new Hashtable<>();
ldapEnv.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
ldapEnv.put(Context.PROVIDER_URL, "ldaps://localhost:10636");
ldapEnv.put(Context.SECURITY_AUTHENTICATION,"simple");
ldapEnv.put(Context.SECURITY_PRINCIPAL,"uid=admin,ou=system");
ldapEnv.put(Context.SECURITY_CREDENTIALS,"secret");
DirContext connection = new InitialDirContext(ldapEnv);
System.out.println("Benvenuto " + connection);
NamingEnumeration enm = connection.list("");
while (enm.hasMore()) {
System.out.println(enm.next());
}
enm.close();
connection.close();
}catch(Exception e) {
e.printStackTrace();
}
}
}
This code is actually working when SSL is not tested, replacing the
ldapEnv.put(Context.PROVIDER_URL, "ldaps://localhost:10636");
with
ldapEnv.put(Context.PROVIDER_URL, "ldap://localhost:10389");
I made the setup for the LDAP server with Apache Directory Studio, and followed this tutorial here in order to get the LDAPS to work:
http://directory.apache.org/apacheds/basic-ug/3.3-enabling-ssl.html
So I made the certificate, even installed it and imported it with keytool into cacerts.
I enabled portforwarding for the chosen port (10636), but still, I'm getting this exception:
javax.naming.CommunicationException: simple bind failed: localhost:10636 [Root exception is
java.net.SocketException: Connection or outbound has closed]
at java.naming/com.sun.jndi.ldap.LdapClient.authenticate(LdapClient.java:219)
at java.naming/com.sun.jndi.ldap.LdapCtx.connect(LdapCtx.java:2795)
at java.naming/com.sun.jndi.ldap.LdapCtx.<init>(LdapCtx.java:320)
at java.naming/com.sun.jndi.ldap.LdapCtxFactory.getLdapCtxFromUrl(LdapCtxFactory.java:225)
at java.naming/com.sun.jndi.ldap.LdapCtxFactory.getUsingURL(LdapCtxFactory.java:189)
at java.naming/com.sun.jndi.ldap.LdapCtxFactory.getUsingURLs(LdapCtxFactory.java:243)
at java.naming/com.sun.jndi.ldap.LdapCtxFactory.getLdapCtxInstance(LdapCtxFactory.java:154)
at java.naming/com.sun.jndi.ldap.LdapCtxFactory.getInitialContext(LdapCtxFactory.java:84)
at java.naming/javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:730)
at java.naming/javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:305)
at java.naming/javax.naming.InitialContext.init(InitialContext.java:236)
at java.naming/javax.naming.InitialContext.<init>(InitialContext.java:208)
at java.naming/javax.naming.directory.InitialDirContext.<init>(InitialDirContext.java:130)
at Prova3.main(Prova3.java:31)
Caused by: java.net.SocketException: Connection or outbound has closed
at java.base/sun.security.ssl.SSLSocketImpl$AppOutputStream.write(SSLSocketImpl.java:1246)
at java.base/java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:81)
at java.base/java.io.BufferedOutputStream.flush(BufferedOutputStream.java:142)
at java.naming/com.sun.jndi.ldap.Connection.writeRequest(Connection.java:398)
at java.naming/com.sun.jndi.ldap.Connection.writeRequest(Connection.java:371)
at java.naming/com.sun.jndi.ldap.LdapClient.ldapBind(LdapClient.java:359)
at java.naming/com.sun.jndi.ldap.LdapClient.authenticate(LdapClient.java:214)
... 13 more
Thank you in advance
For Googlers:
simple bind failed errors are almost always related to SSL connection.
With nc or telnet, check whether a connection can be established between client and remote host and port.
With SSLPoke.java (a simple Java class to check SSL connection), check whether certificates are correctly imported and used, also check correct TLS version. Use something like java -Dhttps.protocols=TLSv1,TLSv1.1,TLSv1.2 -Djavax.net.debug=all SSLPoke google.com 443 > log.txt 2>&1.
Look for:
Warning: no suitable certificate found - continuing without client authentication = check whether you have set javax.net.ssl.trustStore
Fatal (HANDSHAKE_FAILURE): Couldn't kickstart handshaking = could be mismatched TLS versions
Also check whether your intermediate CA is expired
I am trying to access a jms queue configured on Websphere Application Server 8. But I am getting java.lang.NoClassDefFoundError: com.ibm.ws.naming.util.CommonHelpers Exception.
Can anyone suggest a solution? Does any jar file needs to be added?
I have already added -
com.ibm.ws.messagingClient.jar
com.ibm.ws.sib.client_ExpeditorDRE_8.0.0.jar
com.ibm.ws.sib.client.thin.jms_8.0.0.jar
com.ibm.xml.thinclient_8.0.0.jar
in classpath.
You can find the code at How to set Context.INITIAL_CONTEXT_FACTORY? NoInitialContextException?
package com.jms.test;
import java.util.Hashtable;
import javax.jms.Queue;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class Demo {
public static void main(String[] args) throws NamingException {
System.out.println("Start.....");
Hashtable environment = new Hashtable();
environment.put(Context.INITIAL_CONTEXT_FACTORY, "com.ibm.websphere.naming.WsnInitialContextFactory");
environment.put(Context.PROVIDER_URL, "iiop://localhost:2810");
//String pUrl = System.getProperty(Context.PROVIDER_URL);
//System.out.println("*******"+pUrl+"********");
InitialContext ctx = new InitialContext(environment);
Queue queue = (Queue) ctx.lookup("jms/inQueue");
System.out.println("*** Queue is *** "+queue.toString());
}}
Exception Stack Trace -
Exception in thread "main" java.lang.NoClassDefFoundError: com.ibm.ws.naming.util.CommonHelpers
at com.ibm.websphere.naming.WsnInitialContextFactory.getInitialContext(Unknown Source)
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:230)
at javax.naming.InitialContext.initializeDefaultInitCtx(InitialContext.java:313)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:343)
at javax.naming.InitialContext.internalInit(InitialContext.java:281)
at javax.naming.InitialContext.<init>(InitialContext.java:206)
at com.jms.test.Demo.main(Demo.java:16)
Caused by: java.lang.ClassNotFoundException: com.ibm.ws.naming.util.CommonHelpers
at java.net.URLClassLoader.findClass(URLClassLoader.java:434)
at java.lang.ClassLoader.loadClass(ClassLoader.java:646)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:358)
at java.lang.ClassLoader.loadClass(ClassLoader.java:612)
... 7 more
Try to add com.ibm.ws.admin.client_8.5.0.jar it is in the WebSphere\AppServer85\runtimes folder. CommonHelpers class is in that jar.
UPDATE for comment
This might be related to SSL required. Please check these two post how to disable SSL:
initial and forwarded IOR inaccessible
Connecting to JMS queues in WAS8.0 Server
I'm new to java oracle database coding and I'm getting this error when running the code below (eclipse kepler):
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class main {
public static void main(String[] args) {
try {
Class.forName("oracle.jdbc.OracleDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
return;
}
Connection connection = null;
try {
connection = DriverManager.getConnection(
"jdbc:oracle:thin:#10.0.32.166:1521:orcl", "username", "password");
} catch (SQLException e) {
e.printStackTrace();
return;
}
}
}
Error is for ojdbc14.jar:
java.sql.SQLException: Listener refused the connection with the following error:
ORA-12505, TNS:listener does not currently know of SID given in connect descriptor
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:113)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:263)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:389)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:454)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:165)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:35)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:802)
at java.sql.DriverManager.getConnection(DriverManager.java:571)
at java.sql.DriverManager.getConnection(DriverManager.java:215)
at oracle.main.main(main.java:18)
While I tried to change ojdbc6.jar but a different error shows up:
java.sql.SQLException: Listener refused the connection with the following error:
ORA-12505, TNS:listener does not currently know of SID given in connect descriptor
at oracle.jdbc.driver.SQLStateMapping.newSQLException(SQLStateMapping.java:70)
at oracle.jdbc.driver.DatabaseError.newSQLException(DatabaseError.java:133)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:199)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:480)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:413)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:508)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:203)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:33)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:510)
at java.sql.DriverManager.getConnection(DriverManager.java:571)
at java.sql.DriverManager.getConnection(DriverManager.java:215)
at oracle.main.main(main.java:18)
Caused by: oracle.net.ns.NetException: Listener refused the connection with the following error:
ORA-12505, TNS:listener does not currently know of SID given in connect descriptor
at oracle.net.ns.NSProtocol.connect(NSProtocol.java:361)
at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:966)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:292)
... 7 more
Reason I'm experimenting on the 2 jar is I'm not sure which is compatible to jdk 1.6 as this is were I'm gonna deploy (linux env) the executable jar once completed. Need help to identify what these errors are and ideas to fix them? thank you
You do not have a database "called" orcl on the database server 10.0.32.166 with an oracle listener running on port 1521.
Do lsnrctl statusfrom the console on server 10.0.32.166 and see if you have a line like Service "ORCL" has X instance(s). If you don't have that make sure the database is up and you have the correct SID and it is using the listener on port 1521.
I'm developing a Java EE 6 application that uses AD to help with logging in. My authenticator is simple and looks like this:
#Singleton
#TransactionManagement(TransactionManagementType.BEAN)
public class ADAuthenticator{
private static final String ldapHost = "ldap://domainname.mycompany.com:389";
private static final String domain = "domainname";
public ADAuthenticator() {
}
public void authenticate(String user, String pass) throws NamingException{
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, ldapHost);
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, user + "#" + domain);
env.put(Context.SECURITY_CREDENTIALS, pass);
LdapContext ctxGC = new InitialLdapContext(env, null);
}
}
This has worked well for some time, but lately I'm getting the following exception (except some times where it magically works):
javax.naming.CommunicationException: mydomain.mycompany.com:389 [Root exception is java.net.ConnectException: Connection timed out: connect]
at com.sun.jndi.ldap.Connection.<init>(Connection.java:210)
at com.sun.jndi.ldap.LdapClient.<init>(LdapClient.java:118)
at com.sun.jndi.ldap.LdapClient.getInstance(LdapClient.java:1580)
at com.sun.jndi.ldap.LdapCtx.connect(LdapCtx.java:2652)
at com.sun.jndi.ldap.LdapCtx.<init>(LdapCtx.java:293)
at com.sun.jndi.ldap.LdapCtxFactory.getUsingURL(LdapCtxFactory.java:175)
at com.sun.jndi.ldap.LdapCtxFactory.getUsingURLs(LdapCtxFactory.java:193)
at com.sun.jndi.ldap.LdapCtxFactory.getLdapCtxInstance(LdapCtxFactory.java:136)
at com.sun.jndi.ldap.LdapCtxFactory.getInitialContext(LdapCtxFactory.java:66)
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:667)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
at javax.naming.InitialContext.init(InitialContext.java:223)
at javax.naming.ldap.InitialLdapContext.<init>(InitialLdapContext.java:134)
at com.xdin.competence.util.ADAuthenticator.authenticate(ADAuthenticator.java:35)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at <ejb stuff omitted>
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:529)
at java.net.Socket.connect(Socket.java:478)
at java.net.Socket.<init>(Socket.java:375)
at java.net.Socket.<init>(Socket.java:189)
at com.sun.jndi.ldap.Connection.createSocket(Connection.java:352)
at com.sun.jndi.ldap.Connection.<init>(Connection.java:187)
... 101 more
Nothing has changed with the code, and according to IT-support, no configuration regarding AD has been changed either.
I seperated the relevant code into a simple console application, and it works perfectly with the exact same settings. So the problem seems related to Java EE or the webserver. Any ideas?
The packets for the TCP/IP connection do not arrive in time. This may have multiple reasons, but from what you describe, this may simply be a network congestion between your Java code and the LDAP server which in turn causes package loss. There might also be a firewall blocking that port inbetween.
You need to learn about the ping, traceroute and telnet host port tools to troubleshoot your network layer. I would suggest you team up with a network guy from IT-support and ask him nicely to help finding out where your package loss is.
Hi friends i am facing the following error while trying to connect to a database through java code:
Exception in thread "Main Thread" java.lang.ClassNotFoundException: oracle.jdbc.OracleDriver
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at com.example.model.Driver.main(Driver.java:13)
My java code is:
package com.example.model;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Driver {
public static void main(String args[]) throws ClassNotFoundException,
SQLException {
Class.forName("oracle.jdbc.OracleDriver");
// or you can use:
// DriverManager.registerDriver(
// new oracle.jdbc.driver.OracleDriver());
Connection conn = DriverManager.getConnection(
"jdbc:oracle:thin:#127.0.0.1:1521:ORCL", "scott", "tiger");
Statement stmt = conn.createStatement();
ResultSet rset = stmt.executeQuery("select * from customer");
while (rset.next())
System.out.println(rset.getString(1));
rset.close();
stmt.close();
conn.close();
}
}
As per i came to know it could be due to class path issue but don't know how to resolve it.
I set my class path to
C:\bea\user_project\workspace\wlserver_10.3\server\ext\jdbc\oracle\11g\ojdbc5.jar;
I am using weblogic 10.3 workspace and weblogic 10.3 server.
The java.lang.ClassNotFoundException: oracle.jdbc.OracleDriver definitely means that the mentioned class is not on the class path. And because ojdbc5.jar has it, the whole question is: "how did you set your classpath"?
Here is what I get on my machine using your code (using the default package):
$ cat > Driver.java
...
$ javac Driver.java
$ java -cp /home/pascal/opt/Oracle/Middleware/wlserver_10.3/server/ext/jdbc/oracle/11g/ojdbc5.jar:. Driver
Exception in thread "main" java.sql.SQLException: The Network Adapter could not establish the connection
at oracle.jdbc.driver.SQLStateMapping.newSQLException(SQLStateMapping.java:70)
at oracle.jdbc.driver.DatabaseError.newSQLException(DatabaseError.java:131)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:197)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:525)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:413)
at oracle.jdbc.driver.PhysicalConnection.(PhysicalConnection.java:508)
at oracle.jdbc.driver.T4CConnection.(T4CConnection.java:203)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:33)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:510)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at Driver.main(Driver.java:15)
Caused by: oracle.net.ns.NetException: The Network Adapter could not establish the connection
at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:328)
at oracle.net.resolver.AddrResolution.resolveAndExecute(AddrResolution.java:421)
at oracle.net.ns.NSProtocol.establishConnection(NSProtocol.java:634)
at oracle.net.ns.NSProtocol.connect(NSProtocol.java:208)
at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:966)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:292)
... 7 more
Caused by: java.net.ConnectException: Connection refused
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:529)
at java.net.Socket.connect(Socket.java:478)
at java.net.Socket.(Socket.java:375)
at java.net.Socket.(Socket.java:189)
at oracle.net.nt.TcpNTAdapter.connect(TcpNTAdapter.java:127)
at oracle.net.nt.ConnOption.connect(ConnOption.java:126)
at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:306)
... 12 more
The driver is found (I get an exception because I'm not running any Oracle server but this is another story).
I will try to put it in the weblogic's lib folder... and restart the server, so you will be sure that it is a classpath problem...
You should download the oracle drivers from oracle and put the .jar file in the project's CLASSPATH...
You can download it at Oracle's drivers download page