Java: Compiling and running multiple packages from the command-line
Solution 1
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.
Solution 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
Comments
-
urbaindepuce almost 2 years
I created multiple packages and want to compile and run them. I fiddled around with
javac
andjava
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.