2

I created multiple packages and want to compile and run them. I fiddled around with javac and java and learned about how packages should be named and how a project should be structured. I hope I got all right. But I fail at compilation and running the stuff. I know I could use an IDE for this, but I want to try it with the command-line tools just for curiousity. Here is how my project is organized:

Project
  + src
    + net
      + chris
        + dojo
            - Program.java
          + datastructures
            - Queue.java
            - LinkedList.java
          + sorting
            - MergeSort.java
  + bin
    + net
      + chris
        + dojo
            - Program.class (should be here but missing because compilation fails)
          + datastructures
            - Queue.class
            - LinkedList.class
          + sorting
            - MergeSort.class

Compilation for the classes in the "datastructures" and "sorting" packages is working fine. Here are the commands I used. The folder structure in the "bin" folder is automatically created by the compiler.

javac -d bin src\net\chris\dojo\datastructures\*.java
javac -d bin src\net\chris\dojo\sorting\*.java

The problem is when I try to compile "Program.java" (thats the test class I run from the command-line) the compiler is throwing errors, because it cannot find the packages "net.chris.dojo.datastructures" and "net.chris.dojo.sorting". Here is the compilation command:

javac -d bin src\net\chris\dojo\Program.java

This is the output I get:

src\net\chris\dojo\Program.java:3: error: cannot find symbol
import net.chris.dojo.datastructures;
                     ^
symbol:   class datastructures
location: package net.chris.dojo
src\net\chris\dojo\Program.java:4: error: cannot find symbol
import net.chris.dojo.sorting;
                     ^
symbol:   class sorting
location: package net.chris.dojo
src\net\chris\dojo\Program.java:11: error: cannot find symbol
            MergeSort.sort(values);
            ^
symbol:   variable MergeSort
location: class Program
src\net\chris\dojo\Program.java:12: error: cannot find symbol
            Queue queue = new Queue();
            ^
symbol:   class Queue
location: class Program
src\net\chris\dojo\Program.java:12: error: cannot find symbol
            Queue queue = new Queue();
                              ^
symbol:   class Queue
location: class Program
src\net\chris\dojo\Program.java:13: error: cannot find symbol
            LinkedList list = new LinkedList();
            ^
symbol:   class LinkedList
location: class Program
src\net\chris\dojo\Program.java:13: error: cannot find symbol
            LinkedList list = new LinkedList();
                                  ^
symbol:   class LinkedList
location: class Program
7 errors

Thats the code of my class files:

Queue.java

package net.chris.dojo.datastructures;

public class Queue {
    ...
}

LinkedList.java

package net.chris.dojo.datastructures;

public class LinkedList {
    ...
}

MergeSort.java

package net.chris.dojo.sorting;

public class MergeSort {
    ...
}

Program.java

package net.chris.dojo;

import net.chris.dojo.datastructures;
import net.chris.dojo.sorting;

public class Program {

    public static void main(String[] args) {
        int[] values = { 9, 4, 6, 2, 0, 3, 8, 1, 7, 5 };
        MergeSort.sort(values);
        Queue queue = new Queue();
        LinkedList list = new LinkedList();
    }

}

I would run it with this command:

java -cp bin net.chris.dojo.Program

I execute all commands in the root folder of the project. Thanks for your help.

urbaindepuce
  • 503
  • 1
  • 5
  • 17
  • Take a look at ANT (http://ant.apache.org/). Command line is good, but handling classpath can be a bit of a pain. Btw change your imports and specify a class path on javac command line too. – BigMike Sep 20 '13 at 13:07
  • I mean change javac -d bin src\net\chris\dojo\Program.java into javac -d bin -cp bin src\net\chris\dojo\Program.java after you've compiled your packages. – BigMike Sep 20 '13 at 13:11
  • Thanks that worked! So everytime when I want to compile a class which is using classes from other packages I should include the `-cp` option to tell `javac` where those packages can be found? – urbaindepuce Sep 20 '13 at 17:09
  • @BigMike Can you post your comment as an answer so I can mark this question as answered? – urbaindepuce Sep 20 '13 at 17:16
  • Don't worry. As long as you solved your problem is just fine. But really take a look at ANT in order to script builds, will save you from lots of headaches later. – BigMike Sep 23 '13 at 07:29

2 Answers2

5

The solution was to include the classpath when compiling. That way it can find the packages it depends on.

javac -d bin -cp bin src\net\chris\dojo\Program.java

Thanks @BigMike for the solution.

urbaindepuce
  • 503
  • 1
  • 5
  • 17
2

Try change this in your Program class

import net.chris.dojo.datastructures;
import net.chris.dojo.sorting;

to

import net.chris.dojo.datastructures.*;
import net.chris.dojo.sorting.*;

And when you compile your Program.java use following command

javac -d bin src\net\chris\dojo\Program.java -classpath bin
Alexey Odintsov
  • 1,705
  • 11
  • 13
  • Did not change anything. I still get those 7 errors. I am coming from a C# background and thought this is like importing namespaces. If so, why the asterisk? – urbaindepuce Sep 20 '13 at 12:22
  • @urbaindepuce You can use .* when you want to import all classes and interfaces from specified package. You can import specific class like `import net.chris.dojo.sorting.MergeSort`, but you can't import package itself like you do in your code. [See docs](http://docs.oracle.com/javase/tutorial/java/package/usepkgs.html) – Alexey Odintsov Sep 20 '13 at 12:44