How to compile Java into C# code using Process.Start?

Henrique Rodrigues 1 Reputation point
2022-07-19T14:26:31.863+00:00

Well, I have a small project in C#, which would compile several programs in different languages, but it's not working very well, I was trying to compile Java in C# CONSOLE using Process.Start, but I get several errors

try  
{  
Process.Start("cmd.exe", $@"javac App.java && java App ""C:\Java P1\src\""");  
 }  
catch(Exception ex)  
{  
Console.WriteLine(ex);  
}  

please i need help asap
I thank

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
9,936 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Henrique Rodrigues 1 Reputation point
    2022-07-20T12:48:41.927+00:00

    well, solve my problem using this command

    Process cmd = new Process();  
                        cmd.StartInfo.FileName = "cmd.exe";  
                        cmd.StartInfo.RedirectStandardInput = true;  
                        cmd.StartInfo.RedirectStandardOutput = true;  
                        cmd.StartInfo.CreateNoWindow = true;  
                        cmd.StartInfo.UseShellExecute = false;  
                          
      
                        cmd.Start();  
      
                        cmd.StandardInput.WriteLine($@"cd ""C:\src\"" && javac App.java && java App");  
                        cmd.StandardInput.Flush();  
                        cmd.StandardInput.Close();  
                        Console.WriteLine(cmd.StandardOutput.ReadToEnd()); //SAÍDA  
    
    0 comments No comments