Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Java Error in Running a Java Program

aartiyadav

New Coder
Hello All, I am in learning face to java programming and 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

After writing a simple Hello World program with the following format:
Java:
 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.

As suggested, I am using online compiler this https://www.interviewbit.com/online-java-compiler/ and the output was:

Java:
import java.io.PrintStream;

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

Where is the problem coming from? How can I fix it? Any suggestions?
 
Hello All, I am in learning face to java programming and 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

After writing a simple Hello World program with the following format:
Java:
 class test{

        public static void main(String[] args){

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

and running it on command prompt using


, and then writing


the output says:


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

As suggested, I am using online compiler this https://www.interviewbit.com/online-java-compiler/ and the output was:

Java:
import java.io.PrintStream;

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

Where is the problem coming from? How can I fix it? Any suggestions?
Hi there... all you are missing is the keyword `static` in the main function signature

public static void main (String args[]){}
 
all you are missing is the keyword `static` in the main function signature
Yes, the error message is quite specific about that. However the code in the original post is absolutely correct and worked fine for me. Maybe the OP edited their comment to add the missing keyword, without acknowledging you provided the solution ? If not, something is weird here. Could be the CLASSPATH is set to another folder of jarfile that has another test.class compiled from wrong code.
 
Hello All, I am in learning face to java programming and 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

After writing a simple Hello World program with the following format:
Java:
 class test{

        public static void main(String[] args){

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

and running it on command prompt using


, and then writing


the output says:


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

As suggested, I am using online compiler this Online Java Compiler and the output was:

Java:
import java.io.PrintStream;

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

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

First, ensure that the test.java file is located in the same directory as the commands for compilation and execution. Ensure the JDK is installed correctly on your Windows 10 computer, and double-check your Java installation if you still have problems.
I hope it will work.
Thanks
 
ensure that the test.java file is located in the same directory as the commands for compilation and execution
That's a convenient beginner hack to avoid setting up paths etc properly, but it's a really bad idea. The last place you should put your files is in someone else's installation folder. What happens when you update the JDK?
Moreover the Java compiler has built-in requirements about how the source files fit into directories that follow the code's package structure, so your advice will only work for trivial programs with only the unnamed package.
(... and did you notice the date of the original question? ...)
 
Back
Top Bottom