Other Question about Java and Lambda

Star-Dust

Expert
Licensed User
Longtime User
I have this code inside a B4J library of mine placed inline with java.
It works correctly if I compile with Java 11 but if I compile with Java 8.202 it tells me that it can't compile in java 7 (strange since I compile with 8 and that if compiled outside b4j it doesn't throw an error).

My question since I know very little about java, how could I change this Lambda into a normal function.
Java:
Expand Collapse Copy
Future<KeyPair> keyPairmain = Executors.newSingleThreadExecutor().submit(()->
    {
      KeyPair keyPair = null;
      try
      {
        System.out.println("creating RSA keypair...");
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
        keyGen.initialize(2048);
        keyPair = keyGen.genKeyPair();
        System.out.println("creating RSA keypair...done");
      }
      catch (Exception e)
      {
        System.out.println("creating RSA keypair exception");
      }
      return keyPair;
    });
 
Solution
B4X:
Expand Collapse Copy
Future<KeyPair> keyPairmain = Executors.newSingleThreadExecutor().submit(new Callable() {
 public KeyPair call() throws Exception {
  //all code here
}
});
thanks a lot i will try it. ?

I use Java 11 but the compiled library doesn't work on Java 8 users. I would like to produce something that works on both
 
Upvote 0
in reality it is not called multiple times, but only when starting a device to transmit a code that authorizes its use. the device after receiving the code responds to the software.

This piece of code is provided by the device manufacturer in an example for Java. Corresponding manufacturer examples in other languages do not have this part. I tried to remove it but the device does not work
 
Upvote 0
B4X:
Expand Collapse Copy
Future<KeyPair> keyPairmain = Executors.newSingleThreadExecutor().submit(new Runnable() {
 public void run() {
  //all code here
}
});

Though you can simply use Java 11.
Unfortunately it doesn't work because it's not a void but a function that returns a value
 
Upvote 0
Perfect now it works, I added this.
B4X:
Expand Collapse Copy
import java.util.concurrent.Callable;

Many thanks
 
Upvote 0
Back
Top