Question

Hello I'm new to Java programming and well this is my following code...

public class Main {

  public static void main(String[] args) {

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

  }

}

but when I try to compile I get the following error...

test.java:1: class Main is public, should be declared in a file named Main.java

public class Main {

       ^

1 error

I acuqire the following code from this website http://www.learnjavaonline.org/

I'm using the Linux Ubuntu OpenJDK Development Kit (JDK)

I believe this is how you compile.

cristian@ubuntu:~/Java$ javac test.java

I'm new to Java so any tips or guides will be appreciated thanks.

Was it helpful?

Solution

Name your file the same name as the class. If the public class is Main, then your file must be named Main.java, not test.java.

OTHER TIPS

Either that or change the name of the class to test (notice the lower case -- class name must match file name).

The name of your file and the name of your class must be the same. Either rename your file to "Main.class", or change your class to public class test

This should work:

public class Test {

  public static void main(String[] args) {

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

  }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top