Error: class X is public should be declared in a file named X.java

Java

Java Problem Overview


I am trying to write a program, but I'm getting this compiler error:

Main.java:1: error: class WeatherArray is public, should be declared in a file named WeatherArray.java
public class WeatherArray {
       ^
1 error

I have checked my file names, and my public class is the same as my .java file.

How can I fix this?

Here is my code:

public class WeatherArray {
	public static void main(String[] args) {
        // ...
	}
}

Java Solutions


Solution 1 - Java

Name of public class must match the name of .java file in which it is placed (like public class Foo{} must be placed in Foo.java file). So either:

  • rename your file from Main.java to WeatherArray.java
  • rename the class from public class WeatherArray { to public class Main {

Solution 2 - Java

The name of the public class within a file has to be the same as the name of that file.

So if your file declares class WeatherArray, it needs to be named WeatherArray.java

Solution 3 - Java

This happens when you have 1 name for the Java class on hard disk and another name of Java class in the code!!

For example, I renamed my MainActivity class to MainnActivity only (!) in the code. I got this error immediately.

There is also a visual indicator in the Project tab of Android Studio - a class inside a class, like you have nested classed, but with an error indicator.

The solution is to simply rename class name in the Project tab (SHIFT + F6) to match the name in the Java code.

Solution 4 - Java

I had the same problem but solved it when I realized that I didn't compile it with the correct casing. You may have been doing

> javac Weatherarray.java

when it should have been

> javac WeatherArray.java

Solution 5 - Java

You named your file as Main.java. name your file as WeatherArray.java and compile.

Solution 6 - Java

your file is named Main.java where it should be

> WeatherArray.java

Solution 7 - Java

I my case, I was using syncthing. It created a duplicate that I was not aware of and my compilation was failing.

Solution 8 - Java

To avoid this error you should follow the following steps:

1) You should make a new java class

You should make a new java class.

2) Name that class

Name that class

3) And a new java class is made

And a new java class is made

Solution 9 - Java

I encountered the same error once. It was really funny. I had created a backup of the .java file with different filename but the same class name. And kept on trying to build it till I checked all the files in my folder.

Solution 10 - Java

In my case (using IntelliJ) I copy and pasted and renamed the workspace, and I am still using the old path to compile the new project.

In this case this particular error will happen too, if you have the same error you can check if you have done the similar things.

Solution 11 - Java

The terminal is not case sensitive when writing "Javac [x].java", so make sure what you write in the terminal matches the filename and class name.

My class name and file name were both "MainClass", but I was compiling using "Mainclass". Notice I forgot to make the "c" capital.

Solution 12 - Java

From Ubuntu command line:

//WeatherArray.java
public class WeatherArray {
  public static void main(String[] args) {
    System.out.println("....Hello World");
}}

> ls

WeatherArray.java > javac WeatherArray.java > > ls

WeatherArray.java WeatherArray.class

> java WeatherArray

....Hello World

Of course if you name your java file with different name than WeatherArray, you need to take out public and it would be:

// Sunny.java
class WeatherArray {
   public static void main(String[] args) {
      System.out.println("....Hello World"); }}
// javac Sunny.java; java WeatherArray

Solution 13 - Java

If you make WeatherArray class from public to default.

public class WeatherArray =====> class WeatherArray

then you do not get any error and you can easily compile your code by just writing

==> javac any_name_you_assign_to_file.java

Now a WeatherArray.class will generate.

To run your code you have to use class name

==> java WeatherArray

Solution 14 - Java

Compile WeatherArray.java instead of Main.java

This error comes if you have not saved your source code with the same name of your public class name.

Solution 15 - Java

If your class name is the same as the filename then check that it does not contain any zero width character

I accidentally copied a class name with invisible whitespace which caused this exception

Eclipse was able to build the file and Gradle was not

This can be very confusing

Solution 16 - Java

The answer is quite simple. It lies in your admin rights. before compiling your java code you need to open the command prompt with run as administrator. then compile your code. no need to change anything in your code. the name of the class need to be the same as the name of the java file.. that's it!!

Solution 17 - Java

when you named your file WeatherArray.java,maybe you have another file on hard disk ,so you can rename WeatherArray.java as ReWeatherArray.java, then rename ReWeatherArray.java as WeatherArray.java. it will be ok.

Solution 18 - Java

error example:

public class MaainActivity extends Activity {

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Get the view from activity_main.xml
    setContentView(R.layout.activity_main);

  }
}

correct example:just make sure that you written correct name of activity that is"main activity"

public class MainActivity extends Activity {


  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Get the view from activity_main.xml
    setContentView(R.layout.activity_main);

  }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChris FrankView Question on Stackoverflow
Solution 1 - JavajlordoView Answer on Stackoverflow
Solution 2 - JavaBostonJohnView Answer on Stackoverflow
Solution 3 - JavasandaloneView Answer on Stackoverflow
Solution 4 - JavaWarren FeeneyView Answer on Stackoverflow
Solution 5 - JavaPermGenErrorView Answer on Stackoverflow
Solution 6 - JavaAlexWienView Answer on Stackoverflow
Solution 7 - Javaabc123View Answer on Stackoverflow
Solution 8 - JavaRahat BatoolView Answer on Stackoverflow
Solution 9 - JavaYash P ShahView Answer on Stackoverflow
Solution 10 - JavaNg Sek LongView Answer on Stackoverflow
Solution 11 - JavaDylanView Answer on Stackoverflow
Solution 12 - JavaNotTooTechyView Answer on Stackoverflow
Solution 13 - JavaSachin SinghView Answer on Stackoverflow
Solution 14 - JavaiamfnizamiView Answer on Stackoverflow
Solution 15 - Javaanswer42View Answer on Stackoverflow
Solution 16 - JavaRohan KumarView Answer on Stackoverflow
Solution 17 - JavaSangooView Answer on Stackoverflow
Solution 18 - Javadhiraja gunjalView Answer on Stackoverflow