javax.net.ssl.SSLException: Received fatal alert: protocol_version when using java-algorand-sdk with PureStake API

[This is in relation to doing https://github.com/algorandfoundation/buildweb3 in Java]

This is odd:

Looking at the PureStake sample code, and with a PureStake account set up, and after trying some things in their Swagger Playground, I tried simply plugging in appropriate PureStake values on a copy of the GettingStarted class from the Java “Your First Transaction” demo:

    private AlgodClient connectToNetwork() {
        final String ALGOD_API_ADDR = "https://testnet-algorand.api.purestake.io/ps2";
        final String ALGOD_API_TOKEN = <REDACTED>;
        final String ALGOD_API_TOKEN_KEY = "X-API-Key";
        final Integer ALGOD_PORT = 443;
. . .

It sat there for quite a few seconds, and then I got this:

Exception in thread "main" javax.net.ssl.SSLException: Received fatal alert: protocol_version
        at sun.security.ssl.Alerts.getSSLException(Alerts.java:208)
        at sun.security.ssl.Alerts.getSSLException(Alerts.java:154)
        at sun.security.ssl.SSLSocketImpl.recvAlert(SSLSocketImpl.java:1977)
        at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1093)
        at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1328)
        at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1355)
        at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1339)
        at com.squareup.okhttp.internal.io.RealConnection.connectTls(RealConnection.java:192)
        at com.squareup.okhttp.internal.io.RealConnection.connectSocket(RealConnection.java:149)
        at com.squareup.okhttp.internal.io.RealConnection.connect(RealConnection.java:112)
        at com.squareup.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:184)
        at com.squareup.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:126)
        at com.squareup.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:95)
        at com.squareup.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:281)
        at com.squareup.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:224)
        at com.squareup.okhttp.Call.getResponse(Call.java:286)
        at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:243)
        at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:205)
        at com.squareup.okhttp.Call.execute(Call.java:80)
        at com.algorand.algosdk.v2.client.common.Client.executeCall(Client.java:113)
        at com.algorand.algosdk.v2.client.common.Query.baseExecute(Query.java:27)
        at com.algorand.algosdk.v2.client.common.Query.baseExecute(Query.java:21)
        at com.algorand.algosdk.v2.client.algod.AccountInformation.execute(AccountInformation.java:36)
        at com.algorand.javatest.firsttransaction.GettingStartedPS.printBalance(GettingStartedPS.java:126)
        at com.algorand.javatest.firsttransaction.GettingStartedPS.firstTransaction(GettingStartedPS.java:140)
        at com.algorand.javatest.firsttransaction.GettingStartedPS.main(GettingStartedPS.java:211)

Clearly, something is not on speaking terms . . . .

Your Java version looks too old and does not support the TLS version of PureStake.
PureStake only supports TLS 1.2 and 1.3 (which is now industry standard as previous versions are essentially insecure).
Which version of Java are you using?

You definitely need at the very least Java 1.8.

If you’re using Maven, set the Java version using https://www.baeldung.com/maven-java-version

From a command line, I get:

Jamess-iMac:~ jameslampert$ java -version
java version "1.8.0_121"
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)

Since I’m still following the “Your First Transaction” demo, I’m still doing it in M$ :face_vomiting: Visual Studio Code. Tomorrow, I’ll have time to see what compiler VS Code is using.

In VS Code, here is what I get if I try to launch the main method in the debugger:

Jamess-iMac:demo jameslampert$ /usr/bin/env /Library/Java/JavaVirtualMachines/jdk1.7.0_09.jdk/Contents/Home/bin/java -agentlib:jdwp=transport=dt_socket,server=n,suspend=y,address=localhost:51366 -cp /var/folders/cn/vsn822092lqdlp8g6psy47380000gn/T/cp_d5h4ttloymsdzpdxi8v929i8q.jar com.algorand.javatest.firsttransaction.GettingStartedPS

(notice the explicit call to Java 7, which I’ve bolded). But if I do a java -version, I get the same as I get doing a java -version from any other command line:

Jamess-iMac:demo jameslampert$ java -version
java version “1.8.0_121
Java™ SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot™ 64-Bit Server VM (build 25.121-b13, mixed mode)

This is the issue.
Java 1.7 will not work with PureStake.

If you’re using maven see comment above.
Otherwise, see Java project management in Visual Studio Code

Yes. I gathered that the problem was that VS Code was using Java 7, even though Java 8 is not only installed, but is the default Java. Thanks for the link: I’ve got it open in the next browser tab, and hopefully it will solve the problem. VS Code is as new to me as Algorand is.

That solved the problem. And I’d completely forgotten that the project does use Maven (new to that, too: whaddya expect, I’m used to doing everything manually, from a 5250 terminal session on an IBM Midrange box), and so following the instructions given in the “Java project management . . .” link took me right to the POM file. It sync’d correctly on the second try, and started using Java 8.

The first time I tried running my doctored-up version of “GettingStarted” after that, it still blew up on

        Response < com.algorand.algosdk.v2.client.model.Account > respAcct = client.AccountInformation(myAccount.getAddress()).execute();

only this time with some kind of EOF error (I lost the stacktrace, unfortunately), but the second and third times, it successfully transferred a “Monopoly Algo” between two of my test accounts, using the PureStake node.

Thanks once again for your patience.