Home > Net >  error:cannot access the String bad source file:./String.java file does not contain class String
error:cannot access the String bad source file:./String.java file does not contain class String

Time:02-12

newInstance.java:11: error: cannot access String public String toString(){ ^ bad source file: ./String.java file does not contain class String Please remove or make sure it appears in the correct subdirectory of the sourcepath. Note: newInstance.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details. 1 error

CodePudding user response:

I think the problem can be resolve in two ways,

  1. Deleting the Source file A.java
  2. Changing the import statement from import com.test.helpers.*; to import com.test.helpers.A in the file App.java.

I'd be highly grateful if you can explain what happens here. Or I might be making a goofy human mistake or a syntax error.

Here's the link to the source files

CodePudding user response:

You have two problem:

  • Don't name your classes the same as existing JDK classes. And especially not the same as classes in the java.lang package. If you have a file of your own named String.java then Remove or rename it and report back.

  • Assume you have two classes in same package

    FirstClass.java

    package com.test.files;
    public class FirstClass {
      public void _message(){
          System.out.println("Hello World");
      }
    }
    

    SecondClass.java

    import com.test.files.*;
    
    public class App{
       public static void main(String args[]){
          FirstClass  firstClass  = new FirstClass();
          firstClass._message();
       }
    }
    

    After declaring both class in same package you are going to compile your project using javac -d . FirstClass.java. Now compiler is confused because both file in same source package and our import statment in SecondClass.java is import com.test.files.* so compiler load FirstClass.java file two time, compiler create ambiguity. For get rid for this problem you have to change import statment in SecondClass.java from com.test.files.* to com.test.files.FirstClass or delete file FirstClass.java file and put it in another package.

  • Related