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?

Agent 0
  • 361
  • 1
  • 5
  • 17
  • 1
    Have you tried specifying the classpath? – mustaccio Dec 26 '17 at 04:16
  • I have set my system path to C:\Program Files\Java\jdk-9.0.1\bin;C:\Program Files\Java\jre-9.0.1\bin. Is this the path you are talking about? – Agent 0 Dec 26 '17 at 04:19
  • 1
    Have you tried opening the `.class` file in a Java decompiler? Does it still have a `static` before the `main` in that version? – Silvio Mayolo Dec 26 '17 at 04:21
  • 3
    Try defining your class as public. – Santosh Dec 26 '17 at 04:21
  • I don't have Java decompiler, I was just using Sublime Text and command prompt. Opening the test.class in sublime gave me a bunch of numbers and letters (not very useful). – Agent 0 Dec 26 '17 at 04:23
  • I have tried public class test{} and it didn't work, @Santosh. – Agent 0 Dec 26 '17 at 04:24
  • 1
    Close your CMD and open it again. I think you didnt restart it since after the Path variable set – Vishnu T S Dec 26 '17 at 04:26
  • I tried it again, but didn't solve it @vts. – Agent 0 Dec 26 '17 at 04:30
  • I have tested something with java decompiler, and I get an output, can you check my edit section @SilvioMayolo? – Agent 0 Dec 26 '17 at 04:31
  • I have tested this issue it has been occur due to `Main`. You must use `main`. All small letters. – Amit Garg Dec 26 '17 at 04:32
  • I am using main, public static void main(String[] args), can you tell me which part you are talking about @Amit Garg? – Agent 0 Dec 26 '17 at 04:34
  • 1
    Hm... so your compiler is actually stripping away the `static` for some reason. I have to say that I'm a bit baffled. Hopefully someone else has something to try from here. – Silvio Mayolo Dec 26 '17 at 04:36
  • 2
    I suggest you to save your program and compile it once again. – Vishnu T S Dec 26 '17 at 04:37
  • Exactly @Silvio Mayolo, as you suggested Java decompiler gave us the clue :), I will google about it more maybe I can find something. – Agent 0 Dec 26 '17 at 04:38
  • I solved it! For some reason when I save in Sublime text with public static void main(String[] args), the "static" gets deleted. I decide to open test.java program using notepad and then I added static again and saved it. I run it and boom, "Hello World" :D. Good suggestion @vts. – Agent 0 Dec 26 '17 at 04:41
  • Excellent! By all means, post that as an answer then. You might not be the only person whose Sublime editor has a strange setting turned on. – Silvio Mayolo Dec 26 '17 at 04:42
  • Yes, I will post the solution soon, I think I will change my editor to visual studio code, I heard some people had weird problems with Sublime. – Agent 0 Dec 26 '17 at 04:44
  • Thanks everyone for the help and suggestions, it was helpful. – Agent 0 Dec 26 '17 at 04:44

1 Answers1

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.
Agent 0
  • 361
  • 1
  • 5
  • 17