Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing parent folder packages from inside a package

Tags:

java

package

If i have a class inside a package say mypackage.myclass and want to access a package's class that is in the same folder as the package's root (not inside the package) say anothepackage.anotherclass how can I actually do so?

Example presented in code:

// directories are preceded by a DIR, files are preceded by a CLASS
// any level deeper is preceded by a [Tab]
DIR mypackage
    CLASS myclass
    DIR myinnerpackage
        CLASS myinnerclass
DIR anotherpackage
    CLASS anotheclass
CLASS yetanotherclass
DIR org
    DIR apache
        DIR commons
            DIR exec
                CLASS DefaultExecutor

Inside the file myclass:

package mypackage;

public class myclass{
    // simplest example  code I can come up with
    org.apache.commons.exec.DefaultExecutor exec = new org.apache.commons.exec.DefaultExecutor();
}

Why does this piece of code not work for me?

Generally how can myclass access anotherclass or yetanotherclass and how can myinnerclass access them as well? Try to refrain for imports as I want to use certain members of each class only (some variables have same names and will get things messed)! Thanks in advance!

like image 396
Angelos Chalaris Avatar asked Jan 24 '26 21:01

Angelos Chalaris


1 Answers

Use relative path:

String dir = "../anotherpackage/anotheclass";
              ^
              |
              -- (go one level up)
like image 88
Eng.Fouad Avatar answered Jan 27 '26 10:01

Eng.Fouad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!