Search code examples
javapackagejavac

Compiling java package


My project contains three source files that I have created and one testing file (Homework1.java). The directory structure looks like this:

ija/ija2016/homework1/HomeWork1.java
ija/ija2016/homework1/cardpack/Card.java
ija/ija2016/homework1/cardpack/CardDeck.java
ija/ija2016/homework1/cardpack/CardStack.java

The HomeWork1.java file contains the main method, some tests and also imports the other three files:

import ija.ija2016.homework1.cardpack.Card;
import ija.ija2016.homework1.cardpack.CardDeck;
import ija.ija2016.homework1.cardpack.CardStack; 

Now I am able to compile the Card.java, CardStack.java and CardStack.java, but I am only able to do so directly from the /cardpack/ directory. When I try to compile them from anywhere else, then the CardDeck and CardStack classes don't recognize the Card symbol.

My question is, how do I compile the project as a whole? Should the three source files that I have created have import package in the header (the CardStack and CardDeck use the Card class)?


Solution

  • in your root folder, try to run:

    $javac -cp . ija/ija2016/homework1/HomeWork1.java

    then you can run your program

    $java -cp . ija/ija2016/homework1/HomeWork1

    -cp/-classpath: defines your classpath.