socket

  • Thread starter Patrick Wallingford
  • Start date
P

Patrick Wallingford

Hm, I can seem to compile this in Eclipse, but not in command line tools.
What am I missing?

SSLSocket.java:17: incompatible types
found : java.net.Socket
required: SSLSocket
SSLSocket socket =
SSLSocketFactory.getDefault().createSocket(TARGET_HTTPS_SERVER,
TARGET_HTTPS_PORT);

-- code --

import java.io.*;
import java.net.*;
import javax.net.*;
import javax.net.ssl.*;
import java.security.*;
import java.security.cert.Certificate;

public class SSLTest {

private SSLSession sslSession = null;

public static final String TARGET_HTTPS_SERVER = "localhost";
public static final int TARGET_HTTPS_PORT = 8443;

public static void main(String[] args) throws Exception {

SSLSocket socket = (SSLSocket) SSLSocketFactory.getDefault().
createSocket(TARGET_HTTPS_SERVER, TARGET_HTTPS_PORT);

SSLSession sslSession = socket.getSession();

Certificate[] localCerts = null;
Certificate[] peerCerts = sslSession.getPeerCertificates();

etc...
 
W

wesley.hall

Patrick said:
Hm, I can seem to compile this in Eclipse, but not in command line tools.
What am I missing?

You seem to be compiling two different versions of the source code...
look...
SSLSocket.java:17: incompatible types
found : java.net.Socket
required: SSLSocket
SSLSocket socket =
SSLSocketFactory.getDefault().createSocket(TARGET_HTTPS_SERVER,
TARGET_HTTPS_PORT);

Notice how the line here does NOT have the cast to SSLSocket?
-- code --
SSLSocket socket = (SSLSocket) SSLSocketFactory.getDefault().
createSocket(TARGET_HTTPS_SERVER, TARGET_HTTPS_PORT);

Notice how this one does!

The cast is required because createSocket returns a Socket not an
SSLSocket.
 
C

Chris Uppal

Patrick Wallingford wrote:

Error message
found : java.net.Socket
required: SSLSocket
SSLSocket socket =
SSLSocketFactory.getDefault().createSocket(TARGET_HTTPS_SERVER,
Code:
SSLSocket socket = (SSLSocket) SSLSocketFactory.getDefault().
createSocket(TARGET_HTTPS_SERVER, TARGET_HTTPS_PORT);

Note that the error message doesn't match the code. The code you posted has an
explicit cast of the returned value from createSocket(), whereas the code the
compiler is moaning about lacks that cast. So the compiler refuses the
assignment of a Socket reference to a SSLSocket variable.

-- chris
 
P

Patrick Wallingford

Whoa,

My bad. The actual error is, when I do make the cast, it works on Eclipse
but fails on command line compiling giving the following error:

SSLSocket.java:17: inconvertible types
found : java.net.Socket
required: SSLSocket
SSLSocket socket = (SSLSocket)
SSLSocketFactory.getDefault().createSocket(TARGET_HTTPS_SERVER,
TARGET_HTTPS_PORT);
 
C

Chris Uppal

Patrick said:
My bad. The actual error is, when I do make the cast, it works on Eclipse
but fails on command line compiling giving the following error:

SSLSocket.java:17: inconvertible types
found : java.net.Socket
required: SSLSocket
SSLSocket socket = (SSLSocket)
SSLSocketFactory.getDefault().createSocket(TARGET_HTTPS_SERVER,
TARGET_HTTPS_PORT);

I can't think of anything specific. It /should/ work (the IDE is correct). It
seem that the problem must be some sort of configuration/classpath issue with
the command-line tools, but I can't guess what from here.

Two things to try:

Execute
java -version
and
javac -J-showversion

to check that both commands, "java" and "javac", are actually resolving to the
Java installation that you expect. If you are using, say, Ant to build, then
you'll have to work harder to find out what's really going on :-(

Try (if you haven't already) being explicit about
import javax.net.ssl.SSLSocket;
in your Java source, just in case "SSLSocket" is resolving to something weird.
(Since you are using Eclipse, the chances are that you /are/ doing that
already).

Beyond that, I'm afraid, I have no other suggestions.

-- chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top