Dear all (mostly Johannes, I suspect),
I have a java method that is currently part of an ImageJ plugin and I need to turn it into a DLL for use in a different environment. The method takes some arrays of doubles and returns an array of doubles. The problem seems like the opposite of the one that can be solved with JNI: calling DLLs from within java. The intern that I gave the problem to found that something called IKVM can be used to turn a java program that contains a main method into an executable .exe file. This is close, but I need a function. It looks like IKVM offers a way to package java methods as c functions, but we are having trouble getting started. We don't know which c IDE, if any, to use to test it. In any case, is IKVM the right answer? Has anyone pioneered this before? Bob Robert Dougherty, Ph.D. President, OptiNav, Inc. 1414 127th Place NE #106 Bellevue, WA 98005 Tel. (425)891-4883 FAX (425)467-1119 www.optinav.com [hidden email] -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
Hi Bob,
On Mon, 30 Jul 2012, Robert Dougherty wrote: > Dear all (mostly Johannes, I suspect), how did you come to that idea? :-) > I have a java method that is currently part of an ImageJ plugin and I > need to turn it into a DLL for use in a different environment. The > method takes some arrays of doubles and returns an array of doubles. > The problem seems like the opposite of the one that can be solved with > JNI: calling DLLs from within java. The intern that I gave the problem > to found that something called IKVM can be used to turn a java program > that contains a main method into an executable .exe file. This is > close, but I need a function. It looks like IKVM offers a way to > package java methods as c functions, but we are having trouble getting > started. We don't know which c IDE, if any, to use to test it. In any > case, is IKVM the right answer? Has anyone pioneered this before? There are a couple of ways. One way I explored briefly is to make a shared library that instantiates a JVM and runs the Java code as it would usually do, returning the result back via JNI. However, this approach has a can of problems, such as: discovery of Java at runtime, startup time of the JVM, clunky JNI API. Another way, if your code lends itself to it, would be to write an automatic translator from Java to C. That limits the syntax of your source code unless you are willing to pour an insane amount of time into the translator. The way I would recommend is actually to use GNU C's Java component: GCJ. It is able to compile Java directly into a shared library, and unless you play cute games such as reflection of Java classes or even generating Java classes on the fly, it is really fast and robust. The website is here: http://gcc.gnu.org/java/ Here is an example. First the Java class: -- snip Dougherty.java -- public class Dougherty { public static int Robert(int[] list) { return list.length; } } -- snap -- Compile it with gcj -fPIC -shared -o libdougherty.so Dougherty.java This is for Linux, of course. If I recall correctly, you do your development on Windows, where you do not need -fPIC because the output is not an ELF library, and you would replace the extension ".so" by ".dll". Also, to use gcj Windows, you most likely want to use MinGW: http://www.mingw.org/wiki/Compile_with_gcj Now the C++ header file: -- snip dougherty.hh -- #include <gcj/array.h> #include <java/lang/Object.h> class Dougherty : public ::java::lang::Object { public: static jint Robert(jintArray array); }; -- snap -- and the corresponding C++ file: -- snip dougherty.cc -- #include "dougherty.hh" #include <gcj/cni.h> #include <stdio.h> int main(int argc, char **argv) { JvCreateJavaVM(NULL); JvAttachCurrentThread(NULL, NULL); jintArray array = JvNewIntArray(20); printf("result: %d\n", (int)Dougherty::Robert(array)); return 0; } -- snap -- One thing to keep in mind with GCJ when you intend to target Windows platforms: Typically, C++ programmers on Windows use Visual C++ which produces assembler code that disagrees with the C++ standard, while GCJ (being part of the GNU C Compiler collection) produces assembler code that adhers to the official C++ ABI. This is only an issue if you want to expose a C++ API. My recommendation would therefore be to write pure-C wrappers (i.e. not object-oriented) wrapping the C++ methods so that other people can link to your shared library from Visual C++ projects. Ciao, Johannes -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
Johannes,
Wow. Thanks. We'll try it and report back. Bob >> I have a java method that is currently part of an ImageJ plugin and I ... > There are a couple of ways. ... -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
In reply to this post by Robert Dougherty
Hi Bob,
I agree with Johannes's suggestion of GCJ. From your use case it sounds like it will work for you. If for some reason it doesn't, I explored several ways of doing cross-language integration while trying to make it easier to call Bio-Formats from C++. See this page for details: http://loci.wisc.edu/bio-formats/interfacing-non-java-code Of particular interest might be the Jace library, which is designed to make it easy to wrap Java code in C++ proxies for easier access from C++: http://sourceforge.net/projects/jace/ Regarding IKVM.NET, when I tried it five years ago it was very slow and buggy, and did not support the full Java specification (particularly AWT). But five years later, maybe it's awesome now. However, my understanding is that IKVM.NET is mainly for use from .NET applications, and needs to run the Java code in a CLR-managed runtime (see http://en.wikipedia.org/wiki/Managed_code for details). Regards, Curtis On Mon, Jul 30, 2012 at 1:14 PM, Robert Dougherty <[hidden email]> wrote: > Dear all (mostly Johannes, I suspect), > > I have a java method that is currently part of an ImageJ plugin and I need > to turn it into a DLL for use in a different environment. The method takes > some arrays of doubles and returns an array of doubles. The problem seems > like the opposite of the one that can be solved with JNI: calling DLLs from > within java. The intern that I gave the problem to found that something > called IKVM can be used to turn a java program that contains a main method > into an executable .exe file. This is close, but I need a function. It > looks like IKVM offers a way to package java methods as c functions, but we > are having trouble getting started. We don't know which c IDE, if any, to > use to test it. In any case, is IKVM the right answer? Has anyone > pioneered this before? > > Bob > > > Robert Dougherty, Ph.D. > President, OptiNav, Inc. > 1414 127th Place NE #106 > Bellevue, WA 98005 > Tel. (425)891-4883 > FAX (425)467-1119 > www.optinav.com > [hidden email] > > -- > ImageJ mailing list: http://imagej.nih.gov/ij/list.html > -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
Free forum by Nabble | Edit this page |