com.mycompanypackage
containing classes CompanyApp and
BusinessLogic;
and org.mypersonalpackages.util containing classes
Semaphore and HandyBits.
BusinessLogic needs access to HandyBits
(and your boss has waived rights to your personal code so long as
you don't mind putting it in the product!)
c:\java for my examples.
c:\java\com\mycompanypackage\CompanyApp.java
c:\java\com\mycompanypackage\BusinessLogic.java
c:\java\org\mypersonalpacakges\util\Semaphore.java
c:\java\org\mypersonalpacakges\util\HandyUtil.java
package com.mycompanypackage;
BusinessLogic.java
could start:
package com.mycompanypackage; import org.mypersonalpackages.util.*;or possibly
package com.mycompanypackage; import org.mypersonalpackages.util.HandyUtil;Some people like using import-on-demand (the first version), others don't. To be honest, it's mainly a matter of laziness. I use import-on-demand because I'm lazy. I realise that it could create incompatibilities if names clash later on, but within the standard Java packages that I use that's not likely to happen very often. (This is partly because I don't do much GUI work. If I were using the
java.awt and java.util packages in the
same class, I'd be more careful. I'll try to keep my publicly
available source a bit cleaner though. (Don't look yet - I haven't
done the cleaning!)
HandyUtil
refers to Semaphore.SOME_CONSTANT which is a static
final primitive/literal String, the value will be embedded in
HandyUtil.class.) There are two ways you could compile
everything. Either specify it explicitly:
c:\java> javac -d . com\mycompanypackage\*.java org\mypersonalpackage\util\*.javaor create a list of files and pass that to javac:
c:\java> dir /s /b *.java > srcfiles.txt c:\java> javac -d . @srcfiles.txtNote that you compile from the root directory, and you specify
-d . to tell the compiler to put the classes in a
package structure starting at the root too. Some people don't like
keeping class and source files together - in that case, you could
use -d classes, although you need to create the
classes directory first. (You'll also need to either
compile everything each time or put classes on the classpath for the
compiler with the -classpath option.) Unless you really know
what you're doing, I suggest you use the above and make sure you
don't have a classpath set. If you must use a classpath
for some reason (see my notes on the
extension mechanism for ways to avoid this), make sure that
. (the current directory) is on the classpath.
java.lang.NoClassDefFoundError: MyCompanyApp (wrong name:
com/mycompanypackage/MyCompanyApp. Hopefully, if you've
understood everything above, you won't run into this. It happens if
you try to run your code using something like this:
c:\java\com\mycompanypackage> java MyCompanyAppHere's how to avoid it:
c:\java
c:\java> java com.mycompanypackage.MyCompanyApp
Back to the main page.