java 是否可以使用Java来创建dll?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1771005/
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-10-29 17:54:50  来源:igfitidea点击:

Is it possible to use Java to create dll?

javadll

提问by Superhero

Want to create animation dll for Window XP Is it ok to create Java2d animation and export as dll??

想要为Window XP 创建动画dll 可以创建Java2d 动画并导出为dll 吗?

采纳答案by bmargulies

Yes. You need to write code in C++ to start the JVM with the invocation interface to JNI, and call into it. However, you may find it difficult to create windows in this way that integrate seamlessly with your Windows environment application to display your animation. This is a rather advanced JNI usage, and I'd recommend reading the JNI book before even trying a little bit of it.

是的。您需要用 C++ 编写代码以使用 JNI 的调用接口启动 JVM,并调用它。但是,您可能会发现很难以这种方式创建与 Windows 环境应用程序无缝集成以显示动画的窗口。这是一个相当高级的 JNI 用法,我建议在尝试之前阅读 JNI 书籍。

回答by o.k.w

I doubt so, unless there's some 3rd party tools out there. For your case where graphics is involved, chances are even lower.

我对此表示怀疑,除非那里有一些 3rd 方工具。对于涉及图形的情况,机会甚至更低。

回答by Andrew Rademacher

Actually, what Quentin said should work.
When you compile java to native with GCJyou first compile the .javafiles into platform specific .o(object) files. Presumably you would compile the .ofiles into a dllrather than an exe. GCJalso includes components like the garbage collector, and base java libraries. None of which require a JVMto run. The downer is that the dllwould be huge. A simple "Hello World" app when compiled with GCJis ~35MB, thanks to all the default libs and the garbage collector. Likewise your dllwould be huge.

事实上,昆汀所说的应该行得通。
当您将 java 编译为本机时,GCJ首先将.java文件编译为平台特定.o(对象)文件。据推测,您会将.o文件编译成.dll而不是exe. GCJ还包括垃圾收集器和基础 Java 库等组件。其中没有一个JVM需要运行。令人沮丧的是,这dll将是巨大的。使用GCJis编译时的简单“Hello World”应用程序~35MB,感谢所有默认库和垃圾收集器。同样你dll会很大。

回答by Patrick Cornelissen

No, IIRC you can't. DLLs are linked directly when loaded. Java code needs a jvm, so you can only provide a dll that starts a jvm and starts code there, but not all necessarily stuff fits in the dll.

不,IIRC 你不能。DLL 在加载时直接链接。Java 代码需要一个 jvm,所以你只能提供一个 dll 来启动一个 jvm 并在那里启动代码,但不一定所有的东西都适合这个 dll。

You should not do this. It looks like you're trying to use the wrong approach for your problem.

你不应该这样做。看起来您正试图对您的问题使用错误的方法。

回答by AaronLS

There are "bridges" that allow Java and non-Java code to call into one another. Depending on what you are trying to accomplish, these might be useful as you could write your Java code and then call into it from a C++ or C# DLL, depending on which language you are creating your DLL with, which will also determine what kind of bridge you need. I have never seen a freely provided bridge though. All the ones I've found when looking had to be purchased.

存在允许 Java 和非 Java 代码相互调用的“桥梁”。根据您要完成的任务,这些可能很有用,因为您可以编写 Java 代码,然后从 C++ 或 C# DLL 调用它,具体取决于您使用哪种语言创建 DLL,这也将决定哪种语言你需要的桥梁。不过,我从未见过免费提供的桥梁。我在寻找时找到的所有东西都必须购买。

回答by Quentin

Well…

好…

  1. GCJ is available for Windows.
  2. GCJ is part of GCC.
  3. GCC can create dlls.
  1. GCJ 适用于 Windows
  2. GCJ 是 GCC 的一部分。
  3. GCC 可以创建 dll

It might be possible to put that together to build DLLs using GCJ.

有可能将它们放在一起以使用 GCJ 构建 DLL。

回答by James

I am pretty sure you can only create .Jar files from java not dlls

我很确定你只能从 java 创建 .Jar 文件而不是 dll

回答by Carl Smotricz

I agree with bmargulies. It's probably feasible for an expert, but it would be a large DLL and you'd be mixing technologies that were never made to work together. It doesn't make sense to try this, in my opinion.

我同意 bmargulies。对于专家来说,这可能是可行的,但它会是一个大型 DLL,并且您将混合从未一起工作的技术。在我看来,尝试这个没有意义。

回答by Wolfgang Grinfeld

Yes, it is possible to generate DLLs from Java source code.

是的,可以从 Java 源代码生成 DLL。

2 Methods I have used:

2 我用过的方法:

  1. IKVM
  2. Graal
  1. 虚拟机
  2. 格拉尔

IKVM is mature, but rather slow in runtime execution of the generated DLL.

IKVM 已经成熟,但生成的 DLL 的运行时执行速度相当慢。

Graal is fast, but early days and immature in the Windows environment. See https://openjdk.java.net/jeps/295for further info.

Graal 速度很快,但在 Windows 环境中处于早期且不成熟。有关更多信息,请参阅https://openjdk.java.net/jeps/295

There are other commercial options available as well.

还有其他商业选项可用。