Running .jar file through c# on windows?

I wrote a program that downloads a jar file, then runs it. It works perfectly on linux but not on windows, does anyone know what the issue could be? (posting code in a sec)
48 Replies
Protogen Posting
Protogen PostingOP7d ago
using System.Diagnostics;
using System.Net;
using System.Runtime.InteropServices;
using Octokit;
using Microsoft.Win32;

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

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

Release latest = releases[0];

string lastDownloadedRelease = "";

try{
using (StreamReader reader = new StreamReader("LastVersion.txt"))
{
lastDownloadedRelease = reader.ReadLine();
}
}
catch(Exception e)
{

}

Console.WriteLine("Last Release Downloaded: "+lastDownloadedRelease);

Console.WriteLine("CurrentRelease: "+latest.TagName);

if(lastDownloadedRelease.Equals("") || !latest.TagName.Equals(lastDownloadedRelease))
{
using (StreamWriter outputFile = new StreamWriter("LastVersion.txt"))
{
outputFile.WriteLine(latest.TagName);
}

Console.WriteLine("Downloading Update!");

var webClient = new WebClient();

webClient.UseDefaultCredentials = true;

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

Console.WriteLine("Download Check Done... Launching");

if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
Console.WriteLine("Wow... A Linux User... So Hot...");

string result = ShellHelper.Bash("java -jar Marrow.jar");

if(result.Contains("command not found"))
{
Console.WriteLine("No Java Version Found!");

Console.WriteLine("Run This Command: sudo apt install openjdk-21-jdk");
}
}
else
{
var processInfo = new ProcessStartInfo(Environment.GetEnvironmentVariable("%JAVA_HOME%"), "-jar Marrow.jar")
{
CreateNoWindow = true,
UseShellExecute = false
};
Process proc;
if ((proc = Process.Start(processInfo)) == null)
{
throw new InvalidOperationException("??");
}

proc.WaitForExit();
int exitCode = proc.ExitCode;
proc.Close();
}
using System.Diagnostics;
using System.Net;
using System.Runtime.InteropServices;
using Octokit;
using Microsoft.Win32;

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

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

Release latest = releases[0];

string lastDownloadedRelease = "";

try{
using (StreamReader reader = new StreamReader("LastVersion.txt"))
{
lastDownloadedRelease = reader.ReadLine();
}
}
catch(Exception e)
{

}

Console.WriteLine("Last Release Downloaded: "+lastDownloadedRelease);

Console.WriteLine("CurrentRelease: "+latest.TagName);

if(lastDownloadedRelease.Equals("") || !latest.TagName.Equals(lastDownloadedRelease))
{
using (StreamWriter outputFile = new StreamWriter("LastVersion.txt"))
{
outputFile.WriteLine(latest.TagName);
}

Console.WriteLine("Downloading Update!");

var webClient = new WebClient();

webClient.UseDefaultCredentials = true;

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

Console.WriteLine("Download Check Done... Launching");

if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
Console.WriteLine("Wow... A Linux User... So Hot...");

string result = ShellHelper.Bash("java -jar Marrow.jar");

if(result.Contains("command not found"))
{
Console.WriteLine("No Java Version Found!");

Console.WriteLine("Run This Command: sudo apt install openjdk-21-jdk");
}
}
else
{
var processInfo = new ProcessStartInfo(Environment.GetEnvironmentVariable("%JAVA_HOME%"), "-jar Marrow.jar")
{
CreateNoWindow = true,
UseShellExecute = false
};
Process proc;
if ((proc = Process.Start(processInfo)) == null)
{
throw new InvalidOperationException("??");
}

proc.WaitForExit();
int exitCode = proc.ExitCode;
proc.Close();
}
here's the code for hte main thingy
using System;
using System.Diagnostics;

