Table of Contents

JVM enabled languages

Besides Java, many other languages run on the JVM, for example Scala and Kotlin. A longer list is on Wikipedia.

With JCOBridge you can use all the libraries available, with no limitation: set up the environment and use them as you would from Java. The following example uses Scala from both the JVM and the CLR.

JVM/Scala REPL

Scala REPL shell using .NET

The Scala REPL shell using .NET.

JVM/Scala from CLR/C#

A simple Scala class:

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(", ")
    }
}

The C# code, with the initialization needed to use Scala, which invokes the methods of the Scala class:

using MASES.JCOBridge.C2JBridge;
using System;

namespace ScalaClassUseExample
{
    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();
            }
        }
    }

    class TestClass : SetupJVMWrapper<TestClass>
    {
        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();
        }
    }
}

CLR/.NET from JVM/Scala

A Scala program that uses native .NET objects:

import java.util.Iterator
import org.mases.jcobridge._

object Main extends App {
  try
  {
    JCOBridge.Initialize("");
  }
  catch
  {
    // catch to avoid problems with the trial mode of JCOBridge
    case jce: JCException => System.out.println(jce.getMessage)
  }

  val bridge = JCOBridge.CreateNew()

  // adds a new reference to WPF
  bridge.AddReference("PresentationFramework")
  // get the MessageBox type
  val msgType = bridge.GetType("System.Windows.MessageBox")
  // invoke a static method to show a message box on the screen
  msgType.Invoke("Show", "Please press enter to continue")

  // get a .NET type
  val enumType = bridge.GetType("System.Environment")
  // invoke a static method
  val genObj = enumType.Invoke("GetLogicalDrives")
  // retrieve the iterator
  val iteratorObj = genObj.asInstanceOf[JCObject].iterator
  // iterate on all objects and print the value
  while (iteratorObj.hasNext) println(iteratorObj.next)

  // invoke a static method to show a message box on the screen
  msgType.Invoke("Show", "Please press enter")

  // event callback example
  val tObj = bridge.NewObject("System.Timers.Timer"); // create the timer object
  val timerObj = tObj.asInstanceOf[JCObject];
  // register an event handler for when the timer elapses
  timerObj.RegisterEventListener("Elapsed", new ScalaJCVoidEventEmit());
  // set the Interval property
  timerObj.Set("Interval", 1000);
  // enable the timer
  timerObj.Set("Enabled", true);

  // invoke a static method to show a message box on the screen
  msgType.Invoke("Show", "Please press enter")
}

final class ScalaJCVoidEventEmit() extends JCVoidEventEmit {
  override def EventRaised(args: Object*) : Unit =
  {
    // Scala seems to have a problem translating a varargs argument into JVM bytecode.
    // This method is needed to avoid compilation problems
  }
  // this method defines exactly the signature expected from the event
  def EventRaised(sender: Object, arg: Object) : Unit =
  {
    println("Timer Elapsed")
  }
}