How can I call .NET code from Java?

I'm not looking for the usual answer like Web-services. I'm looking for a light solution to be run in the same machine.

Edit: I'm looking for way in Java to call .NET methods

Asked by: Adelaide347 | Posted: 21-01-2022

Answer 1

I am author of jni4net, open source interprocess bridge between JVM and CLR. It's build on top of JNI and PInvoke. No C/C++ code needed. I hope it will help you.

Answered by: Edgar877 | Posted: 22-02-2022

Answer 2

I believe Java can talk to COM and .NET can expose COM interfaces. So that may be a very light weight solution that doesn't require any 3rd party. There is also the option of using sockets to communicate between the programs which wouldn't require a heavy instance of IIS to be installed on the machine.

Answered by: Lenny848 | Posted: 22-02-2022

Answer 3

Have you looked at IKVM?

Answered by: Kellan834 | Posted: 22-02-2022

Answer 4

Hve you looked into the Java Native Interface?

What I have done in the past is to write a C library, which is callable from both Java and .NET (and also COM, NSIS scripting language, and other technologies).

The JNI would work well if you only want to expose a few methods, but it might get unwieldy if you wanted to, for example, expose the entire .NET framework, because of the number of header files and wrapper methods you would need to create.

Answered by: Freddie357 | Posted: 22-02-2022

Answer 5

We tried IKVM in our production environment but it kept crashing. We use JNBridge which is a commercial product but is very stable and performs well in our ASP.NET environment.

Answered by: Kellan334 | Posted: 22-02-2022

Answer 6

If you're willing to run your Java code under .NET (or Mono), it is possible to do this using IKVM.

Because (as far as I know) current Java compilers don't read .NET assemblies, there are 2 steps. First you need to generate a stub JAR containing dummy classes and methods with the signatures of the .NET assembly you want to use, so that javac (or your IDE) knows what methods are available. For example, if you want to use something in the main .NET standard library from Java, run

ikvmstub mscorlib

which generates the stub mscorlib.jar. (It found the Mono mscorlib.dll assembly for me automatically under Linux, but if it fails you may have to give the full path to the DLL.)

You can then write a Java file that uses it, e.g. (based on the example from the IKVM documentation):

import cli.System.IO.Directory;
public class IKVMTest {
    public static void main(String[] args) {
        for(String file : Directory.GetFiles(".")) // From .NET standard library
            System.out.println(file);              // From Java standard library

Note that CLI packges are prefixed with cli., hence the import cli.System instead of just System.

To compile, include the stub JAR on the classpath:

javac -classpath mscorlib.jar

Since Java linking occurs at runtime, the output class files use the methods of the desired names and signatures, but you can swap out the dummy stub methods with the real .NET methods when run under IKVM:

ikvm IKVMTest

and it will print the files in the current directory by calling the .NET methods.

Answered by: Melissa977 | Posted: 22-02-2022

Similar questions

c# - How to call into .NET dll from Java

I have this code to create a simple .NET .dll. It only returns an int. But, it is not working inside Java. using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ReturnINT { public class ReturnINT { public static int RetornaInteiro () { try { int number = 2; ...

Call my own Java code from C#

Having my own Java code I'm using C# to call some unmanaged code that call (via JNI) the java code. I'm using JNI since I need to ensure: the ability that the Java code will run over real JVM and not over some .NET VM the ability to attach to the VM for debugging (IKVM does'nt support it) I need free solution The current free solutions are not applicable (e.g. IKVM) A...

how to call jsp file from java?

I have two jsp file and one java file. My constraints is if jspfile1 call java then java file call the jspfile2. Is it possible? How to achieve this?

How to call java from C++

I need to run this line from my c++ program: java -jar test.jar text1 text2 the java app will give a float value and give it to the c++ program. How can I do this? I never call a java stuff before from my ms visual studio C++ file.

java - Call a HTML File From Jar

Im generating a report from a template html file in my program. It resides in /src/main/resources and its name is "template.html". Im using ClassLoader inside the code like this: private String readTemplateFile() { String str = ""; URL url = ClassLoader.getSystemResource("template.html"); try { FileReader input = new FileReader(url.getFile()); BufferedRead...

java - How can i call my page using sms?

I want to develop a web application in JSP ,in which a user can sent their id using their mobile number and after that they will get their data like total amount,balance etc. I am not getting how can i do this? If you can provide any api name and example that will be great for me. Thanks

java - Call from one JSP file to another JSP

I need to call from one JSP to another, do some stuff over there.. I am inside the caller jsp, inside his handleReqeust(HttpServletReqeust request) method I am trying to forward the request to another JSP, to call the other JSP file to his handleRequest(HttpServletReqeust request) off course with the request object I tried it: RequestDispatcher dispatcher = request.getRequestDispatcher("/t...

java - Getting Call value is null

In my code I am getting a value of Call is null. i.e I am using three internal API's they are ht...

Can C++ call Java code?

I know that Java code can call C++ code through JNI. Is it possible, however, to call Java code from C++ again through JNI or with any other method?

java - How to call a view by its given ID

I have a loop that creates 64 buttons, and within the loop, the button gets an id using button.setId(n) The question is, how do I call the button with a certain id to change its properties. Ideally, I'm looking for something similar to this ImageView button2 = (ImageView)findViewById(button.("with id 14, for example"))

Call Java code from C#

I'm stuck on one thing i can't get solved. I have part of code, which is executed from command line like a charm. Works without any problem. So, i will to try to call this command (same) out from C#. That's the code i'm run from commandline. java -Xincgc -Xmx1024m -cp "%APPDATA%.minecraft\bin\minecraft.jar;%APPDATA%.minecraft\bin\lwjgl.jar;%APPDATA%.minecraft\bin\lwjgl_util.jar;%APP...

Still can't find your answer? Check out these amazing Java communities for help...

Java Reddit Community | Java Help Reddit Community | Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)