4

- Background information:

I have recently started learning the basics of Java programming language. To run my program at the command prompt, I downloaded the java development kit also known as JDK, and I set my windows 10 system path to:

C:\Program Files\Java\jdk-9.0.1\bin;C:\Program Files\Java\jre-9.0.1\bin

- Problem:

After writing a simple Hello World program with the following format:

    class test{

        public static void main(String[] args){

            System.out.println("Hello World!");
        }
    }

and running it on command prompt using

javac test.java

, and then writing

java test

the output says:

Error: Main method is not static in class test, please define the main method as:
public static void main(String[] args)

I have tried compiling my simple program on an online Java compiler and everything works fine.

- Edit:

As suggested using a Java decompiler. I used http://www.javadecompilers.com/result and the output was:

    import java.io.PrintStream;

      public class test { 
          public test() {} public void main(String[] paramArrayOfString) { 
               System.out.println("Hello World!"); 
          }
      }

- Question:

Where is the problem coming from? How can I fix it?

18
  • 1
    Have you tried specifying the classpath? Commented Dec 26, 2017 at 4:16
  • 1
    Have you tried opening the .class file in a Java decompiler? Does it still have a static before the main in that version? Commented Dec 26, 2017 at 4:21
  • 3
    Try defining your class as public. Commented Dec 26, 2017 at 4:21
  • 1
    Close your CMD and open it again. I think you didnt restart it since after the Path variable set Commented Dec 26, 2017 at 4:26
  • 2
    I suggest you to save your program and compile it once again. Commented Dec 26, 2017 at 4:37

1 Answer 1

0

Solution:

I was using "Sublime Text 3" when writing and saving my test.java program. @Silvio Mayolo suggested using a java decompiler to find out the problem, and I noticed that when saving my program in Sublime, the static gets deleted in test.java file for some reason. Then I did the following steps:

  1. I closed sublime text 3
  2. I opened my test.java file using notepad. I realized that static was missing after public, so it was public void main(String args){}.
  3. I added static in notepad, so it became public static void main(String[] args){}
  4. I saved the file again in notepad.
  5. I ran javac test.java in command prompt, and then java test, and I got my Hello World output.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.