JCOBridge Examples

2025-12-10 0 462

JCOBridge Examples

JCOBridge main features

Field proven

Built on top of the field proven DLR plugin available in the Sinapse platform, JCOBridge guarantees the best performance in JVM and CLR worlds integration.

JVM ( Available only for .NET Framework on https://www.jco***bridge.com, .NET Core available on supported OS and Architectures)

  • Retrieve CLR Type
  • Instantiate CLR object
  • Invoke static methods
  • Invoke instance methods
  • Get/Set static properties
  • Get/Set instance properties
  • Set Delegates
  • Subscribe/Unsubscribe events
  • Integrates WPF controls into AWT/Swing window
  • Integrate WinForms controls into AWT/Swing window
  • Integrate complex .NET Graphical user interfaces objects into AWT/Swing window
  • User interface Controls, properties and events management

CLR ( Available for .NET Framework on Windows and .NET Core on Windows and Linux, other platforms available on demand )

  • Retrieve JVM class
  • Instantiate JVM objects
  • Invoke static methods
  • Invoke instance methods
  • Get/Set static fields
  • Get/Set instance fields
  • Use dynamic access to write code in a seamless way as it is done in Java language
  • Use specific interface to direct manages methods and fields

JCOBridge Examples

JCOBridge (JVM-CLR Object Bridge) allows the execution of JVM native languages, like java and scala, from CLR/.NET languages and vice-versa, it allows to import and use libraries, components and also to manage graphical user interface from one programming world to the other.
More information on www.jcobridge.com

The Short way

To explore the examples you need to perform the following steps:

  1. Download the last trial release from this link
  2. Install it following the wizard
  3. Clone this repository
  4. Use directly the executables ready into Output directories, or
  5. Enjoy the jcobridge with your preferred development tools

Repository Content

In this repository it is possible to find example code for the different programming language supported by JCObridge.
The examples are organized in two main folder, JVM and CLR that contains the relative projects. Before execute the code is needed that examples of both world are compiled, because no runtime compilation of foreign code is done, only execution.

.NET to Java Examples

.NET Core Linux/Windows Graphical User Interface example

The project Cross Platform GUI shows how to use AWT to create a cross-platform graphical user interface for .NET Core on Windows and Linux hosts. To use Swing simply change the controls within the code to your preferred ones.

Java Class Use Example

This is a basic example where we call the simple class defined in JavaClass.java from a .NET application.
in the /JVM/java/src/JavaClass.java we have a simple class

public class JavaClass {
    /**
     * This simple method return the \"Hello World!!\" string
     * * @return \"Hello World!!\" string
     */
    public String helloWorld()
    {
        return \"Hello World from Java!!\";
    }
    
    /**
     * This simple method return the sum of two double
     * @param a
     * @param b
     * @return a + b
     */
    public double add(double a, double b)
    {
        return a+b;
    }
    
    /**
     * This simple method return the sin of a double
     * @param a
     * @return sin of a
     */
    public double sin(double a)
    {
        return Math.sin(a);
    }
}

in the \\CLR\\JavaClassUseExample\\program.cs we have the simple .NET C# application

using MASES.LicenseManager.Common;
using MASES.JCBridge.C2JBridge;
using System;

namespace JavaClassUseExample
{
    class Program
    {
        static void Main(string[] args)
        {
            new TestClass().Execute();
        }

        class TestClass : SetupJVMWrapper
        {
            public override string ClassPath { get { return @\"..\\..\\JVM\\Output\"; } }

            public void Execute()
            {
                double a = 2;
                double b = 3;
                double c = Math.PI/2;
                var javaClass = DynJVM.JavaClass.@new();
                string hello = javaClass.helloWorld();
                double result = javaClass.add(a, b);
                double sin = javaClass.sin(c);
                Console.WriteLine(\"{0} {1} + {2} = {3} and sin({4:0.0000000}) = {5:0.00000000}\", hello, a, b, result, c, sin);
            }
        }
    }
}

Executing the code we have the following output:

Hello World from Java!! 2 + 3 = 5 and sin(3,1415927) = 1,00000000

JVM Environment Example

This example is an extension of the Java Class Use Example where Environment parameters are configured in the .NET TestClass class.

class TestClass : SetupJVMWrapper
{
    // the following line setup the classpath where JVM will search for classes
    // during runtime it is possible to dynamically add other path using a call like DynJVM.JVMHelper.addPath(<the path to add>);
    public override string ClassPath { get { return @\"C:\\Program Files\\MASES Group\\JCOB\\Core;..\\..\\JVM\\Java\\Output\"; } }

    // uncomment the following line and set the correct JRE if the automatic search system fails
    // public override string JVMPath { get { return @\"C:\\Program Files\\Java\\jre1.8.0_121\\bin\\server\\jvm.dll\"; } }

    // the following code adds all possible switch to the starting JVM.
    // for a complete list see Oracle documentation: https://docs.*o*r*acle.com/javase/8/docs/technotes/tools/windows/java.html
    public override IEnumerable<KeyValuePair<string, string>> JVMOptions
    {
        get
        {
            var dict = new Dictionary<string, string>();
            dict.Add(\"-Xmx128M\", null); // this line adds a complete argument
            // dict.Add(property, value); // this line adds an argument like -Dproperty = value

            return dict;
        }
    }

    // the following code adds initial packages to the import statement.
    public override IEnumerable<string> JVMPackages
    {
        get
        {
            var list = new List<string>();
            list.Add(\"java.lang\"); // this line adds java.lang.* like you do with \"import java.lang.*\" in Java
            return list;
        }
    }

    // uncomment and set the following line when you need features of JDK like the use of the compiler
    // public override string JDKHome { get { return @\"C:\\Program Files\\Java\\jdk1.8.0_121\\\"; } }

    public void Execute()
    {
        double a = 2;
        double b = 3;
        double c = Math.PI/2;
        var javaClass = DynJVM.JavaClass.@new();
        string hello = javaClass.helloWorld();
        double result = javaClass.add(a, b);
        double sin = javaClass.sin(c);
        Console.WriteLine(\"{0} {1} + {2} = {3} and sin({4:0.0000000}) = {5:0.00000000}\", hello, a, b, result, c, sin);
        Console.WriteLine(\"Press Enter to exit\");
        Console.ReadLine();
    }
}

ConsoleTest

This is a more complex application that explores the ability of JCOBridge doing the following operations:
-Execute java code in the .NET environment using the Dynamic JVM wrapper
-Manage shared object
-Register CLR object in the Java Virtual Machine side
-Use the registered object from the JVM side
-Call Methods on the JVM registered class and have the operation reflected to the CLR object
-Create dialog in the JVM side and use it from the .NET code

.NET Libraries for use within the Java to .NET Examples

CSharpClass

This library contain a single class that provide double and string operations to be called from JVM.

WinFormsTestControl

A Windows Form panel with his complete logic that will be used to demonstrate User Interface Integration in a Java Graphical application.

WPFTestControl

A WPF panel with his complete logic that will be used to demonstrate User Interface Integration in a Java Graphical application.

Java to .NET Examples

CSharp Class Use Examples

This is a basic example where we call the simple class defined in CSharpClass.cs from a Java application.
in the \\CLR\\CSharpClass\\CSharpClass.cs we have a simple class

using System;

namespace MASES.CLRTests
{
    public class CSharpClass
    {
        /// <summary>The method <c>HelloWorld</c> return the \"Hello World!!\" string</summary>
        public String HelloWorld()
        {
            return \"Hello World from C#!!\";
        }

        /// <summary>The method <c>Add</c> return the sum of two double</summary>
        public double Add(double a, double b)
        {
            return a + b;
        }

        /// <summary>The method <c>Add</c> return the sin of a double</summary>
        public double Sin(double a)
        {
            return Math.Sin(a);
        }
    }
}

in the /JVM/src/JavaClass.java we have the simple Java application

import java.io.IOException;
import org.mases.jcobridge.*;

public class CSharpClassUseExample {

    public static void main(String[] args) {
        try {
            try {
                try {
                    JCOBridge.Initialize(\"\");
                    } catch (JCException e) {
                        e.printStackTrace();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                //declare and create JCOBridge instance
                JCOBridge bridge;
                bridge = JCOBridge.CreateNew();
                // adds the path where extarnal assemblies where found
                bridge.AddPath(\"../CLR/Output/\");
                // add REFERENCES to the .dll file
                bridge.AddReference(\"CSharpClass\");
                // GENERATE Object
                JCObject CSharpObject = (JCObject) bridge.NewObject(\"MASES.CLRTests.CSharpClass\");
                double a = 2;
                double b = 3;
                double c = Math.PI/2;
                //Invoke the C# class methods
                String hello = (String) CSharpObject.Invoke(\"HelloWorld\");
                double result = (double) CSharpObject.Invoke(\"Add\",a, b);
                double sin = (double) CSharpObject.Invoke(\"Sin\", c);
                System.out.println(String.format(\"%s %.0f + %.0f = %.0f and sin(%.8f) = %.8f\", hello, a, b, result, c, sin));
            
            } catch (JCException jce) {
            jce.printStackTrace();
            System.out.println(\"Exiting\");
            return;
        }
    }
}

Executing the code we have the following output:

Hello World from C#!! 2 + 3 = 5 and sin(3,14159265) = 1,00000000

Windows Forms and WPF User Interface Integration

In this little more complex example we integrate into an awt java user interface two different complex control, taken from two .NET library.
The first control is a Windows Form, the second one is a WPF object.
The application in \\JVM\\Java\\srcAWTWinFormsWPF.java expose the complete process from the control reference and generation to the .NET event listener registration to the .NET events callback management.

import java.awt.Frame;
import java.io.IOException;
import org.mases.jcobridge.*;

public class AWTWinFormsWPF implements IJCVoidEventEmit {
    public static void main(String args[]) {
        new AWTWinFormsWPF().createAndShow();
    }

    int cycle = 0;
    java.awt.TextArea gTextArea;

    // WPF
    JCControl gControlWpfControl = null;

    // FORMS
    JCControl gControlFormsControl = null;

    void createAndShow() {
        try {
            // LOGGER
            IJCEventLog logger = null;
            try {
                try {
                    JCOBridge.Initialize(\"\");
                } catch (JCException e) {
                    e.printStackTrace();
                }

                logger = new JCFileEventLog(\"WinFormsWPF.txt\");
            } catch (IOException e) {
                e.printStackTrace();
            }

            JCOBridge bridge;

            bridge = JCOBridge.CreateNew();
            bridge.RegisterEventLog(logger);
            // adds the path where extarnal assemblies where found
            bridge.AddPath(\"../../CLR/Output/\");

            // add REFERENCES
            bridge.AddReference(\"WPFTestControl\");
            bridge.AddReference(\"WinFormsTestControl\");

            // GENERATE CONTROLS
            gControlWpfControl = bridge.GetControl(\"MASES.CLRTests.WPFTestControl.TestControl\");
            gControlFormsControl = bridge.GetControl(\"MASES.CLRTests.WinFormsTestControl.TestControl\");

            // CONFIGURE CONTROLS
            gControlWpfControl.RegisterEventListener(\"FromComboBox\", this);
            gControlWpfControl.RegisterEventListener(\"FromTextBox\", this);

            gControlFormsControl.RegisterEventListener(\"FromComboBox\", this);
            gControlFormsControl.RegisterEventListener(\"FromTextBox\", this);

            Frame dialog = new Frame();
            gTextArea = new java.awt.TextArea();
            gTextArea.setText(\"This is an AWT TextArea\");

            java.awt.GridLayout layout = new java.awt.GridLayout(2, 2);

            dialog.setLayout(layout);
            dialog.add(gControlWpfControl);
            dialog.add(gControlFormsControl);
            dialog.add(gTextArea);
            dialog.validate();
            dialog.setTitle(\"WinForms-WPF AWT integration\");
            dialog.setVisible(true);
            dialog.setSize(200, 200);

        } catch (JCException jce) {
            jce.printStackTrace();
            System.console().readLine(\"Please press enter\");

            System.out.println(\"Exiting\");
            return;
        }
    }

    @Override
    public void EventRaised(Object... args) {
        System.out.println(\"EventRaised\");
        if (args[1] instanceof JCObject) {
            JCObject obj = (JCObject) args[1];
            System.out.println();
            try {
                if (obj != null) {
                    gTextArea.setText(\"Text area: event: \" + obj.toString() + \" Content: \" + obj.Get(\"Content\"));
                }
            } catch (JCException e) {
                e.printStackTrace();
            }
        }
    }
}

Classes to be used from the CLR test applications

JavaClass

A single class that provide double and string operations to be called from the .NET CLR.

GlobalVariableTest

A class that contains two methods and display how to register and use Shared global variables and object

import org.mases.jcobridge.*;
import java.awt.*;

public class GlobalVariableTest
{
    public static void createGlobal() throws JCException
    {
        Dialog dialog = new Dialog((Dialog)null);
        JCOBridge.RegisterJVMGlobal(\"SharedDialog\", dialog);
    }
    
    public static void testMyCLRClass(Integer a, Integer b) throws JCException
    {
        JCObject resultGetCLRObject = (JCObject)JCOBridge.GetCLRGlobal(\"MyCLRClass\");
        resultGetCLRObject.Invoke(\"Add\", a, b);
    }
}

The createGlobal method create a global awt dialog and register it to be used seamlessly from the CLR side.
The testMyCLRClass show how to use a registered CLR global object, in the CLR example we create this object from the .NET side and we call this function to use it and demonstrate hot operations are reflected between JVM and CLR in a transparent manner.

.NET to Scala Example

A simple Scala class is defined to be used from CLR in JVM\\Scala\\scalaclass\\src\\main\\scala\\ScalaClass.class
Call compile and execute batch script in JVM\\Scala
Before call compile and execute batch script Scala binaries shall be installed.

import java.lang._

final class ScalaClass(aString: String, val anInteger: Int) {
    def this() {
        this(\"defaultString\", -1)
    }

    def this(aBool: Boolean) {
        this(\"defaultString\", -1)
    }

    val scalaString = \"This is a Scala String\"

    def add(x: Int, y: Int): Int = x + y

    def stringConcat(args: Array[String]): String = 
    {
        return args.mkString(\", \")
    }
}

In the \\CLR\\ScalaClassUseExample\\Program.cs we have a simple application that use the defined ScalaClass

using CommonTest;
using MASES.JCOBridge.C2JBridge;
using MASES.LicenseManager.Common;
using System;
using System.IO;

namespace ScalaClassUseExample
{
    class TestClass : BaseTestClass
    {
        public override string GetProjectClassPath()
        {
#if !JCOBRIDGE_CORE
            return @\"..\\..\\JVM\\Scala\\Output\";
#else
            return @\"..\\..\\..\\JVM\\Scala\\Output\";
#endif
        }

        public override string ClassPath
        {
            get
            {
                return new ClassPathBuilder(GetProjectClassPath() + @\"\\*\", @\"C:\\Program Files (x86)\\scala\\lib\\*.jar\").Build();
            }
        }

        public override void Execute()
        {
            int a = 10;
            int b = 15;
            var scalaClass = DynJVM.ScalaClass.@new();
            var result = scalaClass.add(a, b);
            Console.WriteLine(\"{0} + {1} = {2}\", a, b, result);

            string[] strings = new string[] { \"One\", \"Two\", \"Three\" };
            var concatString = scalaClass.stringConcat(strings);
            Console.WriteLine(\"{0} = {1}\", string.Concat(strings), concatString);

            Console.WriteLine(\"Press Enter to exit\");
            Console.ReadLine();
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                new TestClass().Execute();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(\"Press any key.\");
                Console.ReadKey();
            }
        }
    }
}

In Scala all the needed libraries shall be explicitly added to the base path.

Scala to .NET Example

In this example we call .NET object from Scala language via JCOBridge.
Before call compile and execute batch script Scala binaries shall be installed.

下载源码

通过命令行克隆项目:

git clone https://github.com/masesgroup/JCOBridge-Examples.git

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

申明:本文由第三方发布,内容仅代表作者观点,与本网站无关。对本文以及其中全部或者部分内容的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。本网发布或转载文章出于传递更多信息之目的,并不意味着赞同其观点或证实其描述,也不代表本网对其真实性负责。

左子网 编程相关 JCOBridge Examples https://www.zuozi.net/33278.html

Ai Learn
上一篇: Ai Learn
ftpclient
下一篇: ftpclient
常见问题
  • 1、自动:拍下后,点击(下载)链接即可下载;2、手动:拍下后,联系卖家发放即可或者联系官方找开发者发货。
查看详情
  • 1、源码默认交易周期:手动发货商品为1-3天,并且用户付款金额将会进入平台担保直到交易完成或者3-7天即可发放,如遇纠纷无限期延长收款金额直至纠纷解决或者退款!;
查看详情
  • 1、描述:源码描述(含标题)与实际源码不一致的(例:货不对板); 2、演示:有演示站时,与实际源码小于95%一致的(但描述中有”不保证完全一样、有变化的可能性”类似显著声明的除外); 3、发货:不发货可无理由退款; 4、安装:免费提供安装服务的源码但卖家不履行的; 5、收费:价格虚标,额外收取其他费用的(但描述中有显著声明或双方交易前有商定的除外); 6、其他:如质量方面的硬性常规问题BUG等。 注:经核实符合上述任一,均支持退款,但卖家予以积极解决问题则除外。
查看详情
  • 1、左子会对双方交易的过程及交易商品的快照进行永久存档,以确保交易的真实、有效、安全! 2、左子无法对如“永久包更新”、“永久技术支持”等类似交易之后的商家承诺做担保,请买家自行鉴别; 3、在源码同时有网站演示与图片演示,且站演与图演不一致时,默认按图演作为纠纷评判依据(特别声明或有商定除外); 4、在没有”无任何正当退款依据”的前提下,商品写有”一旦售出,概不支持退款”等类似的声明,视为无效声明; 5、在未拍下前,双方在QQ上所商定的交易内容,亦可成为纠纷评判依据(商定与描述冲突时,商定为准); 6、因聊天记录可作为纠纷评判依据,故双方联系时,只与对方在左子上所留的QQ、手机号沟通,以防对方不承认自我承诺。 7、虽然交易产生纠纷的几率很小,但一定要保留如聊天记录、手机短信等这样的重要信息,以防产生纠纷时便于左子介入快速处理。
查看详情

相关文章

猜你喜欢
发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务