Reputation: 115
I wrote a very small function to start a Java application in C# NET, but I am getting the error "An object reference is required for the non-static field, method, or property 'MinecraftDaemon.Program.LaunchMinecraft()' C:\Users\Mike\Desktop\Minecraft\MinecraftDaemon\Program.cs". I have searched other threads that suffer from the same issue but I don't understand what it means or why I am getting it.
namespace MinecraftDaemon
{
class Program
{
public void LaunchMinecraft()
{
ProcessStartInfo processInfo = new ProcessStartInfo("java.exe", "-Xmx1024M -Xms1024M -jar minecraft_server.jar nogui");
processInfo.CreateNoWindow = true;
processInfo.UseShellExecute = false;
try
{
using (Process minecraftProcess = Process.Start(processInfo))
{
minecraftProcess.WaitForExit();
}
}
catch
{
// Log Error
}
}
static void Main(string[] args)
{
LaunchMinecraft();
}
}
}
Upvotes: 3
Views: 31187
Reputation: 11
static void Main(string[] args)
{
Program pg = new Program();
pg.LaunchMinecraft();
}
Try this.
Upvotes: 0
Reputation: 50215
LaunchMinecraft
is not a static method so you cannot access it in the static method Main
without calling it from the Program
object.
Two options
1. Make LaunchMinecraft
static
public void LaunchMinecraft()
{ ... }
2. Create a new Program
object in Main
and call it that way.
var program = new Program();
program.LaunchMinecraft();
Upvotes: 4
Reputation: 49724
I don't know much about C# but the Main()
method is static, while LaunchMinecraft()
isn't, that's the cause of this error.
Upvotes: 0
Reputation: 45445
You need to change it to:
public static void LaunchMinecraft()
That way, the static Main
method can access the static LaunchMinecraft
method.
Upvotes: 4
Reputation: 61437
You are trying to call an instance method (ie. a method that needs a specific object to operate on) from a static method (a method that works without a specific object). Make the LaunchMinecraft
method static as well.
Upvotes: 0