JNI4NET - how to run Java application from C# Class Library project?

1.8k Views Asked by At

I have Java swing application, and I want to run it from C#.

It works correctly when I use it from WindowsFormsApplication (see working version below). I set WindowsFormsApplication window invisible, and the application exits after I call System.exit(0); in Java. But when I try to run the same using ClassLibrary project, I cannot call Application.Run();, so the program exits immediately. (In debug mode using a breakpoint I can see that Java program with GUI initializes correctly and begins to run). How to make it wait until Java program exits?

Working example using WindowsFormsApplication:

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

using java.io;
using java.lang;
using java.util;
using net.sf.jni4net;
using net.sf.jni4net.adaptors;

using tt_factory;

namespace BookMap
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string [] args)
        {
            Init();
            TT_Factory.create_replay(); // creates Java GUI
            Application.Run();
        }

        private static void Init()
        {
            BridgeSetup bridgeSetup = new BridgeSetup(true);
            bridgeSetup.AddJVMOption("-Xms900m");
            bridgeSetup.AddAllJarsClassPath(Application.StartupPath + "\\..\\lib");
            bridgeSetup.JavaHome = Application.StartupPath + "\\..\\jre";
            Bridge.CreateJVM(bridgeSetup);
            Bridge.RegisterAssembly(typeof(TT_Factory).Assembly);
        }
    }
}

Example using ClassLibrary project and ConsoleApplication as test

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            BookMap.create_replay();
            /***** This method is implemented by ClassLibrary project:
             public static void create_replay() 
             {
                init_jvm();
                TT_Factory.create_replay();
             }
             ***** How to make program to wait here ? *****/
        }
    }
}

Update:

I tried starting new thread, but result is the same: the program exits immediately.

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            Thread thread = new Thread(new ThreadStart(BookMap.create_replay));
            thread.Start();
            thread.Join();
        }
    }
}
1

There are 1 best solutions below

0
On

Here is a simple solution that works. I didn't understand yet how, but the program waits until Java application calls System.exit(0), and then exits regardless Console.Read();

From MSDN: The Read method blocks its return while you type input characters; it terminates when you press the Enter key.

In this case no one presses enter in the console.

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            BookMap.create_replay();
            Console.Read();
        }
    }
}