Chaitali
Chaitali

Reputation: 11

Ballerina java.lang.ClassNotFoundException while running module/project

I am trying Ballerina and Java Interoperability by importing a jar consisting of Java classes code into ballerina and trying to call constructor and methods in the java class through .bal file.

This works perfectly fine when I run .bal file.

ballerina run <balfile>

However when I run the whole project module it gives me below exception.

enter code here
error: java.lang.NoClassDefFoundError message=com/example/wrapperpackage/Wrapper cause=error java.lang.ClassNotFoundException message=com.example.wrapperpackage.Wrapper
        at cceackh.implementation:wrapperconstructor(java-test.bal:14)
           cceackh.implementation:main(java-test.bal:42)
PS C:\Users\cceackh\java_ballerina_interop\ballerina_interop>

I have saved the jar file in BallerinaHome/distributions/bre/lib folder.

Thanks.

Upvotes: 1

Views: 332

Answers (1)

Sameera Jayasoma
Sameera Jayasoma

Reputation: 1610

I assume that you have already read the guide How to Structure Ballerina Code. When you compile a Ballerina program with ballerina build <root-module>, the compiler creates an executable jar file. In this case, the Ballerina compiler produces a self-contained jar file.

If you are using Java interoperability in Ballerina, you need to specify all your jar files in Ballerina.toml file so that the compiler can package them within the executable jar file.

Here is how you can specify a jar file dependency in Ballerina.toml.

[platform] 
target = "java8" 

[[platform.libraries]] 
# Absolute or relative path to the jar file
path = "<path-to-jar-file-1>" 
# A comma-separated list of Ballerina module names that depends on this jar
modules = ["<ballerina-module-1>"]

[[platform.libraries]] 
path = "<path-to-jar-file-2>" 
modules = ["<ballerina-module-1>","<ballerina-module-2>"]
[platform] 
target = "java8" 

[[platform.libraries]] 
    path = "./javalibs/mysql-connector-java-<version>.jar" 
    modules = ["ordermgt"]

Upvotes: 1

Related Questions