Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java import sub-sub directory

Tags:

java

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?

like image 787
Peter Hwang Avatar asked Dec 26 '22 03:12

Peter Hwang


1 Answers

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
like image 181
aioobe Avatar answered Jan 10 '23 11:01

aioobe