We've written a Java program which we are looking to use and interact with from C#. What are our options? Optimally it would be possible to compile the Java application as a library (.DLL) that we could reference from C# perhaps using P/Invoke. This, however, doesn't appear to be an option according to the first few searches online.
We opt to be able to use ASP.NET to built a search engine powered by the Java code, so if this opens up for any other options please let us know.
To call a specific Java function from C, you need to do the following: Obtain the class reference using the FindClass(,,) method. Obtain the method IDs of the functions of the class that you want to call using the GetStaticMethodID and GetMethodID function calls.
If you want to call native code from Java program, you should use JNI. This will require preparation in the C side of your code, but it works pretty good. If you are new to programming, again, I would recommend avoiding that. Regarding GUI - you can work with Swing.
Once you have a JVM and JNIEnv* (details omitted...) you can invoke the Java method from C++ as follows: jclass myClass = env->FindClass("foo. bar"); jmethodID mid = env->GetStaticMethodID(myClass, "timesTen", "(I)I"); jint hundred = env->CallStaticIntMethod(myClass, mid, (jint)10);
Code in Java, Execute as C++. However, at some point, we need to integrate these languages, e.g. calling a method written in Java to your C++ code. The need to integrate Java and C++ is not something new. In fact, we can find a tutorial dated back to 1996. Since then, the two languages have developed remarkably.
Sorry, you cannot
call java code / classes
Directly
from C# code.
One way of doing this is to wrap
up your java classes in a java Web Service
and call classes indirectly
through that web service interface in your C# code.
Another way is using
javareg.exe
which exposes java classes as COM
. You can find it at following location:
C:\Program Files\Microsoft VisualStudio\VIntDev98\bin\javareg.exe
Following posts might help as well
The simplest approach would probably be to publish the functionality of your java library as web services and add a web-reference from your asp.net application.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With