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:
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:
Future<KeyPair> keyPairmain = Executors.newSingleThreadExecutor().submit(new Callable() {
 public KeyPair call() throws Exception {
  //all code here
}
});

Star-Dust

Expert
Licensed User
Longtime User
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

Star-Dust

Expert
Licensed User
Longtime User
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

Star-Dust

Expert
Licensed User
Longtime User
B4X:
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

Star-Dust

Expert
Licensed User
Longtime User
Perfect now it works, I added this.
B4X:
import java.util.concurrent.Callable;

Many thanks
 
Upvote 0
Top