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 utilizing 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.*;' 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
Thank you very much
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
I have the same problem finally,
I was solved.
If u have a more than class in com.test.helpers then u can use import com.test.helpers.*;
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
then from under src folder
that should run your program.
Hi the problem here is that the JVM confuses the class file due to the
ambiguous
class file name in both the directory (theJavaTest
as well as thecom.test.helpers
directory).when you do
javac -d . A.java
the compiler makes a class file in the directorycom.test.helpers
and now it confuses it with the sourcefile there inJavaTest
Deleting the Source file A.java
When you delete the source file
A.java
fromJavaTest
, JVM knows now that the class file incom.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
fromcom.test...
and not fromJavaTest
packageNow, 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 doimport com.test.helpers.*;
then you must specifically usecom.test.helpers.A
in place ofA
everywhere in your current class implementation to tell the compiler not to confuse it with the source atJavaTest
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!
every thing is in right place, java does not ask to put your A.java file, in the helpers folder
the whole reason why your code ran with the A.java file removed and not otherwise is: when your app.java (or the main java file which is importing other classes in it, -->) is in a default package then while importing the classes it gives priority to the same directory and not to the package directory(if there exist any file that has the same name as the class file -> and thats why it gives error bad source file A.java -> as it wanted A.class)
And because of that there is a rule in java : That never place a .java file (of the same name)in parallel to the packages
so to solve this problem you have to either remove the file, A.java or rename it (to any other name that does not present in the package) or you can use fully qualified import statement