Java error - bad source file: file does not contain class x . Please remove or make sure it appears
Solution 1
Hi the problem here is that the JVM confuses the class file due to the ambiguous
class file name in both the directory (the JavaTest
as well as the com.test.helpers
directory).
when you do javac -d . A.java
the compiler makes a class file in the directory com.test.helpers
and now it confuses it with the sourcefile there in JavaTest
Deleting the Source file A.java
When you delete the source file A.java
from JavaTest
, JVM knows now that the class file in com.test....
is to be used, ambiguity goes away.
Changing the import statement from 'import com.test.helpers.*;' to 'import com.test.helpers.A' in the file, 'App.java'.
Here you are specifying which particular file to use in your class implementation that is you are telling the compiler to use the file A.java
from com.test...
and not from JavaTest
package
Now, the solution for this ambiguity to not ever be a problem for you, you must import the specific files with import statement i.e. import com.test.helpers.A;
or if you want to do import com.test.helpers.*;
then you must specifically use com.test.helpers.A
in place of A
everywhere in your current class implementation to tell the compiler not to confuse it with the source at JavaTest
I know it's a lot late for this particular answer, but I wanted to share my views for the upcoming readers, if it could help them in any way, it would be great. Thanks!
Solution 2
move the A.java under folder JavaTest to com/test/helpers. the error you are seeing is for the compiler complaining that A.java is in a folder that does not match its package declaration. Remember, you cannot access A from App without A being in a package.
from under src driectory run the following command to compile your classes
src> javac ./*.java ./com/test/helpers/*.java
then from under src folder
src>java App
that should run your program.
Solution 3
the file A.java should be in the path JavaTest\com\test\helpers\A.java
and don't forget to compile it like this: javac -d . com\test\helpers\A.java
Solution 4
I have the same problem finally,
I was solved.
//Use like this
import com.test.helpers.A;
If u have a more than class in com.test.helpers then u can use import com.test.helpers.*;
user3210872
Updated on February 01, 2022Comments
-
user3210872 over 2 years
Recently started studying Java for an exam.
While learning packages, tried this and got an error message. What I did was
//Creating class A (Within package the package: com.test.helpers) package com.test.helpers; public class A { public void sayHello(){ System.out.println("Hello World"); } }
//And then the class App utilising the class A import com.test.helpers.*; public class App{ public static void main(String args[]){ A a = new A(); a.sayHello(); } }
I had both of these files in a directory called 'JavaTest' (on Windows 7), and first compiled the A.java using the command
javac -d . A.java
And then, while attempting to compile App.java, I got the following error message:
App.java:5: error: cannot access A A a = new A(); ^ bad source file: .\A.java file does not contain class A Please remove or make sure it appears in the correct subdirectory of the source path. 1 error
However, the problem seems to resolve in two ways,
- Deleting the Source file A.java
- Changing the import statement from
import com.test.helpers.*;
toimport com.test.helpers.A
in the fileApp.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.
-
user3210872 almost 10 yearsThank you, that works perfectly. And do you have any idea why the error never appears when the import statement in the App.java changed from
code
import com.test.helpers.*;code
tocode
import com.test.helpers.A;code
? -
jambriz almost 10 yearshonestly i dont know, but my guess is it has something to do with the internals of the classloader. as the A.class ended up in the right path it had no trouble finding it, and it's somehow different with the wildcard *. but as i said, this is just a guess, if i find out the exact reason i'll gladly share ( btw, if it solved your problem dont forget to accept the answer ;) )
-
user3210872 almost 10 yearsThank you. The main thing I'm wondering is why the program worked with A.java source file in the wrong folder, but with import statement using the class name explicitly, instead of wildcard * ? Any explanation highly appreciated.
-
user3210872 almost 10 yearsThan you. However, the main reason for putting up this question here is to get an explanation as to why the program worked with A.java source file in the wrong folder, but with import statement using the class name explicitly, instead of wildcard * ?
-
Py-Coder over 7 yearsIf i solved your problem please accept it @user3210872 bro