An object reference is required for a non-static field, method, or property

I wrote a very small function to run a Java application in C # NET, but I get the error "An object reference is required for a non-static field, method or property" MinecraftDaemon.Program.LaunchMinecraft () 'C: \ Users \ Mike \ Desktop \ Minecraft \ MinecraftDaemon \ Program.cs ". I was looking for other topics that suffer from the same problem, but I don’t understand what this means or why I get 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();
        }
    }
}
+3
source share
5 answers

You need to change it to:

public static void LaunchMinecraft()

In this way, the static method Maincan access the static method LaunchMinecraft.

+4
source

LaunchMinecraft , Main, Program.


 1. LaunchMinecraft static

public void LaunchMinecraft() 
{ ... }  

2. Program Main .

var program = new Program();
program.LaunchMinecraft();
+4

You are trying to call an instance method (i.e. a method that requires a specific object to work) from a static method (a method that works without a specific object). Make the method LaunchMinecraftstatic too .

0
source

I don't know much about C #, but the method is Main()static and LaunchMinecraft() not the cause of this error.

0
source
     static void Main(string[] args)
            {
                Program pg = new Program();
                pg.LaunchMinecraft();

            }

Try it.

0
source

Source: https://habr.com/ru/post/1785984/


All Articles