简体   繁体   English

从 C# 执行 a.jar 文件,但它在错误的位置生成文件

[英]execute a .jar file from C# but it generates files at wrong location

I have coded a Minecraft Server Runner in C# WinForms which lets you run a Minecraft Server, a.jar file which needs to generate files.我在 C# WinForms 中编写了一个 Minecraft Server Runner 代码,它可以让您运行一个 Minecraft Server,a.jar 文件需要生成文件。 The problem is that I launch this.jar file via the.exe application, and the files generate at the.exe application location.问题是我通过.exe应用程序启动this.jar文件,文件在.exe应用程序位置生成。

-- What I have tried: - 我尝试过的:

I tried moving the.exe application to the specific server file location, but the application needs a restart to register this change which I don't want to happen.我尝试将 .exe 应用程序移动到特定的服务器文件位置,但应用程序需要重新启动才能注册我不希望发生的此更改。

I also don't want the user being forced to put the.exe application to the Server folder and restart it.我也不希望用户被迫将 .exe 应用程序放入服务器文件夹并重新启动它。 Here is the code I use to launch the.jar file:这是我用来启动 .jar 文件的代码:

Process.Start("C:\user\documents\server\server.jar");

How can I fix this issue?我该如何解决这个问题?

The jar file can be executed by the java -jar filename.jar . jar 文件可以通过java -jar filename.jar执行。 So use the following Process.Start call to invoke the jar.因此,使用以下Process.Start调用来调用 jar。

Process.Start("java", "-jar C:\user\documents\server\server.jar", username, password, domain);

Hope this helps.希望这可以帮助。

PS: For this to work, either add the Java to your path or invoke with the java.exe's Path. PS:为此,请将 Java 添加到您的路径或使用 java.exe 的路径调用。

To fix this I executed the.jar file in the C# application via the CMD.为了解决这个问题,我通过 CMD 在 C# 应用程序中执行了 .jar 文件。 Here is the code I used instead:这是我使用的代码:

string path = @"C:\user\documents\server\"; //Path to your server.jar file.
var process = new System.Diagnostics.Process();
process.StartInfo.FileName = path + "server.jar"; //Name of the .jar file.
process.StartInfo.WorkingDirectory = path;
process.StartInfo.UseShellExecute = true;
process.Start();

All credits go to "Olivier Rogier" ( https://stackoverflow.com/users/12031933/olivier-rogier ) for helping me find this solution所有积分 go 到“Olivier Rogier”( https://stackoverflow.com/users/12031933/olivier-rogier )帮助我找到这个解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM