0

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();
        }
    }
}
4

1 回答 1

0

这是一个有效的简单解决方案。我还不明白怎么做,但是程序一直等到 Java 应用程序调用 System.exit(0),然后不管 Console.Read();

来自MSDNRead 方法在您键入输入字符时阻止其返回;当您按下 Enter 键时,它会终止。

在这种情况下,没有人在控制台中按 Enter。

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            BookMap.create_replay();
            Console.Read();
        }
    }
}
于 2014-01-09T22:21:20.323 回答