从 Java 调用 C# 代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50398/
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
Calling C# code from Java?
提问by Keith G
Does anyone have a good solution for integrating some C# code into a java application?
有没有人有将一些 C# 代码集成到 Java 应用程序中的好方法?
The code is small, so I could re-write in java, but I would rather reuse the code if possible. Don't repeat yourself, etc.
代码很小,所以我可以用java重写,但如果可能的话,我宁愿重用代码。不要重复自己等等。
Also, I know I can expose the C# as a web service or whatever, but it has some security/encryption stuff in there, so I would rather keep it tightly integrated if possible.
此外,我知道我可以将 C# 公开为 Web 服务或其他什么,但它有一些安全/加密的东西,所以如果可能的话,我宁愿保持它紧密集成。
Edit: It's going to be on a server-based app, so "downloading" another runtime is irrelevant.
编辑:它将在基于服务器的应用程序上运行,因此“下载”另一个运行时无关紧要。
采纳答案by Tom Carter
There is an IL to Java Bytecode compiler GrassHopperwhich may be of use to you. I've never tried it though.
有一个 IL 到 Java 字节码编译器GrassHopper,它可能对您有用。不过我从来没有尝试过。
I'd look at rewriting your code in Java though
不过,我会考虑用 Java 重写您的代码
EDIT: Note that Grasshopper seems to be no longer available.
编辑:请注意 Grasshopper 似乎不再可用。
回答by Joel Coehoorn
If it's short, I think you're better off re-writing the code in java. Downloading one 50Mb runtime is bad enough.
如果它很短,我认为你最好用java重写代码。下载一个 50Mb 的运行时已经够糟糕的了。
回答by ScArcher2
I would rewrite it if it's not too much trouble. The web service would work, but it seems like that would be a lot of overhead just to reuse a little code.
如果不是太麻烦,我会重写它。Web 服务可以工作,但似乎仅重用少量代码就会产生大量开销。
回答by FlySwat
You would use the Java Native Interface to call your C# code compiled into a DLL.
您将使用 Java 本机接口调用编译为 DLL 的 C# 代码。
If its a small amount of C#, it would be much easier to port it to Java. If its a lot, this might be a good way to do it.
如果它是少量的 C#,那么将其移植到 Java 会容易得多。如果它很多,这可能是一个很好的方法。
Here is a highlevel overview of it:
这是它的高级概述:
http://en.wikipedia.org/wiki/Java_Native_Interface
http://en.wikipedia.org/wiki/Java_Native_Interface
Your other option would be to create a COM assembly from the C# code and use J-Interop to invoke it.
您的另一个选择是从 C# 代码创建一个 COM 程序集并使用 J-Interop 来调用它。
回答by Jakub ?turc
If you do not want to rewrite hadle it as an Inter-process communicationand choose one of following:
如果您不想将 hadle 重写为进程间通信,请选择以下之一:
- Named pipes
- Sockets
- SOAP
- 命名管道
- 插座
- 肥皂
回答by jodonnell
回答by Gili
http://www.infoq.com/articles/in-process-java-net-integrationsuggests running CLR and JVM in the same process space and passing calls back and forth. It sounds very efficient. I'm going to give it a try and integrate it into Jaceif it works well.
http://www.infoq.com/articles/in-process-java-net-integration建议在同一进程空间中运行 CLR 和 JVM 并来回传递调用。听起来很有效率。如果它运行良好,我将尝试一下并将其集成到Jace 中。
回答by Pavel Savara
回答by codenheim
If it is a piece of code that is exposable as a command line utility, I just make the other host language use a system call to execute the utility.
如果它是一段可作为命令行实用程序公开的代码,我只是让其他宿主语言使用系统调用来执行该实用程序。
If your C# app needs to call Java, compile a special Java main that takes appropriate command line args and returns text output.
如果您的 C# 应用程序需要调用 Java,请编译一个特殊的 Java main,它采用适当的命令行参数并返回文本输出。
It the oldest, simplest method.
它是最古老、最简单的方法。
回答by Starblazer2078
You can call your c# classes (compiled in a dll) via a bridging library, various libraries are available, every one with his characteristics. JNBridgegenerate proxy classes that you can call to manage the code in java classes. JCOBridgelet you load your c# classes and use it from java using the invoke mechanism, also javonetlet you import java classes and call java code using the invoke mechanism.
All the explored solutions are commercial solutions that let you call java code from .NET and vice-versa with graphical user interface integration and other amenities.
您可以通过桥接库调用您的 c# 类(在 dll 中编译),可以使用各种库,每个库都有其特点。JNBridge生成代理类,您可以调用这些代理类来管理 java 类中的代码。JCOBridge允许您加载 c# 类并使用调用机制从 java 使用它,javonet还允许您导入 java 类并使用 invoke 机制调用 java 代码。
所有探索的解决方案都是商业解决方案,可让您通过图形用户界面集成和其他便利从 .NET 调用 Java 代码,反之亦然。
Links:
jnbridgejava-.NET bridge Developer and Deployment license schema with 30 day free trial
jcobridgejava-.NET bridge Developer and Deployment license schema with unlimited Trial
javonetjava-.NET bridge Research and Professional license schema with 30-day unlimited Trial after sign-up
链接:
jnbridgejava-.NET bridge 开发者和部署许可架构,30 天免费试用
jcobridgejava-.NET bridge 开发者和部署许可架构,无限试用
javonetjava-.NET bridge 研究和专业许可架构,30 天无限试用报名