B4J Question Trying to Compile a B4J UI App as a Library for a Normal Java App to Use

xulihang

Well-Known Member
Licensed User
Longtime User
I am trying to provide an SDK version of my B4J UI app. One of the requirement is to call the functions from a normal Java app

I reference the compiled jar in IDEA and call the the methods with the following Java code:

Java:
package org.example;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
public class Main {
    public static void main(String[] args) throws Exception {
        // Initialize JavaFX toolkit
        new JFXPanel();  // This starts the JavaFX runtime
        Platform.runLater(() -> {
            try {
                b4j.example.main._msgbox();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            //Platform.exit();
        });

        System.out.println("Hello world!");
    }
}

Version which requires launch main application:

Java:
package com.xulihang;

import javafx.application.Platform;
import org.xulihang.imagetrans.main;

import java.net.URL;
import java.net.URLClassLoader;

public class Main {
    public static void main(String[] args) throws InterruptedException {
        // 记录程序启动开始时间
        long programStartTime = System.currentTimeMillis();

        System.setProperty("glass.platform", "Headless");

        // 记录B4J启动开始时间
        long b4jStartTime = System.currentTimeMillis();

        // 在新线程中启动 B4J 应用
        new Thread(() -> {
            main.main(args);
        }).start();

        System.out.println("launched - main thread continues");

        // 等待B4J启动完成
        while (main._started == false) {
            Thread.sleep(1000);
        }

        // 计算B4J启动耗时
        long b4jEndTime = System.currentTimeMillis();
        long b4jDuration = b4jEndTime - b4jStartTime;
        System.out.println("========================================");
        System.out.println("B4J程序启动耗时: " + b4jDuration + " ms (" + (b4jDuration / 1000.0) + " 秒)");
        System.out.println("========================================");

        // 记录工作流执行开始时间
        long workflowStartTime = System.currentTimeMillis();
        System.out.println("开始执行工作流...");

        // 你的其他代码可以继续执行
        // 例如:
        try {
            Platform.runLater(() -> {
                try {
                    main._runworkflowviacli(new String[]{"C:\\test\\CLI_TEST","C:\\Users\\HP\\Documents\\ImageTrans\\imagetrans_projects\\CLI_TEST"});
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

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

    }
}

This works well. Since I uses B4X libraries, it may also work for iOS and Android in the future.

If there are more feasible ways, please let me know. The test project is attached.
 

Attachments

  • UILib.zip
    17.3 KB · Views: 11
Last edited:
Top