如何从 Java 调用 .NET 代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/283679/
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
How can I call .NET code from Java?
提问by Eduardo Santa
I'm not looking for the usual answer like Web-services. I'm looking for a light solution to be run in the same machine.
我不是在寻找像 Web 服务这样的常见答案。我正在寻找在同一台机器上运行的轻型解决方案。
Edit: I'm looking for way in Java to call .NET methods
编辑:我正在寻找在 Java 中调用 .NET 方法的方法
采纳答案by Nick Berardi
I believe Java can talk to COM and .NET can expose COM interfaces. So that may be a very light weight solution that doesn't require any 3rd party. There is also the option of using sockets to communicate between the programs which wouldn't require a heavy instance of IIS to be installed on the machine.
我相信 Java 可以与 COM 通信,.NET 可以公开 COM 接口。所以这可能是一个非常轻量级的解决方案,不需要任何 3rd 方。还可以选择使用套接字在程序之间进行通信,这不需要在机器上安装大量的 IIS 实例。
回答by RB.
Hve you looked into the Java Native Interface?
您是否研究过 Java Native Interface?
What I have done in the past is to write a C library, which is callable from both Java and .NET (and also COM, NSIS scripting language, and other technologies).
我过去所做的是编写一个 C 库,它可以从 Java 和 .NET(以及 COM、NSIS 脚本语言和其他技术)调用。
The JNI would work well if you only want to expose a few methods, but it might get unwieldy if you wanted to, for example, expose the entire .NET framework, because of the number of header files and wrapper methods you would need to create.
如果您只想公开一些方法,JNI 可以很好地工作,但如果您想公开整个 .NET 框架,它可能会变得笨拙,因为您需要创建大量的头文件和包装方法.
回答by Tony Whalen
We tried IKVM in our production environment but it kept crashing. We use JNBridge which is a commercial product but is very stable and performs well in our ASP.NET environment.
我们在生产环境中尝试了 IKVM,但它一直崩溃。我们使用 JNBridge,它是一种商业产品,但非常稳定并且在我们的 ASP.NET 环境中表现良好。
回答by Pavel Savara
回答by Mechanical snail
If you're willing to run your Java code under .NET (or Mono), it is possible to do this using IKVM.
如果您愿意在 .NET(或 Mono)下运行 Java 代码,则可以使用IKVM来执行此操作。
Because (as far as I know) current Java compilers don't read .NET assemblies, there are 2 steps. First you need to generate a stub JARcontaining dummy classes and methods with the signatures of the .NET assembly you want to use, so that javac
(or your IDE) knows what methods are available. For example, if you want to use something in the main .NET standard library from Java, run
因为(据我所知)当前的 Java 编译器不读取 .NET 程序集,所以有 2 个步骤。首先,您需要生成一个存根 JAR,其中包含带有您要使用的 .NET 程序集签名的虚拟类和方法,以便javac
(或您的 IDE)知道哪些方法可用。例如,如果您想使用 Java 主 .NET 标准库中的某些内容,请运行
ikvmstub mscorlib
which generates the stub mscorlib.jar
. (It found the Mono mscorlib.dll
assembly for me automatically under Linux, but if it fails you may have to give the full path to the DLL.)
生成存根mscorlib.jar
。(它mscorlib.dll
在 Linux 下自动为我找到 Mono程序集,但如果失败,您可能必须提供 DLL 的完整路径。)
You can then write a Java file that uses it, e.g. (based on the example from the IKVM documentation):
然后您可以编写一个使用它的 Java 文件,例如(基于 IKVM 文档中的示例):
import cli.System.IO.Directory;
public class IKVMTest {
public static void main(String[] args) {
for(String file : Directory.GetFiles(".")) // From .NET standard library
System.out.println(file); // From Java standard library
}
}
Note that CLI packges are prefixed with cli.
, hence the import cli.System
instead of just System
.
请注意,CLI 包以 为前缀cli.
,因此导入cli.System
而不仅仅是System
.
To compile, include the stub JAR on the classpath:
要编译,请在类路径中包含存根 JAR:
javac -classpath mscorlib.jar IKVMTest.java
Since Java linking occurs at runtime, the output class
files use the methods of the desired names and signatures, but you can swap out the dummy stub methods with the real .NET methods when run under IKVM:
由于 Java 链接发生在运行时,因此输出class
文件使用所需名称和签名的方法,但在 IKVM 下运行时,您可以用真正的 .NET 方法交换虚拟存根方法:
ikvm IKVMTest
and it will print the files in the current directory by calling the .NET methods.
它将通过调用 .NET 方法打印当前目录中的文件。