This error is from a PC connected to a comcast modem. Is port blocking an issue when a person is trying to make an outbound TCP channel with a Java URL . . . the relevant code is :
URL u = new URL("mailto:someguy#gmail.com"); // make a mailto: url
URLConnection c = u.openConnection(); // make its urlconnection
c.connect(); // connect to mail host
catch (Exception e) { // handle any exceptions, print error message
System.err.println(e);
}
The console returns the message :
connect. Timeout = -1
java.net.UnknownHostException: mailhost
MailToURLConnection is NOT in Java 6 or 7 or any earlier version. It is part of the OpenJDK implementation of J2SE specifications. Download and install OpenJDK and use that as your Java JDK and the problem will probably go away. I have checked, OpenJDK 6 has the MailToURLConnection class.
This exception java.net.UnknownHostException: mailhost is thrown to indicate that the IP address of a host could not be determined.
I reckon you might be trying to send a mail.
I cannot find any documentation but it seems URL.openConnection() above creates a sun.net.www.protocol.mailto.MailToURLConnection. Looking at the code here it creates an SmtpClient.
This creates a connection to System.getProperty("mail.host") or to "localhost" or to "mailhost". I guess the "mail.host" is not defined for you and localhost is not accepting the connection so the dns error for mailhost.
Where did find information to use "mailto:" anyway? That same place should also have the rest of usage documentation. If you were just trying something or ran into it accidentally then I would advice not to use it.
Related
I am trying to connect to Hive2 server via JDBC with kerberos authentication. After numerous attempts to make it work, I can't get it to work with the Cloudera driver.
If someone can help me to solve the problem, I can greatly appreciate it.
I have this method:
private Connection establishConnection() {
final String driverPropertyClassName = "driver";
final String urlProperty = "url";
Properties hiveProperties = config.getMatchingProperties("hive.jdbc");
String driverClassName = (String) hiveProperties.remove(driverPropertyClassName);
String url = (String) hiveProperties.remove(urlProperty);
Configuration hadoopConfig = new Configuration();
hadoopConfig.set("hadoop.security.authentication", "Kerberos");
String p = config.getProperty("hadoop.core.site.path");
Path path = new Path(p);
hadoopConfig.addResource(path);
UserGroupInformation.setConfiguration(hadoopConfig);
Connection conn = null;
if (driverClassName != null) {
try {
UserGroupInformation.loginUserFromKeytab(config.getProperty("login.user"), config.getProperty("keytab.file"));
Driver driver = (Driver) Class.forName(driverClassName).newInstance();
DriverManager.registerDriver(driver);
conn = DriverManager.getConnection(url, hiveProperties);
} catch (Throwable e) {
LOG.error("Failed to establish Hive connection", e);
}
}
return conn;
}
URL for the server, that I am getting from the properties in the format described in Cloudera documentation
I am getting an exception:
2018-05-05 18:26:49 ERROR HiveReader:147 - Failed to establish Hive connection
java.sql.SQLException: [Cloudera][HiveJDBCDriver](500164) Error initialized or created transport for authentication: Peer indicated failure: Unsupported mechanism type PLAIN.
at com.cloudera.hiveserver2.hivecommon.api.HiveServer2ClientFactory.createTransport(Unknown Source)
at com.cloudera.hiveserver2.hivecommon.api.ZooKeeperEnabledExtendedHS2Factory.createClient(Unknown Source)
...
I thought, that it is missing AuthMech attribute and added AuthMech=1 to the URL. Now I am getting:
java.sql.SQLNonTransientConnectionException: [Cloudera][JDBC](10100) Connection Refused: [Cloudera][JDBC](11640) Required Connection Key(s): KrbHostFQDN, KrbServiceName; [Cloudera][JDBC](11480) Optional Connection Key(s): AsyncExecPollInterval, AutomaticColumnRename, CatalogSchemaSwitch, DecimalColumnScale, DefaultStringColumnLength, DelegationToken, DelegationUID, krbAuthType, KrbRealm, PreparedMetaLimitZero, RowsFetchedPerBlock, SocketTimeOut, ssl, StripCatalogName, transportMode, UseCustomTypeCoercionMap, UseNativeQuery, zk
at com.cloudera.hiveserver2.exceptions.ExceptionConverter.toSQLException(Unknown Source)
at com.cloudera.hiveserver2.jdbc.common.BaseConnectionFactory.checkResponseMap(Unknown Source)
...
But KrbHostFQDN is already specified in the principal property as required in the documentation.
Am I missing something or is this documentation wrong?
Below is the one of the similar kind of problem statement in Impala (just JDBC engine changes others are same) that is resolved by setting "KrbHostFQDN" related properties in JDBC connection string itself.
Try to use the URL below. Hopefully works for u.
String jdbcConnStr = "jdbc:impala://myserver.mycompany.corp:21050/default;SSL=1;AuthMech=1;KrbHostFQDN=myserver.mycompany.corp;KrbRealm=MYCOMPANY.CORP;KrbServiceName=impala"
I suppose that if you are not using SSL=1 but only Kerberos, you just drop that part from the connection string and don't worry about setting up SSL certificates in the java key store, which is yet another hassle.
However in order to get Kerberos to work properly we did the following:
Install MIT Kerberos 4.0.1, which is a kerberos ticket manager. (This is for Windows)
This ticket manager asks you for authentication every time you initiate a connection, creates a ticket and stores it in a kerberos_ticket.dat binary file, whose location can be configured somehow but I do not recall exactly how.
Finally, before launching your JAVA app you have to set an environment variable KRB5CCNAME=C:/path/to/kerberos_ticket.dat. In your java app, you can check that the variable was correctly set by doing System.out.println( "KRB5CCNAME = " + System.getenv( "KRB5CCNAME" ) ). If you are working with eclipse or other IDE you might even have to close the IDE,set up the environment variable and start the IDE again.
NOTE: this last bit is very important, I have observed that if this variable is not properly set up, the connection wont be established...
In Linux, instead MIT Kerberos 4.0.1, there is a program called kinit which does the same thing, although without a graphical interface, which is even more convenient for automation.
I wanted to put it in the comment but it was too long for the comment, therefore I am placing it here:
I tried your suggestion and got another exception:
java.sql.SQLException: [Cloudera]HiveJDBCDriver Error
creating login context using ticket cache: Unable to obtain Principal
Name for authentication .
May be my problem is, that I do not have environment variable KRB5CCNAME set.
I, honestly, never heard about it before.
What is supposed to be in that ticket file.
I do have, however, following line in my main method:
System.setProperty("java.security.krb5.conf", "path/to/krb5.conf");
Which is supposed to be used by
UserGroupInformation.loginUserFromKeytab(config.getProperty("login.user"), config.getProperty("keytab.file"));
to obtain the kerberos ticket.
To solve this issue update Java Cryptography Extension for the Java version that you use in your system.
Here's the link when you can download JCE for Java 1.7
Uncompress and overwrite those files in $JDK_HOME/jre/lib/security
Restart your computer.
we are using java bridge from PHP application to connect to java application.
But at times, connection get failed to connect. Below is the fucntion used to connect. I am not getting what the issue is. Sometimes it works and sometimes it fails to connect. Any persistent connection issues or count is set?
function open() {
$errno = null;
$errstr = null;
$socket = JAVA_PERSISTENT_SERVLET_CONNECTIONS ?
pfsockopen("{$this->ssl}{$this->host}", $this->port, $errno, $errstr, 20) :
fsockopen("{$this->ssl}{$this->host}", $this->port, $errno, $errstr, 20);
if (!$socket)
throw new java_ConnectException("Could not connect to the J2EE server {$this->ssl}{$this->host}:{$this->port}. Please start it. Or define('JAVA_HOSTS', 8080); define('JAVA_SERVLET', false); before including 'Java.inc' and try again. Error message: $errstr ($errno)\n");
stream_set_timeout($socket, -1);
return $socket;
}
function java_HttpHandler($protocol, $ssl, $host, $port) {
parent::java_SimpleHttpHandler($protocol, $ssl, $host, $port);
try {
$this->socket = $this->open();
} catch (Exception $e) {
$cogLink = "http://xxxx.xx.com/products/sup_products.asp?prod_id=81174";
echo "eeeeerrrr";
}
}
Any persistent connection issues or count is set?
Most likely the back-end ran out of handles. Please note that, in order to obtain a persistent connection to a java "continuation", you'll have to send a servlet engine a POST request first. After that you can pfsockopen() to the java "continuation" the servlet engine has returned and use protocol request
(F p="A"/)
to recycle- and
(F p="F"/)
to put the "continuation" back to the pool.
I have checked the Java.inc code and it doesn't contain the code you have posted. Therefore I assume that you have written custom code to emulate Java.inc.
If so, please make sure to no exceed the bridge thread pool size (20 by default).
I want to check the existence of a url and i tried with the following code in java,
public boolean exists(String URLName) {
try {
if (!URLName.toUpperCase().contains("HTTP"))
URLName="http://"+URLName;
URL url = new URL(URLName);
System.setProperty("java.net.useSystemProxies", "true");
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
urlConn.setConnectTimeout(9000);
urlConn.setReadTimeout(9000);
urlConn.connect();
if(HttpURLConnection.HTTP_OK == urlConn.getResponseCode())
return true;
else
return false;
}
catch (SocketTimeoutException e){
return false;
}
catch (Exception e) {
e.printStackTrace();
return false;
}
}
The above code is working fine in windows but when the code is run through server (linux) i am getting the following error message,
GConf Error: Failed to contact configuration server; some possible
causes are that you need to enable TCP/IP networking for ORBit, or
you have stale NFS locks due to a system crash. See
http://projects.gnome.org/gconf/ for information. (Details - 1:
Not running within active session)
GConf Error: Failed to contact configuration server; some possible
causes are that you need to enable TCP/IP networking for ORBit, or
you have stale NFS locks due to a system crash. See
http://projects.gnome.org/gconf/ for information. (Details - 1:
Not running within active session)
java.net.UnknownHostException: www.ep.gov
at
java.net.PlainSocketImpl.connect(PlainSocketImpl.j ava:177)
at
java.net.SocksSocketImpl.connect(SocksSocketImpl.j ava:366)
at java.net.Socket.connect(Socket.java:525)
at sun.net.NetworkClient.doConnect(NetworkClient.java :158)
at
sun.net.http://www.http.HttpClient.openServe...lient.java:394)
at
sun.net.http://www.http.HttpClient.openServe...lient.java:529)
at sun.net.www.http.HttpClient.<init>(HttpClient.java :233)
at sun.net.www.http.HttpClient.New(HttpClient.java:306)
at sun.net.www.http.HttpClient.New(HttpClient.java:323)
at
sun.net.http://www.protocol.http.HttpURLConn...ction.java:860)
at
sun.net.http://www.protocol.http.HttpURLConn...ction.java:801)
at
sun.net.http://www.protocol.http.HttpURLConn...ction.java:726)
I got the below message from server admin
It is working, while it was run in X session (GUI Mode).
But it is not working in non-GUI mode. Please try to change the coding to make use of non-GUI mode." It seems the above code is making use of GUI mode.
I need an alternate code to check url existence without using GUI.
Regards
Linda
UnknownHostException means that the request is not reaching to its destination. There could be many reason that the linux server is unreachable to the desired URL www.ep.gov.
Please make sure that the server can reach the URL www.ep.gov.
I think your problem is with this line:
System.setProperty("java.net.useSystemProxies", "true");
It seems this causes the system to try and find the HTTP proxy configuration for the web browser, and ends up running into some problems with the Gnome configuration.
Do you actually need to use a HTTP proxy? If not, simply remove that line. If you do, read this page to learn how to configure proxies.
Here's how I'm trying to connect:
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (Exception e) {
throw new DbConnectionException();
}
try {
connection = DriverManager.getConnection(url,username,password);
} catch (SQLException e) {
e.printStackTrace();
throw new DbConnectionException();
}
I'm 100% sure that the url, username, password strings are correct. I've already connected successfully using an external tool (MySQL query browser).
This is the error I receive:
com.mysql.jdbc.CommunicationsException:
Communications link failure due to
underlying exception:
** BEGIN NESTED EXCEPTION **
java.net.SocketException MESSAGE:
java.net.ConnectException: Connection
refused
...
Possibly a url issue. If your code is pointing to MySQL localhost, try changing localhost to 127.0.0.1 on your url.
E.g.:
jdbc:mysql://localhost:3306/MY_DB
to
jdbc:mysql://127.0.0.1:3306/MY_DB
And see if this works.
did you run the mysql browser from the same machine where the code is running? What I am getting at is the permissions in mysql can be host-specific, and depending on how you set them up you might not be able to connect from the machine where the code is running.
Also, you might want to double check the url, name, pword again, perhaps with log statements or a debugger to make sure there are no typos, trailing whitespaces, etc...
Double check the format of your url. It should start with "jdbc:mysql:". Make sure you are using a current version for the driver as well.
Check that you can connect to the database from the mysql admin tool, that will drive out whether your mysql is running and that the port is open.
In my case the problem was that I was using a connection from emulator to localhost.
If you use emulator to localhost don't use localhost value in connection String but use 10.0.2.2 instead:
jdbc:mysql://10.0.2.2:3306/MY_DB
Hope this helps.
I set up a static IP and did port forwarding on my notebook, and now I have a static IP address, but it's relatively static, every time I re-start the machine, I get another address, and since I have a "static" IP I can now do Paypal IPN messaging. But how can I get this static IP from my Java program ? One way I can think of is to visit : http://portforward.com/ and on that page it tells me what my external IP is, so I can extract it with Java code, is there any other way that I can do in my Java code to get this info ?
The best solution is probably dynamic DNS. Essentially, you run a program on your computer (or router) that notifies a DNS server when your IP changes. Then, you can just tell PayPal the domain name.
There is a public service you can call with your script to retrieve your external IP address. Bear in mind they have changed the link location once. If you control your own server, you should probably write your own PHP script to simply return the address of the caller to the script.
http://www.whatismyip.com/faq/automation.asp - follow the developers link they provide
import java.net.*;
import java.io.*;
URL myExternalIP = new URL("PUT THE LINK HERE");
BufferedReader in = new BufferedReader(new InputStreamReader(
myExternalIP.openStream()));
String ip = in.readLine(); //you get the IP as a String
System.out.println(ip);
Your own server script is a simple one-liner. Create a file called whatismyip.php and here's the content.
<? echo $_SERVER['REMOTE_ADDR']?>
I see three ways of doing this:
As you discovered, querying an external server what IP you're apparently connecting from. This is simple but you require such a service to be available and, usually, that no transparent proxy messes with your results.
IGD, a sub-protocol of UPnP can give you the result very easily if your port forwarding devices supports it (many do). Google "IGD JAVA" for libraries and samples.
Register for a dynamic DNS service and then lookup your own DNS name.
You can use the NetworkInterface class:
Enumeration<NetworkInterface> ifcs = NetworkInterface.getNetworkInterfaces();
while(ifcs.hasMoreElements()){
NetworkInterface ifc = ifcs.nextElement();
System.out.println("Adresses of: "+ifc.getDisplayName());
Enumeration<InetAddress> adresses = ifc.getInetAddresses();
while(adresses.hasMoreElements()){
System.out.println(adresses.nextElement().getHostAddress());
}
}
This snippet will show you all of the interfaces and the IPs bound to them. You will need to look through the list to find the appropriate interface (see also NetworkInterface.getByName(String name)) And then look at the addresses for that interface. Once you have found the appropriate InetAdress you can use that to get the string or byte representation.