bind / getHostByName works in terminal, not in debug or run

Answered

Mac OS X El Capitan, I have a simple java program to test SSL connections, certificate paths, etc works fine when run form either a shell, or the terminal in IntelliJ but does not work if run or debugged within IntelliJ (via the run or debug item in the context menu for the class.

I have tried just creating the socket against a host, any host, or using InetAddres.getHostByName(String) ... both fail if executing directly under IDEA. Same results with our without using SSLContext, 

Latest build, latest patches for IntelliJ and Mac has all updates applied.

The program is below, I don't have a problem running JUnit integration tests that connect to external servers / services. 

 

import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;

public class SSLPoke {
public static void main(String[] args) {
if (args.length != 2) {
System.out.println("Usage: "+SSLPoke.class.getName()+" <host> <port>");
System.exit(1);
}
try {
final InetAddress address = InetAddress.getByName("somehost.network.com");
/***** magic comment * /
final SSLContext sslContext = SSLContext.getInstance("TLS");

final TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
final KeyStore ts = KeyStore.getInstance("JKS");
ts.load(new FileInputStream("./truststore.jks"), "changeit".toCharArray());
tmf.init(ts);

final KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
final KeyStore ck = KeyStore.getInstance("JKS");
ck.load(new FileInputStream("./clientkeys.jks"), "changeit".toCharArray());
kmf.init(ck, "changeit".toCharArray());

sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);

final SSLSocketFactory sslSocketFactory = (SSLSocketFactory) sslContext.getSocketFactory();
final SSLSocket sslsocket = (SSLSocket) sslSocketFactory.createSocket(args[0], Integer.parseInt(args[1]));

/***** magic comment */
final SSLSocketFactory sslSocketFactory = (SSLSocketFactory)SSLSocketFactory.getDefault();
final SSLSocket sslsocket = (SSLSocket) sslSocketFactory.createSocket(args[0], Integer.parseInt(args[1]));
/***** magic comment terminator do not touch this. */


final InputStream in = sslsocket.getInputStream();
final OutputStream out = sslsocket.getOutputStream();

out.write(1);

while (in.available() > 0) {
System.out.print(in.read());
}
System.out.println("Successfully connected");

} catch (Exception exception) {
exception.printStackTrace();
}
}
}
3 comments
Comment actions Permalink

Do you run it on the same Java version? Try to copy the exact line you see in IDEA Run console after running the app and paste it into Terminal.

This could be also some firewall/networking issue specific to Mac.

0
Comment actions Permalink

Serge,

Good suggestions, I have grabbed the actually command issued by IntelliJ:

 

/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/bin/java -Didea.launcher.port=7532 "-Didea.launcher.bin.path=/Applications/IntelliJ IDEA.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath "/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/lib/packager.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/lib/tools.jar:/Users/draymer/Working/SSLPoke/out/production/SSLPoke:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar" com.intellij.rt.execution.application.AppMain SSLPoke hostname.network.com 443

 

And it is the same Java version that runs is used on a terminal execution,  Pasting into terminal produces the same result as running it from within IntelliJ itself.  

regards.

0
Comment actions Permalink

See if removing `com.intellij.rt.execution.application.AppMain` part helps. If it doesn't the issue is most likely not related to IntelliJ IDEA.

0

Please sign in to leave a comment.