public static class ShellHelper
{
public static string Bash(this string cmd)
{
var escapedArgs = cmd.Replace("\"", "\\\"");

var process = new Process()
{
StartInfo = new ProcessStartInfo
{
FileName = "/bin/bash",
Arguments = $"-c \"{escapedArgs}\"",
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true,
}
};

process.Start();

string result = process.StandardError.ReadToEnd();

process.WaitForExit(-1);

return result;
}
public static string CMD(this string cmd)
{
var escapedArgs = cmd.Replace("\"", "\\\"");

var process = new Process()
{
StartInfo = new ProcessStartInfo
{
FileName = "c:\\windows\\system32\\cmd.exe",
Arguments = cmd,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true,
}
};

process.Start();

string result = process.StandardError.ReadToEnd();

process.WaitForExit(-1);

return result;
}
}
using System;
using System.Diagnostics;

public static class ShellHelper
{
public static string Bash(this string cmd)
{
var escapedArgs = cmd.Replace("\"", "\\\"");

var process = new Process()
{
StartInfo = new ProcessStartInfo
{
FileName = "/bin/bash",
Arguments = $"-c \"{escapedArgs}\"",
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true,
}
};

process.Start();

string result = process.StandardError.ReadToEnd();

process.WaitForExit(-1);

return result;
}
public static string CMD(this string cmd)
{
var escapedArgs = cmd.Replace("\"", "\\\"");

var process = new Process()
{
StartInfo = new ProcessStartInfo
{
FileName = "c:\\windows\\system32\\cmd.exe",
Arguments = cmd,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true,
}
};

process.Start();

string result = process.StandardError.ReadToEnd();

process.WaitForExit(-1);

return result;
}
}
here's the code for the shell helper thing
SleepWellPupper
GitHub
GitHub - ikvmnet/ikvm: A Java Virtual Machine and Bytecode-to-IL Co...
A Java Virtual Machine and Bytecode-to-IL Converter for .NET - ikvmnet/ikvm
SleepWellPupper
Try running the jar via IKVM, it probably does exactly what you're trying to do here.
Unknown User
Unknown User7d ago
Message Not Public
Sign In & Join Server To View
Protogen Posting
Protogen PostingOP7d ago
Oh lol I might as well just use IKVM and hope it doesn't break anything :3
SleepWellPupper
Can you explain in a couple of sentences the thing you're trying to do? Is all you want to do invoke the jar from a dotnet exe?
Unknown User
Unknown User7d ago
Message Not Public
Sign In & Join Server To View
Protogen Posting
Protogen PostingOP7d ago
basically yeah, tho idk what you mean by invoke in this context
SleepWellPupper
run is what I meant like execute So you don't need any sort of interop between the jar and your c# code. Then Process.Start might be the simpler option for you (in comparison to IKVM)
Protogen Posting
Protogen PostingOP7d ago
that's bascially all I wanna do yeah I'm not sure if process has a run function
SleepWellPupper
made a typo, sorry
Protogen Posting
Protogen PostingOP7d ago
doesn't show up in autocomplete I'm already using process.start
SleepWellPupper
Exactly. I'm suggesting you to keep doing that :)
Protogen Posting
Protogen PostingOP7d ago
oke anyways, since java_home isn't the actual jar, how would I find the jar from there (or if there even is a jar on the computer in the first place) weh
Keswiik
Keswiik7d ago
first off, java itself does not run from a jar. the java_home variable should point to the root folder of the primary jdk/jre installation, actual exe is bin/java.exe
Protogen Posting
Protogen PostingOP7d ago
ah so Environment.GetEnvironmentVariable("%JAVA_HOME%") + "/bin/java.exe" would work?
Keswiik
Keswiik7d ago
don't think you'll need the %s in your variable name
Protogen Posting
Protogen PostingOP7d ago
ok fair
Keswiik
Keswiik7d ago
would only need that in something like a batch script
Protogen Posting
Protogen PostingOP7d ago
I added that cuz I was desperate I'll tell u if it works My friend has to run it
Keswiik
Keswiik7d ago
do you not have java installed on your machine?
Protogen Posting
Protogen PostingOP7d ago
no I'm using linux
Keswiik
Keswiik7d ago
is your project not using .net core? modern c# is xplat although on linux you likely wouldn't need to specify any path for java, i would expect it to be on the system path after installation but i haven't installed java on linux outside of vms / docker containers in a long time
Protogen Posting
Protogen PostingOP7d ago
ok he was thrown this error on windows
No description
Protogen Posting
Protogen PostingOP7d ago
on linux it's way more straightforward than on windows
Protogen Posting
Protogen PostingOP7d ago
current code
Keswiik
Keswiik7d ago
it's not much more difficult on windows, just seems like you don't really know your way around the OS
Protogen Posting
Protogen PostingOP7d ago
I have not used windows in like 6 months and it's only on my school computer lol
Keswiik
Keswiik7d ago
¯\_(ツ)_/¯ can always spin up a windows VM for your own testing would make this process much easier than bouncing back and forth through an intermediarity with no ability to debug and by reading that error message the issue should be easy to spot
Protogen Posting
Protogen PostingOP7d ago
wait is java home null in this case
Keswiik
Keswiik7d ago
An error occurred trying to start process '/bin/java.exe' yes, there is no environment variable named java_home on their PC JAVA_HOME isn't part of the java installation nowadays iirc
Protogen Posting
Protogen PostingOP7d ago
is there an easier way to do this?
Keswiik
Keswiik7d ago
sure, open up a file picker dialog and make them locate the java folder and save that in some kind of config ¯\_(ツ)_/¯ or assume java will be on the system path and make users configure that
Protogen Posting
Protogen PostingOP7d ago
I'm trying to avoid the user having to pick files and stuff windows why can't you be like linux and put stuff in consistent spots
Keswiik
Keswiik7d ago
? the java install should put it on the system path afaik otherwise include a readme that tells people how to set up the JAVA_HOME variable or how to update the system path to include it wouldn't be hard to include fallback logic, attempt to read and validate JAVA_HOME and, if that fails, attempt to start java.exe which should work if it is on the system path and again, developing this would be much, much easier if you set up a windows VM
Protogen Posting
Protogen PostingOP7d ago
https://confluence.atlassian.com/doc/setting-the-java_home-variable-in-windows-8895.html would this be a good thing to link them to for setting up java home if the fallback doesn't work?
Keswiik
Keswiik7d ago
seems fine
Protogen Posting
Protogen PostingOP7d ago
IT WORKS YIPPEE anyways rq I forgot what the command to publish the app without the person using it having to download .net sdk
Keswiik
Keswiik7d ago
Create a single file for application deployment - .NET
Learn what single file application is and why you should consider using this application deployment model.
Unknown User
Unknown User7d ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX7d ago
If you have no further questions, please use /close to mark the forum thread as answered
Yuki [Art Director - CM Studios]
the lino that you sent me in private were of hacking or trolling? Anyway, that was very rude and disrespectful. .
Unknown User
Unknown User2d ago
Message Not Public
Sign In & Join Server To View
Yuki [Art Director - CM Studios]
Unfortunaly no, i spoke to him in private, invinting him to a project, then he said he had to make sure i was not a bot and that i should click a link. I clicked in it and it got to a site saying something like ´´they stole my computer, were going to share it`` really negative expirience
Unknown User
Unknown User2d ago
Message Not Public
Sign In & Join Server To View
Yuki [Art Director - CM Studios]
Weird is too much of a subjective trait
Unknown User
Unknown User2d ago
Message Not Public
Sign In & Join Server To View
Yuki [Art Director - CM Studios]
I think i may get into the form of an art director who got a bit off thanks to studying philosophy and so on dude? hahahaha, were are you guys taking that from?
Want results from more Discord servers?
Add your server