Calling C# code from Java?
There is an IL to Java Bytecode compiler GrassHopper which may be of use to you. I've never tried it though.
I'd look at rewriting your code in Java though
EDIT: Note that Grasshopper seems to be no longer available.
How to call C# function from java
First of all lets create a C# file like this:
using System;
public class Test{
public Test(){}
public String ping(){
return "C# is here.";
}
}
Then compile this with command below:
csc.exe /target:module Test.cs
You can find csc.exe
in install path of .NET framework. After that create java file:
public class Test{
public native String ping();
public static void main(String[] args){
System.load("/path/to/dll");
System.out.println("Java is running.");
Test t = new Test();
System.out.println("Trying to catch C# " + r.ping());
}
}
javac Test.java
This generates a Test.class
.
javah -jni Test
This generates a Test.h
file which will be included in
C++ code.
After that we need to create our C++ file:
#include "stdafx.h"
#include "JAVA/Test.h"
#include "MCPP/Test.h"
#pragma once
#using <mscorlib.dll>
#using "Test.netmodule"
JNIEXPORT jstring JNICALL Java_Test_ping(JNIEnv *env, jobject obj){
Test^ t = gcnew Test();
String^ ping = t->ping();
char* str = static_cast<char*>((System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(ping)).ToPointer());
char cap[128];
strcpy_s(cap, str);
return env->NewStringUTF(cap);
}
Finally:
c:\>java Test
I hope this helps you. A basic example to use function C# in Java.
Sources:
https://www.quora.com/How-common-is-the-problem-of-calling-C-methods-from-Java-Do-many-developers-come-across-such-necessity
JNI: Call C# method from Java
Actually, you cannot directly call the C# method from Java code but you can write C++ wrapper. This C++ wrapper can be get called from Java code and eventually this C++ wrapper can call C# method.
So it would be like this: Java --> C++ Wrapper --> C#
Run C# code from Java and vice-versa
Take a look at jni4net if you're targeting Windows. It's an alpha quality release, but Robocode already uses it to run .NET robots inside the Java runtime.
Another option is to use a high-performance messaging approach. You'll need a second process - likely a .NET plug-in host. That process then exchanges messages with the main Java game process. Messaging libraries like 0MQ are pretty darn fast but may not be fast enough for what you have in mind. In addition, you'll have to create a lot of message plumbing which may be cost/time prohibitive.
Calling C# code from Java?
There is an IL to Java Bytecode compiler GrassHopper which may be of use to you. I've never tried it though.
I'd look at rewriting your code in Java though
EDIT: Note that Grasshopper seems to be no longer available.
Call C# class from Java class
You can run the C# program exe
file from java code.
first compile the C#.NET program to get the Program.exe
file then run the same Program.exe
from java code as below:
public static void main(String[] args) throws IOException {
// TODO code application logic here
Process process;
process = new ProcessBuilder("C:\\ProjectsPath\\Program.exe").start();
}
Edit:
You can send the parameters to the exe file to be invoked by passing the arguments to the ProcessBuilder constructor as below:
Note : here im passing two argumenbts to Program.exe file Name and ID :
process = new ProcessBuilder("C:\\ProjectsPath\\Program.exe" , "Sudhakar","ID501").start();
Call C# mono code from java under linux
- Use Mono to compile your C# classes on the Linux platform of your choice;
- Use JNI to write a set of facade classes between your java code and the compiled C# code.
Related Topics
Docking Window Inside Another Window
What Exactly Happens When I Set Loaduserprofile of Iis Pool
C# Namespace Alias - What's the Point
C#: Passing Null to Overloaded Method - Which Method Is Called
How to Sort a List of Objects by a Specific Field in C#
How to Get the Z-Order in Windows
Custom Deserialization Using JSON.Net
Main Method Code Entirely Inside Try/Catch: Is It Bad Practice
Understanding Wcf Windows Authentication
5.7.57 Smtp - Client Was Not Authenticated to Send Anonymous Mail During Mail from Error
Webclient.Downloadstring() Returns String with Peculiar Characters
For VS. Linq - Performance VS. Future
How to Tell JSON.Net Globally to Apply the Stringenumconverter to All Enums