在 C# 中使用 Java 类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/171717/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 16:34:40  来源:igfitidea点击:

Using Java-classes with C#

c#java

提问by Burkhard

I have a project written in Java (>1.5).

我有一个用 Java (>1.5) 编写的项目。

Is it possible to write parts of the project with C#?
For instance the GUI and calling the methods and instantiate the classes written in java?

是否可以使用 C# 编写部分项目?
例如GUI和调用方法并实例化用java编写的类?

If yes, how?

如果是,如何?

采纳答案by Midhat

There is something called Java Language Conversion Assistantfor .NET. You can convert your Java classes to c# and start coding.

有一种叫做.NET 的Java 语言转换助手。您可以将 Java 类转换为 c# 并开始编码。

There is also something called JNBridge(not free).

还有一种叫做JNBridge(不是免费的)的东西。

回答by Jon Skeet

Not without something like ikvm- or using web services etc to communicate between the two sides. Basically it's likely to be much more work than either rewriting your existing project code in C# or writing the GUI in Java.

不是没有像ikvm这样的东西- 或者使用网络服务等在双方之间进行通信。基本上,它可能比用 C# 重写现有项目代码或用 Java 编写 GUI 要多得多。

回答by Ilya

It seems like my solution is very limited. and apply only to specific version of java.

看来我的解决方案非常有限。并且仅适用于特定版本的 java。

I probably will stay with old good C :) Can't imagine how to work without shared libraries :)

我可能会继续使用旧的好 C :) 无法想象没有共享库如何工作 :)

This documentexplain how to create a dll from java and use it in C code. I'm not C# or java expert but i'm sure that you can load external dll's in C# as well. So not a complete solution but good starting point, IMHO. Generally dll it's a perfect way to mixing languages.

文档解释了如何从 java 创建一个 dll 并在 C 代码中使用它。我不是 C# 或 Java 专家,但我确信您也可以在 C# 中加载外部 dll。所以不是一个完整的解决方案,而是一个好的起点,恕我直言。通常 dll 是混合语言的完美方式。

回答by Fuangwith S.

In simple way you can pack your java classes to jar file then In C# use Processclass for execute and map IO stream

以简单的方式,您可以将 java 类打包到 jar 文件中,然后在 C# 中使用Process类执行和映射 IO 流

回答by Fuangwith S.

I did some research on this a few years ago (2005 I believe) and I liked JNBridgePro as the best third party product to do this. Check it out here http://www.jnbridge.com/

几年前我对此做了一些研究(我相信是 2005 年),我喜欢 JNBridgePro 作为最好的第三方产品。在这里查看http://www.jnbridge.com/

Good luck!

祝你好运!

回答by Pavel Savara

I am author of jni4net, open source intraprocess bridge between JVM and CLR. It's build on top of JNI and PInvoke. No C/C++ code needed. I hope it will help you.

我是jni4net 的作者,是 JVM 和 CLR 之间的开源进程内桥梁。它建立在 JNI 和 PInvoke 之上。不需要 C/C++ 代码。我希望它会帮助你。