-1

I have a requirement to compile and run java programs from C# dynamically by passing parameters. Ex: below is java code i have to execute this code from c# by passing parameters num1, num2. How to do in c#?

  import java.util.Scanner;
 public class AddTwoNumbers2 {

public int CalAdd(int num1, int num2) {   
  return num1 + num2;
    
  }
 }   

I am looking for calling Java code file in c# like below, python code called in c# example code

       var engine = Python.CreateEngine();
        var scope = engine.CreateScope();
        //try
        //{

        var compilerOptions = (PythonCompilerOptions)engine.GetCompilerOptions();
     ErrorSinkProxyListener(errorSink);
        var scriptSource = engine.CreateScriptSourceFromFile(@"C:\Nidec\PythonScript\download_nrlist.py", Encoding.UTF8, Microsoft.Scripting.SourceCodeKind.File);
        var compiledCode = scriptSource.Compile();
        compiledCode.Execute(scope);
        engine.ExecuteFile(@"C:\Nidec\PythonScript\download_nrlist.py", scope);

        //get function and dynamically invoke
        var calcAdd = scope.GetVariable("CalcAdd");           
        result = calcAdd(34, 8); // returns 42 (Int32)
3
  • You run the same commands you would normally do, i.e. javac to compile the code, then java to run it, you just run the commands from C#. If you don't know how to run javac or java, how are you running the Java code to test it? If you don't know how to run commands from C#, do some research, i.e. a web search. Since all of that is very easy, why did you need to ask? Or more specifically, what is stopping you from doing any of those things? Commented May 25, 2021 at 13:18
  • 2
    Does this answer your question? How to call Java code from C#? Commented May 25, 2021 at 13:21
  • or stackoverflow.com/questions/1469764/run-command-prompt-commands Commented May 25, 2021 at 13:31

1 Answer 1

0

You can put the java run commands inside a .sh or .bat file and then use System.Diagnostics.ProcessStartInfo in your C# method to execute that shell script/batch file.

Code snippet:

class MyProcess {
        // Opens urls and .html documents using Internet Explorer.
        void OpenWithArguments()
        {
            // url's are not considered documents. They can only be opened
            // by passing them as arguments.
            Process.Start("IExplore.exe", "www.northwindtraders.com");

            // Start a Web page using a browser associated with .html and .asp files.
            Process.Start("IExplore.exe", "C:\\myPath\\myFile.htm");
            Process.Start("IExplore.exe", "C:\\myPath\\myFile.asp");
        }

        static void Main()
        {
            MyProcess myProcess = new MyProcess();
            myProcess.OpenWithArguments();
        }
}

Refer this

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.