ballerinaballerina-java-interop

Ballerina Java Cast Exception


I'm running the code form the java support learning page and get an exception when I try to build it. Here are the steps I did:

  1. Generate bindings:
ballerina bindgen -o src/test java.io.FileInputStream java.io.InputStream --public
  1. Copy sample code
import ballerina/java;

type FileInputStreamTypedesc typedesc<FileInputStream>;
function test(){
    InputStream inputStream = check newFileInputStream3("sample.txt");
    FileInputStream fileInputStream = <FileInputStream>check java:cast(inputStream, FileInputStreamTypedesc);
}
  1. build
ballerina build test

Compiling source
        acme/test:0.0.1
error: acme/test:0.0.1::main.bal:7:85: incompatible types: expected 'typedesc<ballerina/java:JObject>', found 'typedesc<typedesc<acme/test:0.0.1:FileInputStream>>'

used versions:

Thanks for any help in advance!


Solution

  • The learn page you mentioned is for the Ballerina Swan Lake release. But it seems, anyway, for the 1.2.x version learn page also the same content is there and it is incorrect.

    For the moment, you can update Ballerina to the Swan Lake version (which is anyway the latest); the way to do this is mentioned here. Afterward, you would be able to execute the mentioned code successfully.

    Cheers, Anjana.