1

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
ggkmath
  • 4,188
  • 23
  • 72
  • 129
  • You cannot access classes in default package from a named package. Please see this link for similar discussion [How to access java-classes in the default-package?](http://stackoverflow.com/questions/283816/how-to-access-java-classes-in-the-default-package) – sperumal Jun 17 '12 at 17:20
  • Thanks sperumal, The most help that link gives is "So to access a default package class from within a packaged class requires moving the default package class into a package of its own." Which is where I'm stuck (that's where my questions begin above). For example, does including a `package mypackage;` at the top of MyJavaClass1.java qualify as moving the default package class into a package of its own, or do I need to create a `mypackage` directory and move MyJavaClass1.java and .class there? – ggkmath Jun 17 '12 at 17:23

1 Answers1

2

In java packages are defined at the top of the class as you described. Your code is working only because all code is at the root, i.e., no package. I recommend that you move all your classes into a package... move them all into the mytools dir and add package mytools; to each.

For a quick reference on package best practices, read through this package tutorial. You'll probably want to package your classes by related function, meaning some may go into mytools directory and some my go into mytools/parse (package mytools.parse;)

Daniel Semmens
  • 461
  • 2
  • 4
  • Thanks Daniel. That's the approach I started with. See UPDATE1 above. But it seems to give compile errors as shown. Perhaps for these reasons: http://stackoverflow.com/questions/2030148/whats-the-syntax-to-import-a-class-in-a-default-package-in-java?rq=1 – ggkmath Jun 17 '12 at 17:14
  • OK, then it's not as simple as just including a `package mypackage;` at the top of the file? That is, I actually need to create a `mypackage` directory and place the files inside there? And then once that's done, I create a `mypackage/mytools` directory and place MyToolClass1.java in there with `package mypackage.mytools;` as the first line. – ggkmath Jun 17 '12 at 17:28
  • correct. the directory structure from your classes directory should match your package declaration – Daniel Semmens Jun 17 '12 at 17:31