This question already has answers here:
File name and class name different in java
(1 answer)
Can I compile a java file with a different name than the class?
(21 answers)
Closed 6 months ago.
I usually do my homework on a Java file and submit the file to complete my task, so I separate each file and contain them all in one folder. Sometimes there are two works that have the same class name, but different work. If I let two files have the same class name, they won't run properly. (I'm using vscode)
For example:
File1.java
public class File1{
public static void main(String[] args) {
...
}
}
class Fraction {...}
File2.java
public class File2{
public static void main(String[] args) {
...
}
}
class Fraction {...} //this is not the same with File1.java
Can I do anything keep two class names the same in two different files in one folder?
Do not define the same class name (whether the same script or not) in the same folder. This throws an error many times.
Largely all filenames are Operating System rules dependent.
Java source code files must be named by the main class name of the code in the file. After compiling, a file of the same name but different extension, extension of .class will be there. Dependent how the compiler command line flags are set it will simply by default overwrite any old version .class file.
type javac -h on the shell or prompt for compile command flags info.
I was trying to compile a few files in a package in java. The package name is library. Please have a look at the following details.
This is my Directory Structure:
javalearning
---library
------ParentClass.java
------ChildClass.java
I tried to compile in the following way:
current directory: javalearning
javac library/ParentClass.java //this compilation works fine
javac library/ChildClass.java //error over here
The following is the ParentClass.java:
package library;
class Parentclass{
...
}
The following is the ChildClass.java:
package library;
class ChildClass extends ParentClass{
...
}
The error is as follows:
cannot access ParentClass
bad class file: .\library\ParentClass.class
Please remove or make sure it appears in the correct sub directory of the classpath
You've got a casing issue:
class Parentclass
That's not the same as the filename ParentClass.class, nor is it the same as the class you're trying to use in ChildClass: class ChildClass extends ParentClass.
Java classnames are case-sensitive, but Windows filenames aren't. If the class had been public, the compiler would have validated that the names matched - but for non-public classes, there's no requirement for that.
The fact that you've ended up with ParentClass.class suggests that at some point it was declared as ParentClass, but then you changed the declared name and when recompiling, Windows just overwrote the content of the current file rather than effectively creating Parentclass.class.
Make sure your declared class name exactly matches the filename. You may well want to delete all your class files before recompiling, just to get out of a confusing state.
I have a package called "test" and in that have a public class that contains the main method in a file called ABC.java.
package test;
public class ABC{
public static void main(String[] args) {
new T1();
}
}
In that same package "test" I have two default classes T1 and T2 in a file called T.java
package test;
class T1 {}
class T2 {}
when i try to compile it it says cannot find symbol new T1(). When I put T1 in a separate file T1.java then it compiles fine. Why java is unable to find package private class in the same package.
javac will automatically compile all the linked file used in the file you are compiling if there .class files are not found. Like in your case ABC.java. But one thing to notice is javac will not search for all the files with .java extension to be compiled. But it will look for the file name with the same name as the class. Like in your case T1.
So if you will compile T.java and then compile ABC.java it will run as expected. But if you compile ABC.java and not T.java compiler will not find T1.class then it will look for T1.java, but it will not found it too, which will give you an error. On the other hand if you will rename T.java to T1.java it will work as expected.
In Java, When a class file name(T1.java) is same as class's name(T1) without public keyword, this class is public under this package(test)'s class.
If i compile any java program, I got this error
b1.java:3: cannot access String
bad class file: .\String.java
file does not contain class String
Please remove or make sure it appears in the correct subdirectory of the classpath.
public static void main(String ar[])throws IOException{
As visible from your compilation step in the terminal,
String is already a Java-library class available in JDK.
Hence,you can't give such name to your program/class.
Hence,you receive such error.
Try naming it something else like public class MyString and rename your source code as MyString.java---recompile it and then you won't receive such errors.
String is a datatype defined within the Java-Library. You cannot name a variable as String or any other name of datatypes, in that way you cannot name it for a class.
String is in-built Class in Java. Hence you can not give the name for Class as String. you can check it by - just give the name for Class as Class string and save it as string.java . now it will compile and run properly. but according to naming convention first letter of Class Name must be in UpperCase , so you can name it as MyString.java
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) {
// ...
}
}
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 {
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
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.
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
You named your file as Main.java. name your file as WeatherArray.java and compile.
your file is named Main.java where it should be
WeatherArray.java
Yes! When you face these type of problem then try to following points
Check Your .java file name and class name.
If Class name and public class name are not the same then RENAME class name.
I my case, I was using syncthing. It created a duplicate that I was not aware of and my compilation was failing.
To avoid this error you should follow the following steps:
1) You should make a new java class
2) Name that class
3) And a new java class is made
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.
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.
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.
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
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
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.
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
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!!
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);
}
}
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.