Reputation: 991
My directory structure looks like this:
folder
└───subfolder
└───subsubfolder
I hava a Main.java in folder
and Main.java
uses class inside subsubfolder
.
Here is how I did:
import subfolder.*;
import subfolder.subsubfolder.*;
However, I got the message following when I execute javac Main.java
$ javac -g Main.java
Main.java:23: error: cannot access Node
Node root = new Node();
^
bad class file: ./subfolder/subsubfolder/Node.class
class file contains wrong class: subsubfolder.Node
Please remove or make sure it appears in the correct subdirectory of the classpath.
1 error
Is my way of importing class file wrong?
Upvotes: 5
Views: 9549
Reputation: 181824
Your Node class declares that it belongs to package subsubfolder
, but it should belong to package subfolder.subsubfolder
. Alternatively, you could move directory subfolder/subsubfolder
to be a sibling of directory subfolder
.
Upvotes: 1
Reputation: 421310
It says
package subfolder
The package declaration of Node
should say
package subfolder.subsubfolder;
Providing an example for clarity:
folder/
Your source root (typically called 'src')
folder/Main.java
class Main { ... } (no package declaration)
folder/subfolder
folder/subfolder/subsubfolder/Node.java
package subfolder.subsubfolder;
public class Node { ... }
If your Main
indeed lives in a package (i.e. if your situation is something like src/folder/Main.java
) then you should not do
cd src/folder
javac Main.java
you should do
cd src
javac folder/Main.java
Upvotes: 9