George Newton
George Newton

Reputation: 3303

importing in Java from a different directory inside a directory

I have a directory pj, which holds two directories, dirA and Tests. Tests has a file, Test.java (pj/Tests/Test.java), that imports from File.java (pj/dirA/File.java) in dirA. How do I specify such an import? I tried import dirA.* inside Test.java but that failed. dirA is a package.

pj and Tests are not packages (though I can make them be if necessary).

In File.java, the statement is as follows:

package dirA;
import list.*;

In Test.java, the statements before the class header are as follows

import pj.dirA.File;

When I do

javac Test.java 

from inside Tests, it fails.

Upvotes: 2

Views: 10892

Answers (1)

eis
eis

Reputation: 53563

Add this in your File.java:

package pj.dirA;

and this in your Test.java:

package pj.Tests;

import pj.dirA.File;

then do javac from the folder which contains your pj folder.

Note: I've explained how you can change your current setup to work. Package names that correspond to directories should really be all lowercase.

In java, you don't import directories from anywhere, you can only import packages. This is different if you compare it to C/C++, for instance.

Upvotes: 2

Related Questions