我有Java swing應用程序,我想從C#運行它。JNI4NET - 如何從C#類庫項目運行Java應用程序?
當我從WindowsFormsApplication使用它時,它可以正常工作(請參閱下面的工作版本)。我將WindowsFormsApplication窗口設置爲不可見,並且在我調用Java中的System.exit(0);
之後應用程序退出。但是當我嘗試使用ClassLibrary項目運行相同的程序時,我無法調用Application.Run();
,因此程序立即退出。 (在使用斷點的調試模式中,我可以看到使用GUI的Java程序正確初始化並開始運行)。如何讓它等到Java程序退出?
使用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);
}
}
}
例使用ClassLibrary項目,ConsoleApplication作爲測試
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 ? *****/
}
}
}
更新:
我試圖開始新的線程,但結果是一樣的:程序立即退出。
namespace Test
{
class Program
{
static void Main(string[] args)
{
Thread thread = new Thread(new ThreadStart(BookMap.create_replay));
thread.Start();
thread.Join();
}
}
}
你能解釋一下爲什麼你想調用一個不能保證在用戶上下文中運行的交互式程序嗎? –
我有一個完整的GUI應用程序,用Java swing編寫。現在我想讓它可以從C#訪問,因爲應用程序所需的一些數據源只有C#API。但它已經在WindowsFormsApplication中運行良好。我只是不能使用ClassLibrary項目來運行它。 – Serg
爲了清楚起見,我們重新說明我的問題:您有一個應用程序,希望在基於用戶的上下文中運行(某人登錄到計算機上並將查看您期望顯示的漂亮UI) - 您想從一個不知道向客戶展示用戶界面的任何代碼 - 爲什麼你會認爲這是件好事?是的,這是可能的(相當簡單),但很少,如果有的話,是有道理的。您尚未描述您希望所述應用程序運行的環境,因此很難猜測它發生的原因 - 只知道這是一個壞主意。 –