C#:我如何直接执行此命令行到java.exe?

时间:2021-05-22 20:47:52

I am writing a program that needs to run a java.jar server. I need to run the process directly so I can rewrite the output to a textbox and all-in-all have complete control of it. I tried just doing it through CMD.exe, but that wouldnt work because CMD.exe would just call a new process java.exe and I wouldn't have control of it. I need to call java.exe directly so I can have the control and get the output. Can any of you tell me how to convert this command so I could create a process in C# and call it?

我正在编写一个需要运行java.jar服务器的程序。我需要直接运行该过程,以便我可以将输出重写为文本框,并且所有内容都可以完全控制它。我试过通过CMD.exe做这件事,但那不行,因为CMD.exe只会调用一个新进程java.exe而我无法控制它。我需要直接调用java.exe,这样我就可以获得控件并获得输出。你们中的任何人都可以告诉我如何转换这个命令,这样我就可以在C#中创建一个进程并调用它吗?

I need this CMD command converted:

我需要转换这个CMD命令:

"java -Xmx1024m -cp ./../libs/*;l2jserver.jar net.sf.l2j.gameserver.GameServer"

“java -Xmx1024m -cp ./../libs/*;l2jserver.jar net.sf.l2j.gameserver.GameServer”

into

a command line I can put into the Process.Arguments so I can call Java.exe directly.

我可以将一个命令行放入Process.Arguments中,这样我就可以直接调用Java.exe了。

I've tried to do it... and it just won't work.

我试过这样做......它就行不通了。

I've been looking at this for hours and hours... please someone help!

我一直在看这个好几个小时...请有人帮忙!

1 个解决方案

#1


Part of the problem might be that despite what the Framework documentation says using Process doesn't always resolve things against the PATH environment variable properly. If you know the name of the folder Java is in then use the full path to Java.exe, otherwise use a function like the following:

部分问题可能是,尽管Framework文档说使用Process并不总是正确地解决PATH环境变量的问题。如果您知道Java所在文件夹的名称,请使用Java.exe的完整路径,否则请使用如下所示的函数:

    private void LocateJava()
    {
        String path = Environment.GetEnvironmentVariable("path");
        String[] folders = path.Split(';');
        foreach (String folder in folders)
        {
            if (File.Exists(folder + "java.exe"))
            {
                this._javadir = folder;
                return;
            } 
            else if (File.Exists(folder + "\\java.exe")) 
            {
                this._javadir = folder + "\\";
                return;
            }
        }
    }

It's somewhat hacky but it will find java.exe provided the Java Runtime is installed and it's folder is in the windows PATH variable. Make a call to this function the first time your program needs to find Java and then subsequently start Java using the following:

它有点hacky但是如果安装了Java Runtime并且它的文件夹在windows PATH变量中,它会找到java.exe。在程序第一次需要查找Java然后使用以下代码启动Java时调用此函数:

   //Prepare the Process
   ProcessStartInfo start = new ProcessStartInfo();
   if (!_javadir.Equals(String.Empty)) {
        start.FileName = this._javadir + "java.exe";
   } else {
        start.FileName = "java.exe";
   }
   start.Arguments = "-Xmx1024m -cp ./../libs/*;l2jserver.jar net.sf.l2j.gameserver.GameServer";
   start.UseShellExecute = false;
   start.RedirectStandardInput = true;
   start.RedirectStandardOutput = true;

   //Start the Process
   Process java = new Process();
   java.StartInfo = start;
   java.Start();

   //Read/Write to/from Standard Input and Output as required using:
   java.StandardInput;
   java.StandardOutput;

#1


Part of the problem might be that despite what the Framework documentation says using Process doesn't always resolve things against the PATH environment variable properly. If you know the name of the folder Java is in then use the full path to Java.exe, otherwise use a function like the following:

部分问题可能是,尽管Framework文档说使用Process并不总是正确地解决PATH环境变量的问题。如果您知道Java所在文件夹的名称,请使用Java.exe的完整路径,否则请使用如下所示的函数:

    private void LocateJava()
    {
        String path = Environment.GetEnvironmentVariable("path");
        String[] folders = path.Split(';');
        foreach (String folder in folders)
        {
            if (File.Exists(folder + "java.exe"))
            {
                this._javadir = folder;
                return;
            } 
            else if (File.Exists(folder + "\\java.exe")) 
            {
                this._javadir = folder + "\\";
                return;
            }
        }
    }

It's somewhat hacky but it will find java.exe provided the Java Runtime is installed and it's folder is in the windows PATH variable. Make a call to this function the first time your program needs to find Java and then subsequently start Java using the following:

它有点hacky但是如果安装了Java Runtime并且它的文件夹在windows PATH变量中,它会找到java.exe。在程序第一次需要查找Java然后使用以下代码启动Java时调用此函数:

   //Prepare the Process
   ProcessStartInfo start = new ProcessStartInfo();
   if (!_javadir.Equals(String.Empty)) {
        start.FileName = this._javadir + "java.exe";
   } else {
        start.FileName = "java.exe";
   }
   start.Arguments = "-Xmx1024m -cp ./../libs/*;l2jserver.jar net.sf.l2j.gameserver.GameServer";
   start.UseShellExecute = false;
   start.RedirectStandardInput = true;
   start.RedirectStandardOutput = true;

   //Start the Process
   Process java = new Process();
   java.StartInfo = start;
   java.Start();

   //Read/Write to/from Standard Input and Output as required using:
   java.StandardInput;
   java.StandardOutput;