bcr
bcr

Reputation: 950

How to import classes from within another directory / package

I am working on a somewhat large project with many subdirectories. I am, however, coming across an issue of importing classes from within another directory. The directory structure is as so:

main.dir
   repository.dir
      Bill.java
   transaction.dir
      AutomaticBillPay.java

How can I import Bill into AutomaticBillPay?

I have tried may iterations of:

package main;
package main.repositorysys;

import main.repositorysys.Bill;
import repositorysys.Bill;
import Bill;

Sadly, the only line that compiles is the first: package main;. Any tips / direction will help!

Upvotes: 1

Views: 770

Answers (2)

Abdu Manas C A
Abdu Manas C A

Reputation: 1149

You can achieve it through this

    /*Declare your class package */
    package main.transactionsubsys;

    /*import the classes you want */
    import main.repositorysys.Bill;

    /*Write your class*/
    public class AutomaticBillPay {

    /*AutomaticBillPay code */

    }

Upvotes: 3

ACVM
ACVM

Reputation: 1527

Your AutomaticBillPay should look like this:

package main.transaction;

import main.repository.Bill;

public class AutomaticBillPay {
    // your class implementation here
}

Not sure where repositorysys came from?

package should be the full path to your encompassing directory

import should be the full path to the class you want to import

Upvotes: 1

Related Questions