2

How do I start a java application from a C# application?

I'm working on a minecraft launcher. Is there a general method to start any jar from a C# application?

payo
  • 4,501
  • 1
  • 24
  • 32
Matthewj
  • 1,932
  • 6
  • 26
  • 37
  • 2
    Possible duplicate of http://stackoverflow.com/questions/873809/how-to-execute-a-java-program-from-c and http://stackoverflow.com/questions/129989/how-to-call-java-code-from-c – walther May 03 '12 at 23:01

1 Answers1

3

You could start the jar file as a new process:

string appData = Environment.GetEnvironmentVariable("APPDATA");

Process.Start(@"java",
  @"-Xms512m -Xmx1024m -cp """ + appData + @"\.minecraft\bin\*"" -Djava.library.path=""" + appData + @"\.minecraft\bin\natives"" net.minecraft.client.Minecraft");

This assumes java is in your PATH (which happens by default when java is installed). If not, use the absolute path to java.

This is starting the game directly - you can use the minecraft launcher if the user told you where they put it. On windows, that's an exe, so you can start it directly.

Also, APPDATA is a window's variable. For linux, the path is in $HOME, or ~/. Also, the params aren't quite the same. Assuming your users will run your app with Mono for linux (or apple). I don't have Mono handy to test the options for you, but you can use

public static bool IsLinux
{
    get
    {
        int p = (int) Environment.OSVersion.Platform;
        return (p == 4) || (p == 6) || (p == 128);
    }
}

To help you select the correct start path. The above code was found here. I note that the property in the sample above is called IsLinux, yet the quote from Mono docs says 6 is for Mac OS X. This leads me to believe the property should actually be called IsUnix (as apple fanatics continue to preach such is the case though apple is as much unix as windows is dos). I don't have macware on hand to test this, I leave that to the student.

Community
  • 1
  • 1
payo
  • 4,501
  • 1
  • 24
  • 32
  • Wait, I thought it would be like string appData = Environment.GetEnvironmentVariable("%APPDATA%"); to go into the appdata folder – Matthewj May 04 '12 at 00:36
  • @Matthewj nope, the %'s are just used on the command line to trigger the var lookup, the var name is just appdata. – payo May 04 '12 at 07:36
  • Alright, so I tried out the code and it opens a java command promp and after a few seconds it closes but minecraft doesnt open. – Matthewj May 04 '12 at 21:54
  • @Matthewj it works for me. I tested before I posted. Linux or windows? We should chat about this out-of-comments and work it out for you. We can use SO chat (or irc, I'm always in minecrafthelp on esper). – payo May 04 '12 at 22:28