John
John

Reputation: 17

If i compile any java program, i got this error

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{

Upvotes: 0

Views: 72

Answers (3)

Wild Eagle
Wild Eagle

Reputation: 41

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

Upvotes: 0

user562
user562

Reputation: 88

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.

Upvotes: 0

Am_I_Helpful
Am_I_Helpful

Reputation: 19158

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.

Upvotes: 3

Related Questions