I'm new to java and packages and need some help navigating them. Currently I have a functioning java program for a web application where all my .java and .class files sit in the myapp/WEB-INF/classes
directory on a linux server:
myapp/WEB-INF/classes/MyJavaClass1.class
myapp/WEB-INF/classes/MyJavaClass2.class
myapp/WEB-INF/classes/MyJavaClass3.class
None of these files include a line of code for package
so I assume the system default package is being used.
Then I added a mytools
directory, and created a new class in there.
myapp/WEB-INF/classes/mytools/MyToolClass1.class
But it seems like this new class cannot access the classes in the myapp/WEB-INF/classes
directory unless those java files include a package name. So my question is how to change the files to go from a system default package (e.g. no package specified) to a named package?
Should I append package mypackage;
to the first line of MyJavaClass1.java, MyJavaClass2.java, and MyJavaClass3.java, and append package mypackage.mytools;
to MyToolClass1.java?
Or, do I also need to create a directory mypackage
such that myapp/WEB-INF/classes/mypackage
and place the files MyJavaClass1.class etc. in there, before doing the above?
Then when I compile the .java files, do I compile each within their own directory (or do all files need to be compiled from top level directory)?
UPDATE 1
If I simply keep the default package (i.e. no package) for the java files in myapp/WEB-INF/classes
, and append:
package mytools;
import MyJavaClass1;
to MyToolClass1.java and try to compile MyToolClass1.java from the mytools
directory, I get the following compile errors:
MyToolClass1.java:21: '.' expected
import MyJavaClass1;
^
MyToolClass1.java:21: ';' expected
import MyJavaClass1;
^
2 errors