在 C# 中使用 .jar java 库 API?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/512124/
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
Use a .jar java library API in C#?
提问by timmyg
I'm an entry level programmer so please be descriptive in your responses.
我是一名入门级程序员,所以请在您的回复中进行描述。
I am trying to use a Java API given as a .jar file in my C# .net application. I don't know much Java, but this .jar file says "no main-class manifest attribute" when I try to run it so this means its a library? This Java API also comes with a .java file which shows how to use the library but I cannot see the code inside the .jar.
我正在尝试在我的 C# .net 应用程序中使用作为 .jar 文件给出的 Java API。我不太了解 Java,但是当我尝试运行这个 .jar 文件时,它说“没有主类清单属性”,所以这意味着它是一个库?此 Java API 还附带一个 .java 文件,该文件显示了如何使用该库,但我看不到 .jar 中的代码。
I found this questionon this site, and one of the answers reads, "In simple way you can pack your java classes to jar file then In C# use Process class for execute and map IO stream." I am semi-familiar with the Process class in C# but I don't understand how I could use it to use a Java library in my C# .net project.
我在这个站点上发现了这个问题,其中一个答案是:“通过简单的方式,您可以将 Java 类打包到 jar 文件中,然后在 C# 中使用 Process 类来执行和映射 IO 流。” 我对 C# 中的 Process 类半熟悉,但我不明白如何使用它在我的 C# .net 项目中使用 Java 库。
Is this possible? or was that answer incorrect?
这可能吗?还是那个答案不正确?
If so, could you explain how I can use the .jar library in my C# app.
如果是这样,您能否解释一下我如何在我的 C# 应用程序中使用 .jar 库。
采纳答案by Stephen Curial
You can do it using IVKM.Net. IVKM.NET includes an application called ikvmc. Here's the documentation for this tool:
您可以使用 IVKM.Net 来完成。IVKM.NET 包括一个名为 ikvmc 的应用程序。这是此工具的文档:
http://www.ikvm.net/userguide/ikvmc.html
http://www.ikvm.net/userguide/ikvmc.html
To use it compile your java code into a Jar.
要使用它,请将您的 Java 代码编译成 Jar。
Then run the ikvmc program:
然后运行ikvmc程序:
ikvmc myCode.jar
ikvmc myCode.jar
If your jar contains a main() function, it will be converted into an exe that can be run on the CLR. Otherwise it will be converted into dll's. To use the dll's in your project just add them as references in Visual Studio and the same API that was available to you in Java will be accessible in your .Net application.
如果您的 jar 包含 main() 函数,它将被转换为可以在 CLR 上运行的 exe。否则它会被转换成dll。要在您的项目中使用 dll,只需将它们添加为 Visual Studio 中的引用,您可以在 .Net 应用程序中访问 Java 中可用的相同 API。
You can also include the IKVM.GNU.Classpath.dll dll from ikvmc and use the standard java class libraries in your application.
您还可以包含来自 ikvmc 的 IKVM.GNU.Classpath.dll dll,并在您的应用程序中使用标准的 java 类库。
回答by Rad
回答by Maciej Pulikowski
You could use IKVM.NET - http://www.ikvm.net/userguide/ikvmc.html
On the official website in download - you can get ikvmbin-7.2.4630.5(Works up to Java 7)
However, on the owner's blog, you can download a newer version. http://weblog.ikvm.net/default.aspx- You can get ikvmbin-8.1.5717.0(Works up to Java 8)
To create dll/exe use:
ikvmc hello.jar
On the other hand, if you can edit .jar lib (you created it) you could use http://jni4net.com/project.
您可以使用 IKVM.NET - http://www.ikvm.net/userguide/ikvmc.html
在官方网站上下载 - 您可以获得 ikvmbin-7.2.4630.5(Works up to Java 7)
但是,在所有者的博客上,您可以下载更新的版本。 http://weblog.ikvm.net/default.aspx- 您可以获得 ikvmbin-8.1.5717.0(适用于 Java 8)
要创建 dll/exe 使用:
ikvmc hello.jar
另一方面,如果您可以编辑 .jar lib(您创建它),您可以使用http://jni4net.com/项目。