Bonita Java API

I am trying to deploy a process in a BAR file using Bonita’s Java API. I was able to create a Business Archive object from the BAR file and log in to the server but when I try to deploy the process, I get the following error:

Exception in thread "main" java.lang.ExceptionInInitializerError
	at org.bonitasoft.engine.api.HTTPServerAPI.invokeMethod(HTTPServerAPI.java:144)
	at org.bonitasoft.engine.api.impl.ClientInterceptor.invoke(ClientInterceptor.java:74)
	at jdk.proxy2/jdk.proxy2.$Proxy4.deploy(Unknown Source)
	at org.example.Main.main(Main.java:44)
Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make field private java.lang.Throwable java.lang.Throwable.cause accessible: module java.base does not "opens java.lang" to unnamed module @69997e9d
	at java.base/java.lang.reflect.AccessibleObject.throwInaccessibleObjectException(AccessibleObject.java:391)
	at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:367)
	at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:315)
	at java.base/java.lang.reflect.Field.checkCanSetAccessible(Field.java:183)
	at java.base/java.lang.reflect.Field.setAccessible(Field.java:177)
	at org.bonitasoft.engine.exception.StackTraceTransformer.<clinit>(StackTraceTransformer.java:37)
	... 4 more

This is the code:

package org.example;

import org.bonitasoft.engine.api.*;
import org.bonitasoft.engine.bpm.bar.BusinessArchive;
import org.bonitasoft.engine.bpm.bar.BusinessArchiveFactory;
import org.bonitasoft.engine.bpm.bar.InvalidBusinessArchiveFormatException;
import org.bonitasoft.engine.bpm.process.ProcessDefinition;
import org.bonitasoft.engine.session.APISession;
import org.bonitasoft.engine.util.APITypeManager;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

public class Main {

    public static void main(String[] args) {
        // Let's set the connection settings to use HTTP on the already running Bonita runtime:
        Map<String, String> settings = new HashMap<String, String>();
        settings.put("server.url", "http://localhost:8080");
        settings.put("application.name", "bonita");
        // HTTP Basic Auth is active by default on server-side:
        settings.put("basicAuthentication.active", "true");
        settings.put("basicAuthentication.username", "http-api");  // default value, can be changed server-side in file <BONITA>/server/conf/tomcat-users.xml
        settings.put("basicAuthentication.password", "h11p-@p1");  // default value, can be changed server-side in file <BONITA>/server/conf/tomcat-users.xml
        APITypeManager.setAPITypeAndParams(ApiAccessType.HTTP, settings);

        String barFilePath = "my_bar_file_path"; // I removed the exact path before posting

        org.bonitasoft.engine.api.APIClient apiClient = new APIClient();
        try {
            apiClient.login("install", "install");

            final LoginAPI loginAPI = TenantAPIAccessor.getLoginAPI();
            APISession session = loginAPI.login("install", "install");
            ProcessAPI processAPI = TenantAPIAccessor.getProcessAPI(session);

            BusinessArchive businessArchive = createBusinessArchive(barFilePath);
            System.out.println("BusinessArchive created successfully!");

            ProcessDefinition processDefinition = processAPI.deploy(businessArchive);
            System.out.println("Process deployed: " + processDefinition.getName());

            loginAPI.logout(session);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static BusinessArchive createBusinessArchive(String barFilePath) throws IOException, InvalidBusinessArchiveFormatException {
        File barFile = new File(barFilePath);

        try (InputStream inputStream = new FileInputStream(barFile)) {
            // Read BusinessArchive from InputStream

            return BusinessArchiveFactory.readBusinessArchive(inputStream);
        }
    }
}

I am using maven to build the dependencies.

Hi,

The error may be related to the content of the BAR file you are trying to deploy.
Have you validated that the manual deployment of the BAR file is successful?

Notre regarding your code you have some sort of code duplication:
If you use APIClient then there is no use of using Tenant APIAccessor.

apiClient.login(“install”, “install”);
apiClient..getProcessAPI().deploy(businessArchive);

Should be enough.

You can look at the source code for examples on Github.

Hope this helps,
Captain Bonita