I am trying to create a package of library functions for a main Java program but I am have some issues.
I don't know much about about Java packages and I am going through some documentary online.
I have created my directory as such
./Program/Program.java
./Program/TestFunc.java
./Program/classes/library/
The contents of TestFunc.java are
package library;
public class TestFunc {
public void message01() {
System.out.println("called message01");
}
public void message02() {
System.out.println("called message02");
}
}
I compiled it as I read in the documentation
javac -d ./Program/classes TestFunc.java
Which gives me
./Program/classes/library/TestFunc.class
Then I try to call it in Program.java
import library.*;
public class Program {
public static void main(String[] args) {
System.out.println("Starting Script");
}
}
When I try to compile using
javac -d ./Program/classes Program.java
I get the error
package library does not exist
Why is this?
You've used -d
which says where to put the output, but you haven't told it that the same directory should also be used for input on the classpath. Use the -cp
option for that:
javac -d classes -cp classes Program.java
(It's not clear whether you're trying to do this from inside the Program
directory, or above it - your source filename appears to be inside the Program
directory, but you're specifying the output directory as if you were in the directory above...)
See more on this question at Stackoverflow