Executing a jar file through c#

I'm trying to make a C# launcher for a java program so it'll be easier to run it, but I'm running into issues (especially with getting it working on linux specifically). Has anyone done this before?
8 Replies
Buddy
Buddy4mo ago
C# launcher for a java program
AKA a launcher for Minecraft, correct?
Protogen Posting
Protogen PostingOP4mo ago
kinda like the mc launcher yeah Currently the code installs the latest release from github of the program, I just have to make it run it
Buddy
Buddy4mo ago
$details
MODiX
MODiX4mo ago
When you ask a question, make sure you include as much detail as possible. Such as code, the issue you are facing, what you expect the result to be, what .NET version you are using and what platform/environment (if any) are relevant to your question. Upload code here https://paste.mod.gg/, save, and copy the link into chat for others to see your shared code! (see $code for more information on how to paste your code)
Buddy
Buddy4mo ago
but I'm running into issues
This is lacking a lot of information such as what issues? Do you have a code to show?
Protogen Posting
Protogen PostingOP4mo ago
using System;
using System.Diagnostics;
using System.Net;
using System.Runtime.InteropServices;
using Octokit;

GitHubClient client = new GitHubClient(new ProductHeaderValue("protogenposting"));

IReadOnlyList<Release> releases = await client.Repository.Release.GetAll("protogenposting", "Marrow");

Release latest = releases[0];

var webClient = new WebClient();

webClient.UseDefaultCredentials = true;

webClient.DownloadFile("https://github.com/protogenposting/Marrow/releases/latest/download/Marrow.jar", "Marrow.jar");

if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
RunCommandWithBash("java Marrow.jar");
}
else
{
string strCmdText;
strCmdText= "java Marrow.jar";
System.Diagnostics.Process.Start("CMD.exe",strCmdText);
}

string RunCommandWithBash(string command)
{
var psi = new ProcessStartInfo();
psi.FileName = "/bin/bash";
psi.Arguments = command;
psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;
psi.CreateNoWindow = true;

using var process = Process.Start(psi);

process.WaitForExit();

var output = process.StandardOutput.ReadToEnd();

return output;
}
using System;
using System.Diagnostics;
using System.Net;
using System.Runtime.InteropServices;
using Octokit;

GitHubClient client = new GitHubClient(new ProductHeaderValue("protogenposting"));

IReadOnlyList<Release> releases = await client.Repository.Release.GetAll("protogenposting", "Marrow");

Release latest = releases[0];

var webClient = new WebClient();

webClient.UseDefaultCredentials = true;

webClient.DownloadFile("https://github.com/protogenposting/Marrow/releases/latest/download/Marrow.jar", "Marrow.jar");

if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
RunCommandWithBash("java Marrow.jar");
}
else
{
string strCmdText;
strCmdText= "java Marrow.jar";
System.Diagnostics.Process.Start("CMD.exe",strCmdText);
}

string RunCommandWithBash(string command)
{
var psi = new ProcessStartInfo();
psi.FileName = "/bin/bash";
psi.Arguments = command;
psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;
psi.CreateNoWindow = true;

using var process = Process.Start(psi);

process.WaitForExit();

var output = process.StandardOutput.ReadToEnd();

return output;
}
yeah here here's the error it's giving
/usr/bin/java: /usr/bin/java: cannot execute binary file
/usr/bin/java: /usr/bin/java: cannot execute binary file
it gives a different error when I run it in terminal which is this
Error: Could not find or load main class Marrow.jar
Caused by: java.lang.ClassNotFoundException: Marrow.jar
Error: Could not find or load main class Marrow.jar
Caused by: java.lang.ClassNotFoundException: Marrow.jar
I'm 99% sure this is because the jar is broken but fixing that isn't the most important thing I more just want them to both give the same error
Aaron
Aaron4mo ago
why are you trying to run it with bash?
Protogen Posting
Protogen PostingOP4mo ago
is that not what I'm supposed to do? Imma be honest I was just stealing code and hoping it'd work turns out the command needs to be java -jar Marrow.jar to work, though this doesn't fix my main issue found some code to fix it! just gonna close this now

Did you find this page helpful?