The complete set of examples is also available for anonymous ftp from ftp://ftp.ibiblio.org/pub/languages/java/javafaq/ioexamples2.zip or via HTTP from IBiblio.
Please feel free to use any fragment of this code you need in your own work. As far as I am concerned, it's in the public domain. No permission is necessary or required. Credit is always appreciated if you use a large chunk or base a significant product on one of my examples, but that's not required either.
The examples that are likely to be reused
are placed in the com.elharo
package. Examples that merely demonstrate a point
or run as a stand alone program or applet are generally placed in the
default package. This means you will need to pay attention to classpath
issues when compiling files. Simply typing
$ javac *.java
will generally fail to compile those files that depend on classes in the com.elharo package, generally because the source code file in the local directory takes precedence over the previously compiled file found in the class path. Typing
$ javac -d . *.java
will generally be more successful. Depending on how you organize your source code files, in some cases you may need to move source code files from one directory to another where the compiler can't find them to avoid compiler error messages that claim something like:
error: File .\JavaFilter.java does not contain type JavaFilter as expected. Please adjust the class path so that the file does not appear in the unnamed package
This error message is misleading. What it really means is that the JavaFilter
class is in fact not in the unnamed package.
When the compiler is compiling a different file that uses the short, non-package
qualified name JavaFilter
instead of the full, package-qualified
name com.elharo.swing.filechooser.JavaFilter
, the compiler first looks for
a class in the unnamed package named JavaFilter
. If it finds a file
named JavaFilter.java
in the current directory, it will try
to compile that, which will fail because JavaFilter.java
contains
com.elharo.swing.filechooser.JavaFilter
and not JavaFilter
.
The solution is much simpler than the explanation: first compile JavaFilter.java
and place the resulting JavaFilter.class in the right place in your class path;
move JavaFilter.java
out of the current directory; and
then compile the file that
depends on JavaFilter.java
.
To run a program in a non-default package from the command line, you'll need to use the full, package qualified name. For example, it's not enought to simply type
$ java StreamCopier
You have to type
$ java com.elharo.io.StreamCopier