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,
- Deleting the Source file A.java
- 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 namedString.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 inSecondClass.java
isimport com.test.files.*
so compiler loadFirstClass.java
file two time, compiler create ambiguity. For get rid for this problem you have to change import statment inSecondClass.java
fromcom.test.files.*
tocom.test.files.FirstClass
or delete fileFirstClass.java
file and put it in another package.