Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interacting with java code from C#

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.

like image 593
Kasper Holdum Avatar asked Mar 17 '10 12:03

Kasper Holdum


People also ask

How can I call a Java program from C program?

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.

Can Java and C work together?

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.

Can I call Java from C++?

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);

Can I use Java and C++ together?

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.


2 Answers

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

like image 129
Asad Avatar answered Sep 23 '22 17:09

Asad


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.

like image 45
Klaus Byskov Pedersen Avatar answered Sep 22 '22 17:09

Klaus Byskov Pedersen