问题描述
我有一个Java类和一个C#类。我想从我的Java类中运行该C#类。
I have a java class and a c# class. I want to run that C# class from my java class.
但是我不想将任何东西从Java代码传递给c#,也不想返回任何东西从C#代码开始,我只想运行该C#代码。
However I don't want to pass anything from java code to c# and also don't want anything in return from C# code, I just want to run that C# code.
我想做下面的类所示的事情
I want to do something like shown in below classes
Java类:
public void static main(String[] args){
System.out.println("Running Java code ");
// here need to call C# class
}
}
我希望此代码从java程序之上执行
I want this code to be executed from above java program
using System;
class Program {
Console.WriteLine("Running C# code ");
}
}
推荐答案
您可以从Java代码运行C#程序 exe
文件。
You can run the C# program exe
file from java code.
首先编译C#.NET程序以获取 Program.exe
文件,然后从Java代码运行相同的 Program.exe
,如下所示:
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();
}
编辑:
您可以通过将参数传递给ProcessBuilder构造函数,将参数发送到要调用的exe文件,如下所示:
You can send the parameters to the exe file to be invoked by passing the arguments to the ProcessBuilder constructor as below:
注意:此处将两个参数传递给Program.exe文件名和ID:
Note : here im passing two argumenbts to Program.exe file Name and ID :
process = new ProcessBuilder("C:\\ProjectsPath\\Program.exe" , "Sudhakar","ID501").start();
这篇关于从Java类调用C#类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!