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

发布于 2024-07-22 12:54:12 字数 504 浏览 1 评论 0原文

我正在编写一个需要运行 java.jar 服务器的程序。 我需要直接运行该过程,以便我可以将输出重写到文本框,并且总而言之可以完全控制它。 我尝试通过 CMD.exe 执行此操作,但这不起作用,因为 CMD.exe 只会调用一个新进程 java.exe 而我无法控制它。 我需要直接调用 java.exe,这样我才能控制并获取输出。 你们中的任何人都可以告诉我如何转换此命令,以便我可以在 C# 中创建一个进程并调用它吗?

我需要将此 CMD 命令转换为:

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

转换为

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

我已经尝试这样做...但它不起作用。

我已经看了好几个小时了……请有人帮忙!

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?

I need this CMD command converted:

"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.

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情绪 2024-07-29 12:54:12

部分问题可能是,尽管框架文档说使用 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;
            }
        }
    }

这有点老套,但只要安装了 Java 运行时并且它的文件夹位于其中,它就会找到 java.exe Windows PATH 变量。 当您的程序第一次需要查找 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;

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:

    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:

   //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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文