1

I am trying to build (in Eclipse) and use the code in this older thread:

How do I pass the client certificate with HTTP client?

But I am having problems compiling it because this line is causing the compiler to flag an error:

SSLContext sslContext = SSLContexts.custom().loadKeyMaterial(
    MutualHttpsMain.class.getResource(TEST_CLIENT_KEYSTORE_RESOURCE),
    storePassword, 
    keyPassword,
    (aliases, socket) -> aliases.keySet().iterator().next()
).build();

The error is:

error: lambda expressions are not supported in -source 1.7

My understanding is that lambda expressions were not supported in Java/JDK 1.7, so it seems like Eclipse (or Maven) thinks that the compiler is 1.8, but I've gone through my Eclipse configuration and everything appears to be configured for using JDK 1.8, so I don't know why I am still getting the error, so I am wondering: Is it possible to convert that line so it is no longer a lambda expression?

Can someone here help with that?

EDIT: Sorry, I had to correct the text of the error above... the correct error message says 1.7 and not 7.

12
  • 2
    It is possible, but I'd advise you to look for the "-source 7" issue. I'd guess it's in a maven file ("<source>7</source>"). Commented Aug 31, 2023 at 10:35
  • 1
    just to clarify, do you really want to compile with java 1.7 (basically backport your code to java 1.7) and looking for non-lambda alternative of the code snippet or the issue is that eclipse doesn't compile with 1.8 for some reason? If you want to remove lambda (and sure that there are no other java 8 code parts in your codebase) then please check the definition of loadKeyMaterial method. Its last parameter must be an interface, please post it in the question as well so that we could provide a working non-lambda alternative Commented Aug 31, 2023 at 10:46
  • Look at the Javadoc for loadKeyMaterial, see what type it expects for that parameter, and create an instance of that type, presumably by creating an anonymous instance. But as others have said, check your pom.xml Commented Aug 31, 2023 at 10:54
  • 1
    @daniu - I made the change in the file and now it compiled! Like I said I remember finding and trying a build last night, but I think I may've mistakenly built the wrong project last night :( !! Commented Aug 31, 2023 at 12:00
  • 1
    I see, so try to make two things: 1. Post maven pom.xml - what happens when you compile outside eclipse (mvn clean package) - does it compile? Especially check the properties and the definitions of maven compiler plugin in your pom.xml (you might also have defaults, which is java 1.5 at all) 2. Post the interface like I've asked in the previous comment - this is required to help you to create a non-lambda alternative for this specific case. Commented Aug 31, 2023 at 12:01

1 Answer 1

1

You can't compile Java 8 features (lambda expression, method references) with the Java 7 compiler.

Assuming you use the method from the latest version of Apache HttpCore 4.4.16, just implement an instance of the anonymous class PrivateKeyStrategy:

SSLContext sslContext = SSLContexts.custom().loadKeyMaterial(
    MutualHttpsMain.class.getResource(TEST_CLIENT_KEYSTORE_RESOURCE),
    storePassword,
    keyPassword,
    new PrivateKeyStrategy() {
        @Override
        public String chooseAlias(Map<String, PrivateKeyDetails> aliases, Socket socket) {
            return aliases.keySet().iterator().next();
        }
    }
).build();

A deprectaion note: If you use the deprecated Apache HttpClient org.apache.http.conn.ssl.SSLContextBuilder, consider moving to Apache HttpCore org.apache.http.ssl.SSLContextBuilder.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.