Click here
for a more up-to-date HTML version.
(That link will probably just send you
here.)
Frequently Asked Questions (with answers) for Java programmers
_____________________________________________________
________| |________
\ | Java Programmers FAQ http://www.afu.com | /
\ | Last modified Jan 13, 1999 Peter van der Linden | /
/ |_____________________________________________________| \
/___________) (__________\
The Java FAQs here are intended for people who already have some programming
experience, though maybe not in Java.
Go to the FAQ home page at http://www.afu.com for other Java information and
downloads, and the most up-to-date copy of the FAQ. Report FAQ updates to
faqidea at the address afu.com.
------------------------------------------------------------------------
Specify standard Java on your new PC!
Your new PC can come with the most up-to-date standard version of
Java, but only if you ask for it! The JavaLobby is asking PC vendors
to support Java, and to ship new machines with the Java Plug-In
pre-installed.
See http://www.javalobby.org/servlet/PetitionServlet/pjpc
Please help the Java Lobby to promote this initiative.
------------------------------------------------------------------------
Please support Java Portability.
The biggest value of Java is its portability.
* Portability makes it easier for companies to change/upgrade operating
systems and platforms, without losing their investment in applications
software.
* Portability makes it easier for Java programmers to transfer their
skills to new employers.
* Portability makes a wider variety of software available on all
computers.
Software portability is very much in the interest of most software
developers and customers. Even if you only use Windows 95, portability
matters to you. If your software was all written in Java, it would all just
run when you moved from MS-DOS to Windows 3.1 to Windows 95 to Windows 98 to
Windows NT, and even on Windows CE. Instead, you typically need to buy new
applications software all over again when Windows changes. Portability is
not in Microsoft's interest, as it removes a revenue stream and makes it
easy for users to try other operating systems.
The 1998 anti-monopoly case against Microsoft revealed a Microsoft internal
memo. The memo stated that Microsoft's "strategic objective" was to "kill
cross-platform Java." by grow[ing] the polluted Java market." This is
Exhibit 101 (MS7 033448) in the case.
In November 1998, a Federal judge ruled that Microsoft was probably breaking
its written agreement with Sun by distributing incompatible Java, and that
Microsoft had to stop doing that. If portability matters to you or your
users, avoid Java products from Microsoft; it is deliberately undermining
it. See http://www.usdoj.gov/atr/cases/f1700/1762.htm.
-------------------------------
The sections of the Java FAQ are:
* Portability
* 1. Java Book Information
* 2. General Information
* 3. Compilers and Tools
* 4. Getting Started
* 5. Compiler Messages
* 6. Java Language issues
* 7. I/O
* 8. Core library issues
* 9. Computer Dating
* 10. AWT
* 11. Swing
* 12. Browsers
* 13. Applets
* 14. Multi-Media
* 15. Networking
* 16. Security
* 17. For C, C++ Afficionados
* 18. Java Idioms
* 19. Java Gotcha's
* 20. Further Resources
* 21. Acknowledgements
-------------------------------
1. Java Book Information
1. (sect. 1) Learning Java
[*] The Java FAQ is maintained as a service to the Java community. Java
is a great language, and everyone benefits when newcomers get a helping
hand.
Please consider these books from the FAQ author when you are looking
for a programming text.
o "Just Java 1.2" Java, for people who can already program in
another language.
More at
http://www.amazon.com/exec/obidos/ASIN/0130105341/qid%3D912557197/002-7338022-7872449
Comments from a reader: "Just Java is a great book! Before I
bought it, I had a couple of other books that were OK, but I kept
going back to the bookstore to check "Just Java" whenever some
aspect of the language was unclear. Invariably the Just Java
explanations were lucid and to the point. I just had to get it."
o "Not Just Java, 2nd Edition" Java explained for managers and the
rest of us. More info at
http://www.amazon.com/exec/obidos/ISBN=0138646384
Comment from readers: "...An excellent overview of the IT maze"
"... Not Just Java lets readers educate themselves on where we
are, and where we're going."
"This book describes, with graphical illustrations, the emerging
new ideas and how to use them."
Sample chapters are on Sun Microsystems' webpage at
http://www.sun.com/971124/cover-linden/.
2. (sect. 1) How do I choose a Java book?
[*] There is no one right answer to "which is the right Java book for
me?" It all depends on what you already know and how you like to learn.
Here are the points to check when evaluating a programming book.
o Above all, make sure that it is a Java book. If it comes with a
CD, check that it has a Java compiler on it, not a J++ compiler.
J++ is a different language in some small but important ways, and
is missing several of the key Java libraries.
If your interest is Java, leave the J++ book back on the shelf.
o Does the book cover the current level of Java, which is Java 1.2
(aka Java 2)? Look up "JApplet" in the index. If it's not there
the book doesn't cover JDK 1.2, and you probably need a more
up-to-date book.
o Check that the book has a reasonable number of figures, diagrams,
and illustrations. It is not possible to explain how to program a
window system without pictures and diagrams. Other topics benefit
from pictures, too.
o Check what the book says about itself. Is it a reference work,
intended for Java-experts to look things up in? This is the role
of "Java in a Nutshell", and "The Java Almanac". Do you need that,
or are you looking for a book that teaches by examples and
explanations?
o Programming is one of those things that you learn by doing. Check
that each chapter has exercises, preferably with answers. You'll
need to do the exercises to learn the language. Read Peter
Norvig's excellent advice on learning programming languages and
being a programmer.
o Appraise your own level of programming knowledge: are you
proficient in some other language, or are you learning programming
as well? Does the book cater to your needs?
o Read a section of the book. Does the style keep you interested, as
it educates you? Will you get bored if you read many pages? Is the
book too long for your initial purpose? Browse Amazon online and
see what other readers say about the text.
o If the book comes with a CD, how much other software is on the CD?
You want at least a Java compiler plus all the examples from the
book. Does the Java compiler work on your platform (Mac, Linux,
etc)? Additional software on the CD is a big plus, as we learn the
most from reading other people's code.
Probably no one book is perfect for anyone. Most people buy one to
begin with, then four or five more as they wish to learn more, and
about more up-to-date topics. The FAQ author has purchased and read
probably 60 Java books in the last three years.
3. (sect. 1) Where can I find a some lists of Java books and book reviews?
[*] Here are some good ones:
http://www.flathill.com/languages/java/
also
http://www.fastgraph.com/books/java.html
also
http://teamjava.com/links/tj-srv.cgi?MUF=0,tj-booklist.muf
also
http://www.javaworld.com/javaworld/books/jw-books-index.html
(an exhaustive list -- takes a long time to load).
-------------------------------
2. General Information
1. (Sect. 2) Is Java "Open" or "Proprietary"?
[*] The Java specification is publicly available, and anyone is free to
do clean-room implementations of the JVM and core Java API's. Sun
includes a perpetual, irrevocable, free and royalty-free license in the
front of the Addison-Wesley books containing the specification.
Sun also provides free access to the Java source. If you aren't
planning on using the results in a commercial product, or doing any
sort of external redistribution, there is an Internal Noncommercial Use
Source License available at no charge. Please see
http://java.sun.com/nav/business/source_form.html
Using the Java trademark does requires licensing from Sun. It is not
clear if the Embedded or Personal Java specifications are open, as it
is not clear if a clean-room implementation may be done without
licensing from Sun.
The relative openness of Java contrasts with systems that are only
available from one vendor, whose interfaces are developed in secret,
without an open process for others to participate, whose owners do not
allow competing implementations of the same API, and whose owners
change the APIs as a strategic weapon against competitors. Typically,
such systems also feature "private" APIs that are published late or not
published at all, to allow the single vendor to gain a competitive
advantage for their other products. Typically such proprietary systems
do not make the source code available for inspection by all.
2. (Sect. 2) What is the best way to refer someone to the FAQ when they
ask a question I know is answered there?
[*] The Java Programmers FAQ (at http://www.afu.com) answers your
question in section N.n. ...
This gives them the answer, and shows them where to go for future
questions. It also demonstrates that the FAQ can answer their
questions, supplying an incentive to go there next time. It's regarded
as elementary politeness to look for the FAQ of a newsgroup and read it
before posting any questions.
In general, FAQs for any newsgroup are available by looking at past
postings in a group, or by searching Deja News (see Q 1.4), or via
anonymous FTP at directories under ftp://rtfm.mit.edu. The pathnames
are called things like
/pub/usenet-by-group/comp.lang.java.programmer/Java_Programmers_FAQ
which may help you get to the right one directly, as it takes some time
to get a directory listing there. Alternatively, you can look for
newsgroup names on the same ftp site by going to the same site and
looking under /pub/usenet-by-hierarchy/. That has subdirectories such
as alt/, ba/, ca/, comp/, and subdirectories under them such as
/pub/usenet-by-hierarchy/comp/lang/ and so on. This helps you explore
the world of newsgroups with FAQs.
If you do not have anonymous FTP access, you can access the
rtfm.mit.edu archives by mail server as well. Send an E-mail message to
mail-server@rtfm.mit.edu with "help" in the body for more information.
"RTFM" stands for "Read The effing Manual" - you must expect to put in
some time and effort to master a new area of study.
If you want to look at the definition of Internet standards like FTP,
telnet, visit the IETF site at http://www.ietf.org where all the RFC's
(Request For Comments) can be found.
3. (Sect. 2) What if my question is not answered in this FAQ?
[*] Go to http://www.dejanews.com/home_ps.shtml
o Under "Newsgroups" enter "comp.lang.java.programmer" (or whatever)
o Under "Subject" enter "Frotteur" (or other topic you find
pressing)
o Click "Create Filter"
o It will go to a new document, and you should click the link
labeled
nnn Documents (nnn is some number).
The chances are that you will find several answers to your question.
Some may not be complete or completely accurate however. That is the
nature of Usenet, and free information. If you still don't have an
answer, then post your question on the most appropriate of the
newsgroups. Don't spam the newsgroups by posting to multiple groups.
Knowledgeable posters tend to ignore questions like that.
Also look at http://sunsite.unc.edu/java/cgi-bin/query
and look at http://asknpac.npac.syr.edu/ for a Java newsgroup search.
http://www.javaworld.com/search.html can search the Javaworld
newspaper.
4. (Sect. 2) What Java mailing lists are there?
[*] There are quite a few Java mailing lists.
http://java.miningco.com/msub7.htm has a comprehensive list.
5. (Sect. 2) Where can I look at the definitive Java Language
Specification?
[*] This is available online at:
http://java.sun.com/docs/books/jls/html/index.html (Java 1.0)
and the Java 1.1 inner classes document at:
http://java.sun.com/products/jdk/1.1/docs/guide/innerclasses/
spec/innerclasses.doc.html
and the other Java 1.1 update at:
http://java.sun.com/docs/books/jls/html/1.1Update.html
and the Java API is at:
http://java.sun.com/products/jdk/1.1/docs/api
It is also available as a book in printed form (details at website).
Also see the "Clarifications and Amendments"
http://java.sun.com/docs/books/jls/clarify.html.
You can also see the virtual machine (execution environment)
specification at
http://docs.sun.com:80/ab2/coll.127.1/@Ab2CollToc?subject=java
6. (Sect. 2) Where can I find information about future Java APIs?
[*] JavaSoft has followed a policy of creating new APIs in consultation
with leading industry participants, then posting the draft
specification for public review and comments. Check the JavaSoft
roadmap of new APIs and products at
http://java.sun.com:80/products/api-overview/index.html
Also, some APIs that are under consideration, possibly for JDK 1.2 are
at:
http://java.sun.com/products/jdk/preview/docs/
7. (Sect. 2) I'm looking for a Java style guide on naming conventions.
[*] Check out the section "Naming Conventions" in the language
specification
http://java.sun.com/docs/books/jls/html/6.doc.html#11186
Also take a look at Doug Lea's draft coding standard -
http://gee.cs.oswego.edu/dl/html/javaCodingStd.html
See also naming conventions for some basic rules of thumb.
8. (Sect. 2) How do I check on known bugs in the JDK?
[*] Look at the Java Developer Connection at http://java.sun.com/jdc.
All the Java bugs that Sun knows about are listed there, with the
exception of security-related bugs. The legal department in Sun vetoed
the open publication of security bugs. After you have checked that the
bug is not already listed, you can submit a bug report through:
http://java.sun.com:80/cgi-bin/bugreport.cgi
You should check that the bug doesn't already exist for two reasons:
first, you might find the bug with a workaround listed. Second, you
don't want to waste everyone's time with a duplicate bug report.
You can also send in an RFE (Request For Enhancement) or ease-of-use
issue there. You can even vote on the priority you would assign to a
particular bug fix! Join the Java Developer Connection (it's free) by
going to http://java.sun.com/jdc. Then browse
http://developer.javasoft.com/developer/bugParade/#votes
9. (Sect. 2) What computers have Java ports? Is there a port to Win 3.1?
[*] A partial list of JDK ports is available from
http://java.sun.com/cgi-bin/java-ports.cgi
An (impressive) list of the systems that the GPL Kaffee JVM runs on is
at http://www.transvirtual.com/ports.html
There are several Java ports to Win 3.1. IBM's ADK1.02 is available at
the following locations:
o http://ncc.hursley.ibm.com/javainfo/latest/answers/faq0.html
o http://www.alphaworks.ibm.com/formula
IBM offers a port to Linux, as do others. The IBM Jikes port is at
http://www.alphaworks.ibm.com/ There is a large amount of useful
software there, including a profiling tool called jinsight.
Netscape Navigator for Win3.1 has Java support. Java will never be
well-supported under Win3.1 because Win3.1 lacks the basic features
expected of a modern OS (primarily lengthy filenames and multithreading
support).
Also take a look at JavaSoft's JavaPC kit that can switch a PC into a
thin client Java system (and back to Win3.1/DOS when you want). It's
meant for software OEMs and large corporations running lots of older
PCs, but you can use it on the latest Pentium II too. Details are at
http://java.sun.com/products/javapc/index.html. JavaPC is available now
for $100, runs on 486's with 8Mb or more Unlike the 16-bit versions of
Netscape Navigator and Microsoft Internet Explorer, which provide a
Java Virtual Machine that is only compliant with the JDK 1.0.2 API, the
JavaPC software allows IS managers to deploy JDK 1.1-compatible Java
applications on PCs running DOS and Windows 3.x.
10. (Sect. 2) Where can I find information on Java 3D?
[*] The Java 3D FAQ at http://tintoy.ncsa.uiuc.edu/~srp/java3d/faq.html
may have the answers you're looking for. It contains general
information about Java 3D, as well as programming tips.
11. (Sect. 2) Where can I find information about Java Certification?
[*] Sun is sponsoring an examination which programmers worldwide can
take. Those passing can use the designation "Sun Certified Java
Programmer". There is also a second-level test, involving writing a
program to spec, and taking a further test. That results in the
qualification "Sun Certified Java Developer". You can find out all
about the exam at:
http://www.sun.com/service/suned/
and then search for "sun certified java". It costs $150 to sit the Java
Programmer exam. It is not trivial to pass the Java certification exam.
It requires understanding the objectives of the test, and the material
that is tested for. These are given, along with sample questions, at
the URL mentioned above.
There is a Java certification FAQ at: http://www.marcusgreen.co.uk
12. (Sect. 2) How can I find links to recent news about Java?
[*] This site contains links to late-breaking online news stories about
Java. http://www.intelligence.com/java/
Another good Java news source is http://www.nikos.com/javatoys.
This site is a fine site for programmers who want to be well-informed
about computer industry topics. It has a lot of coverage of Linux as
well as more general news. http://slashdot.org Highly recommended.
This site is a source of independent news and commentary on the
computer industry, including Java. http://www.pjprimer.com/media.html.
You have to subscribe ($10/year, 30 day free trial).
13. (Sect. 2) What are the folks at GNU doing with Java?
[*] First note that the URLs in this section change quickly, and soon
become outdated. If you have an update, send it in. There is a Gnu Java
page at http://www.gnu.org/software/java/java.html
Guava (a GPL'd Java compiler) can be found at
ftp://ftp.yggdrasil.com/pub/dist/devel/compilers/guavac/
Alternatively, it may be available at
http://http.cs.berkeley.edu/~engberg/guavac
Work is progressing on the Cygnus Java frontend to gcc. See
http://www.cygnus.com/product/javalang/
Kaffe (a JVM) can be found at
http://www.transvirtual.com This is Tim Wilkenson's company
devoted to commercializing the Kaffe JVM for the embedded systems
market. He also releases a version of it under the GPL. It also
comes with a the beginnings of a class library and the Pizza
compiler.
Classpath is a free implementation of Sun's core Java libraries (v1.1),
being developed for the GNU Project ( http://www.gnu.org). Information
regarding classpath is at http://www.classpath.org They aim to develop
a 100% free drop in replacement for Sun's class libraries, targeting
first the Japhar JVM (below). They are always looking for help, so feel
free to stop by and volunteer.
See also http://www.japhar.org This is the Hungry Programmer's JVM.
Currently it is development grade only.
14. (Sect. 2) What is "San Francisco"?
[*] San Francisco is the code name for a very large Java project led by
IBM, and involving other companies. The project is to provide a Java
framework for data processing applications. A large number of classes
are provided for general ledger, sales order, inventory management,
etc., and these classes can be extended and customized for particular
industries (vertical markets). It is a large and ambitious software
project.
IBM's SF project competes with products from companies like SAP and
Baan. Of course, the SF project is multi-platform and uses Java beans
and GUI interfaces. More information on SF is available at
http://www.ibm.com/Java/Sanfrancisco
15. (Sect. 2) What large Office-style or other applications have been
written in Java?
[*] Well, the first one to consider is IBM's San Francisco project,
mentioned above. There is also Lotus's e-suite - a suite of Java
applets and beans including a spreadsheet and a word processor. See
http://esuite.lotus.com. These became available in March 1998.
Another office suite in Java is Applix Anyware at
http://www.applix.com/anyware/index.htm. Applix became available in
downloadable demo form in April 1998.
Yet another is Star Division's Client/Server Office. It is an office
suite with the client part written in Java and able to run on
JavaStations. The server part will run on Solaris, NT, OS/390, and
AS/400. The older (non-Java) version is bundled with all Sun
workstations sold in Germany. The Linux version is freely downloadable
from http://www.stardivision.com.
Another is Digital Harbor's Wav word processor. It supports component
software, and it runs in 1MB, not the 114Mb of the latest MS Word. A
free trial is avilable. See: http://www.digitalharbor.com
Another Java application is Formula One for Java, an Excel-compatible
spreadsheet written in 100% pure Java, and available for all systems
that support Java. It runs as a Java Bean, so can easily be assembled
as one component of a larger system. It also runs as an application,
and as an applet! Formula One is a product of Visual Components, Inc.
See http://www.f1j.com.
Another one is Ncode Research Inc. who write Java viewers for office
suites. They are file-format specialists. Their mission is to make all
popular file formats available for the Java platform. They write 100%
Pure Java viewers for Word, Excel and PowerPoint (including Office 95
and 97 formats). See http://www.ncode.com/
Another company operating in the same space is JSoft, at
http://www.jsoftinc.com
The niche for single-user office productivity applications is pretty
well already dominated by Microsoft products, and it is unrealistic to
think that Java software will unseat shrink-wrapped software simply
because it is written in Java. This is why Corel replanned its Java
rewrite of Corel Office before taking it to FCS. When Corel did that,
it also increased its investment in Java from 33% of R&D budget to 50%,
at the expense of Windows.
Most of Java development is taking place for custom applications
internal to a company. Most programmers of any kind have never worked
on MS Office, but work on internal applications, and so it is with
Java. These projects don't have the high profile of major vendors'
products, but they are the mainstay of the industry. There are many
companies working on Java Beans, like http://www.quadbase.com who have
Java graphing software. EspressChart is a Java Bean that gives you the
ability to add 2D and 3D graphs in your applications/applets. This bean
is easy to use, 100% Java, and runs anywhere.
There are some good Java games applets at
http://www.frontiernet.net/~imaging/java_games.html
If you want to use Java to learn math & computer graphics, visit
http://www.frontiernet.net/~imaging/math_is_a_game.html
Finally, note that Sun's Java compiler is written in Java. This is a
really big application in widespread use on millions of platforms. The
compile command "javac test.java" is equivalent to
java sun.tools.javac.Main test.java
javac has a script wrapper just to set the heap size as a command line
argument, as you can do in your own programs.
16. (Sect 2.) What Java User Groups are there?
[*] There are scores of Java User groups around the world, mostly in
urban areas, and centers of software technology development. A partial
list with contact information can be found at
http://sunsite.unc.edu/javafaq/usergroups.html.
If you can't find a user group in your area/school, it's easy and
satisfying to start one.
17. (Sect 2.) What is a Java Bean?
[*] A Java bean is a Java class that follows some simple conventions.
Because it follows conventions, it can easily be processed by a
software tool that connects Beans together. Java beans are reusable
software components.
Think of Java beans as being the software equivalent of Lego[tm]
bricks. Instead of plugging together plastic bricks, you can easily
plug together classes, and have them fit and work with each other. See
http://www.jc100.org/
See the Java Bean FAQ at http://java.sun.com/beans/faq/faq.general.html
18. (Sect 2.) Where can I find examples of the use of the Java class
libraries?
[*] The two volumes of "Java Class Libraries" by Chan, Lee and Krama
published by Addison Wesley, have extensive examples of how to use the
standard libraries. One programmer comments "When I need to use an
unfamiliar area of the class libraries one of the first things I do is
read their examples." You can see them online at
http://java.sun.com/docs/books/chanlee/second_edition/vol1/examples.html
and http://java.sun.com/docs/books/chanlee/second_edition/examples.html
19. (Sect 2.) How can I find out exactly what version of Java I have on my
system?
[*] On a Solaris system, you can use the pkginfo command, like this:
pkginfo -l SUNWjvrt
It will give a reply like this:
PKGINST: SUNWjvrt
NAME: JavaVM run time environment
CATEGORY: system
ARCH: sparc
VERSION: 1.1.6,REV=1998.07.30.16.21
BASEDIR: /
VENDOR: Sun Microsystems, Inc.
...etc
You may also try
java -fullversion
Although that's not an officially-supported command option, and has
gone away in JDK 1.2. Try also
java -version
-------------------------------
3. Compilers and Tools
1. (Sect. 3) Is there a lex and yacc or preferably a flex and bison
equivalent for Java?
[*] There is a lex equivalent called JavaLex and a yacc equivalent
called CUP.
LALR(1) parser JavaLex and JavaCup:
http://www.cs.princeton.edu/~appel/modern/java/
LL(k) parser JavaCC: http://www.suntest.com/JavaCC/
LALR(1) parser SableCC from McGill U.
http://www.sable.mcgill.ca/sablecc/index.html is generously made
available under GNU license.
2. (Sect. 3) Where can I find a byte code obfuscator?
[*] There is a commercially supported obfuscator, with a downloadable
free trial at http://www.4thpass.com/SourceGuard. There are also some
free works from students and others. http://www.primenet.com/~ej/
http://www.math.gatech.edu/~mladue/HoseMocha.java
Some people have reported problems using these with JDK 1.1.
This obfuscator has been updated to be fully compatible with JDK 1.1:
http://www.monmouth.com/~neil/Obfuscate.html
Obfuscators are intended to foil decompilers. Decompilers translate
byte code back into Java source code. Mocha was the first and most well
known of the decompilers; it's no longer supported. There is a
decompiler (written in C++) at
http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
Because it is in C++, there are different versions for every
architecture (hah!) There are also commercial products, such as
SourceAgain from
http://www.ahpah.com/
There's a very good Java Code Engineering and Reverse Engineering FAQ
page at http://Meurrens.ML.org/ip-Links/Java/codeEngineering/.
3. (Sect. 3) Which program is used to create .zip files compatible with
the java* programs?
(e.g. classes.zip, moz3_0.zip)
[*] Use the jar-tool from JDK1.1(.1):
jar [ options ] [manifest] destination input-file [input-files]
E.g.:
jar cvf myJarFile.jar *.class
creates a compressed archive
jar cvfO myJarFile.zip *.class
creates it fullsize (uncompressed) (note the 'O'-option used for
JDK1.0.2)
On Unix you can also use:
zip -rn ".class" my_file.zip *
Info-ZIP home page: http://www.cdrom.com/pub/infozip/
Latest source code: ftp://ftp.uu.net/pub/archiving/zip/src/zip21.zip
Netscape's command line version of its JAR packager and signing tool is
called "zigbert". They also have a signing tool with GUI written in
Java. More info
http://developer.netscape.com/software/signedobj/jarpack.html
If you zip your .class files for JDK 1.0.2 (for 1.1 you'll use a Jar):
1. zip your files uncompressed (can use WinZip 6.2 up);
Unix command:
zip -r0 classes.zip
2. Make sure the main class has no parent directory inside the
archive, (in other words, don't build an archive with
foo/bar/myMain.class, unless your myMain is in a package called
foo.bar. Instead start it at myMain.class). Your packages must be
placed in the archive using their corresponding filesystem
pathnames.
3. Put the archive in the same directory as the .html page.
4. Put something like the following tag in the .html file:
From JDK 1.1 on, an example of the applet tag used with a jar file is
These lines will use an applet called myapplet that can be found in the
jarfile myfile.jar. An example applet tag of a jar file used to hold
classes in packages is
You can supply several jar filenames in a comma-separated list. Jar
files are in compressed PKZIP format.
4. (Sect. 3) Can I compile a Java program to a binary executable, .exe on
a PC?
[*] Compiling into native code destroys portability, which is one of
the main benefits of Java. If you want to create a native executable
because you wanted to make it easy to distribute and use programs,
consider a Jar file instead.
Some companies make products that do this. See the webpages for
Symantec http://www.symantec.com, Supercede http://www.supercede.com,
and Tower Technology http://www.twr.com. The first two are targeted to
Windows. Tower Technology supports several flavors of Unix.
Also, there is a native Java compiler from IBM, known as the HPJ (High
Performance Java) compiler. One user has reported that it created a 2Mb
executable from a 12K java file, and did not run any faster. See
http://www.alphaworks.ibm.com/
See also Instantiations JOVE http://www.instantiations.com/jove.htm,
the paper about the Toba project
http://research.microsoft.com/research/lt/toddpro/papers/coots97.pdf,
Network World, "Vendors Rush To Speed Java Performance", Feb 9 1998, at
http://www.nwfusion.com/news/0209java.html
Compiling to native code takes away the most significant benefit of
Java: portability of executables. Further, if you want your Java DLL
(or .exe) to interact with C++, you'll have to specify which specific
C++ compiler and/or actually compile some sort of linkage via the
appropriate C++ compiler. C++ does not have a standard ABI, so there is
a big problem with interoperability. Every C++ compiler uses a
different object model, a different way of laying out class members,
and a different way of "mangling" names for the linker.
C is much simpler. The only question here is how structures are
"packed" (i.e., are integers aligned on four-byte bounds?). All the C++
compilers can interact with C code, thanks to 'extern "C"'
declarations.
Consider carefully why you want to compile to a native executable, and
whether there is a Java way to accomplish your goal. There may be a
good reason for compiling to native code, but it needs to be thought
through.
5. (Sect. 3) How can I performance profile my Java code?
[*]java -prof MyClass
produces some basic output in a file called java.prof, showing the
number of times methods were invoked. The output lines are of the form:
# of calls method called called by time spent
On a Unix system, you can sort the file with something like
sort -r +82 java.sort
More and better Java tools are a third party opportunity. One Java
profiler is JProbe Profiler, available from http://www.klg.com/jprobe.
JProbe is said to be easy to use. Another profiler is OptimizeIt,
available from http://www.optimizeit.com. Each of these profilers has
performance tuning, which shows which methods took how much time, and
also memory tuning, which shows what objects are in memory and how they
were allocated. Both are important things to know. The latest version
of the CodeWarrior IDE http://www.metrowerks.com has a time-based
profiler for Java code. Java Workshop from Sun also has a time-based
profiler.
6. (Sect. 3) When I use javadoc and I click on any java class included in
the JDK why do I get this message?
Netscape is unable to find the file or directory named:
/E|/Jwrkshop/JDK/bin/java.lang.Throwable.html
[*] References to the JDK classes assume that all generated html files
are in the same directory and, in fact, that all files for all classes
referenced are generated at the same time. There is no way to generate
files incrementally and have them all reference each other, as you
would like.
As long as you have source for everything involved (including the JDK
and all third-party classes), you can list all of your packages and all
of the others on the javadoc command line and generate the whole set at
once, but it is burdensome. Of course, if you receive any libraries as
.class files, even this workaround will not suffice.
Also javadoc will not generate the image files - you need to get them
from the images directory under the JDK API documentation files. You
can just copy the entire directory into your own doc directory. javadoc
is a very nice concept, with a few implementation flaws.
7. (Sect. 3) I'm working on a project with lots of classes and I use the
JDK. A recompile from scratch takes forever when I do it a class at a
time. How do I recompile everything?
[*] The first way is
javac *.java
Another way is
javac -depend tip.java
where "tip.java" is a class "at the tip of the iceberg", i.e. that
depends on (uses) all the other classes. Typically, this may be your
main class. However, "-depend" is known to be buggy and cannot be
relied upon. It also doesn't issue compile commands in parallel to make
use of multi-processor systems.
Without the "-depend" option, the standard "javac files" doesn't look
beyond the immediately adjacent dependencies to find classes lower down
the hierarchy where the source has changed.
The -depend options searches recursively for depending classes and
recompiles it. This option doesn't help when you have dynamically
loaded classes whose names cannot be determined by the compiler from
the dependency graph. E.g. you use something like
Class.forName(argv[0]);
The author of the code using those classes should make sure that those
classes are mentioned in a Makefile.
8. (Sect. 3) Why do I get the java.lang.UnsatisfiedLinkError when I run my
Java program containing Native Method invocations?
[*] Your program is not able to find your shared library or DLL.
On Windows 95/NT, make sure that the DLL exists in a path that is
included within the PATH environment variable. (This need is true for
both standard (untrusted) applications and trusted applets. At least,
if you use the Java Plug-in to give yourself standard Java inside a
browser).
On Solaris, make sure that the environment variable LD_LIBRARY_PATH
includes the path of your shared library.
9. (Sect. 3) An anonymous class can't seem to access a private outer
method. Why is that?
[*] It's a known bug in the JDK 1.1.4. The code is:
public class MyDialog {
void Setup() {
addWindowListener( new WindowAdapter() {
public void windowClosing(WindowEvent e) {
myCloseWindow(); }
}
); // anon inner class
}
private void myCloseWindow() { // private outer method
dispose();
}
}
This code sends javac into an infinite loop. The workaround is to make
the private method non-private, or to make the inner class a named
class. Sun put a workaround in the compiler to silently set the field
to package access.
10. (Sect. 3) What are the major Java releases and their contents?
[*] There have been three Java releases from Sun so far, plus a number
of bugfix (dot-dot) releases. The releases are:
o JDK 1.0.2
This was the release FCS in May 1996. It had some security fixes
over JDK 1.0.
o JDK 1.1
This release (Feb 1997) introduced a new event model in the window
system. It also made JDBC support and beans support a standard
feature. It changed and standardized the native code interface to
JNI. It also introduced inner classes.
o JDK 1.2
This release (Dec 1998) made the Swing library a standard feature.
Swing is a set of rich platform-independent graphical components.
11. (Sect. 3) What is the difference between jre and java?
[*] They are functionally equivalent, with minor differences in the
handling of default classpath and options supported. To reduce
confusion, the jre command was removed in JDK 1.2. Instead there is a
"java" command in both bin and jre/bin.
jre.exe is the java launcher that comes with the Java Runtime
Environment. It ignores the CLASSPATH environment setting in favor of
its own internally generated default and whatever is supplied on the
cmd line using -cp or -classpath. It's intended to be a bit simpler for
those who are only ever running Java programs, not developing them.
java.exe is the java launcher that comes with the JDK. It uses the
CLASSPATH environment setting as a starting point and then tacks on its
own internally generated entries.
They both serve the same purpose and that's to start a Java VM, have it
run a Java application, then terminate. The source for jre.exe is
provided in the JDK. The source to java.exe is provided only in the JDK
Source distribution.
12. (Sect. 3) What IDEs (Integrated Development Environments) are there?
[*] Some popular IDEs include:
Apptivity 2.0
(Progress) http://www.apptivity.com
Bluette http://blue.donga.ac.kr/bluette/index.html
CodeWarrior
Professional http://www.metrowerks.com
GRASP (product is free)http://www.eng.auburn.edu/grasp
Grinder http://www.tpex.com
Java WorkShop 2.0 (Sun)http://www.sun.com/workshop/java
Javelin, Visual Object
Development for Java http://www.stepahead.com.au
JBuilder (Inprise) http://www.inprise.com/jbuilder
JDE for emacs http://sunsite.auc.dk/jde/
Kawa (Webcetera) http://www.tek-tools.com/kawa
Metamata http://www.metamata.com
NetBeans (Swing-based) http://www.netbeans.com
PARTS alpha
(ObjectShare) http://www.objectshare.com
PowerJ (Sybase) http://www.sybase.com/products/powerj
SilverStream http://www.silverstream.com
Simplicity for Java http://www.penumbrasoftware.com
SuperCede 2.0
(Asymetrix) http://www.supercede.com
teikade 1.8R2 (PFU Ltd)http://www.pfu.co.jp/teikade
Together/J 2.0 (Object
Intl Inc.) http://www.oi.com
Visaj 1.0.1 (Imperial
SW Tech) http://www.imperial-software-tech.co.uk
VisualAge 1.0 (IBM) http://www.software.ibm.com/ad/vajava
Visual Cafe 2.1
(Symantec) http://cafe.symantec.com
Visual J++ (Microsoft) (deliberate incompatiblities; not recommended)
Xelfi 0.94 http://www.xelfi.com
13. (Sect. 3) Why is Visual J++ not recommended?
Because Microsoft's strategic objective is "Kill cross-platform Java"
[*] It is not in Microsoft's financial interest to allow users to
easily move software to different platforms. Microsoft is the only
company in the computer industry that is actively trying to undermine
Java. This is not speculation -- the Department of Justice's lawsuit
quoted a Microsoft memo describing the strategic objective to "kill
cross-platform Java by grow[ing] the polluted Java market". See
http://www.usdoj.gov/atr/cases/f1700/1762.htm
Microsoft is being sued because of unauthorized changes it made in
Java. A federal judge issued a preliminary injunction against Microsoft
in March 1998, prohibiting them from labelling their incompatible J++
product as Java. Another injunction was issued against Microsoft in Nov
1998, requiring them to remove deliberate incompatibilities with
standard Java. (Recall that Microsoft did not create Java, but
contracted with Sun to distribute it).
Speak to your management
chain - how comfortable do
they feel using a Microsoft product that is embroiled in a legal
dispute, that introduces deliberate incompatibilities, and whose stated
goal is to lock you in to one platform? It is a safer choice to get
standard Java from any other source than Microsoft. You can use these
facts to move your company to standard Java.
As a Java programmer please join the Java Lobby, an independent
organization dedicated to representing independent non-vendor interests
in Java. It's free, and you can sign up by visiting
http://www.javalobby.org for details. Other ways to encourage portable
java:
o Use development environments from other vendors, or convert
Microsoft Visual J++ to use Sun's JDK, following the instructions
at http://www.orbiter.demon.co.uk/
o Use Netscape Communicator (not Internet Explorer)
o If required to use Internet Explorer, use the Java Plug-In to get
a standard Java system inside it.
o Use a standard JVM from GNU, Kaffe, any of the IDE vendors, or Sun
(but not Microsoft's J++ SDK)
o Free standard Java compilers and the Java Plug-In can be
downloaded from http://java.sun.com.
o Free standard Java Virtual Machines can be downloaded from
http://www.kaffe.org, http://www.oryxsoft.com/projects/gvm, and
http://www.redhat.com/linux-info/jolt
o Free Java AWT software can be downloaded from
http://www.biss-net.com/biss-awt.html and the files are all at
ftp.java-linux.org (the linux site) too.
o Free Java software can be downloaded from
http://www.gnu.org/software/java/java.html
Just for the record, the May 1998 federal case against Microsoft has
nothing to do with innovation or product design, as Microsoft
frequently insists. Microsoft is actually charged with
o taking anti-competitive action to exclude competition in browsers,
in order to protect its monopoly in desktop operating systems.
o using its monopoly to impose restrictive agreements that require
PC manufacturers to accept the Microsoft browser with Windows, and
that hinder the promotion of competing browsers.
Many people think that contracts prohibiting the distribution of a
third party's products are somewhat sleazy. Such contracts are also in
restraint of competition and illegal when used by a monopoly. This is
why Microsoft is facing mounting legal problems in the United States,
Italy, Brazil, and the European Union.
-------------------------------
4. Getting Started
Don't forget to look at the "Compiler Messages" in the next section,
too.
1. (Sect. 4) What is the easiest way to get started with Java?
[*] Follow these steps.
1. Download a free Java compiler from http://java.sun.com
2. Read the free Java tutorial, at the same website (bookmark it, so
you will easily find it again).
3. Avoid Microsoft's J++ product, which is in the words of
Microsoft's own employees "polluted Java". It is designed to
undermine standard Java, and has many deliberate platform-specific
incompatibilities, including new keywords in the language.
4. Look at the beginning of this FAQ for book info and book reviews.
There is no one perfect Java book. The right book depends on the
style and pace that you are most comfortable with. Amazon has good
info and reviews on Java books.
5. Search this FAQ when something in Java confuses you. Many people
have trodden this path before you, and the FAQ contains the
accumulated knowledge and pointers to other references.
2. (Sect. 4) Why doesn't my "Hello World!" program compile?
[*] There are three basic possibilities:
1. Are you successfully running the javac compiler?
Try
javac -garbage
to see if it prints out a message about correct usage. If not,
invoke javac using the full pathname, or set your PATH variable to
include the directory that contains javac.
2. Is the CLASSPATH environment variable used correctly?
In JDK 1.0.2, it was a mistake for beginners not to set CLASSPATH.
In JDK 1.1, it is a mistake when beginners do set CLASSPATH.
In JDK 1.2, CLASSPATH is often not needed. You can put your
classes and Jar files in a directory under the JDK installation,
which the runtime system always checks.
When CLASSPATH is wrong, javac will tell you it can't find
definitions for classes you reference in the source file. For
information on setting up the CLASSPATH, see Question 4.3
3. Is the source correct?
Here javac will emit error and warning messages. See the questions
on compiler messages in the next section.
3. (Sect. 4) Why doesn't my "Hello World!" program run?
[*] There are five common mistakes that cause your VM (java or browser)
to be unable to execute your classes
1. First, did you write an applet or an application? If an applet,
you must make sure that you did extend the java.applet.Applet
class.
2. You must declare your main class as "public". If you don't,
unfortunately some systems will still run the code, while others
won't. The main class is either the one with the main() method in,
or in the case of an Applet, the class that extends Applet.
3. Your class name and the file name must match exactly, even letter
case. If your class is HelloWorld, your source file must be
HelloWorld.java and your class file will be "HelloWorld.class".
4. If an Applet, and you used ftp to transfer the classes to the
server, you must ftp all the classes, and you must use BINARY
transfer not ASCII.
5. Errors in setting the CLASSPATH (and/or codebase in an applet).
Even seasoned programmers do this, pointing inside a package or
mistyping a path delimiter. For information on setting up the
CLASSPATH and codebase, see Question 4.3
If you are running an applet, you should check the following further
points:
1. If your class isn't loading, recheck the HTML applet tag.
2. If you are writing to System.out, the results are displayed in the
browser's java console. You'll have to create a window if you want
one.
3. Make sure your browser is compatible with the Java language
features you are using. Internet Explorer and older versions of
Netscape's browsers have omitted some support for JDK 1.1. Try
your applet in the JDK's appletviewer first.
4. How do I set the CLASSPATH?
[*] The CLASSPATH environment variable tells the VM's class loader
where to find classes that are directly or indirectly invoked,
including system classes. The CLASSPATH variable should
o point to the class directory, for classes not in a package.
o point to the package root, for classes in a package. The root is
the parent directory of the highest directory of the package name.
o point directly to the zip or jar file, if the classes are in an
archive file. You may have to list the contents of the archive to
get the correct package/path name for the class.
Separate multiple paths and archives with a platform-specific
separator, ";" for Windows; ":" for Solaris
Also remember that
o Browsers set the CLASSPATH to the directory of the HTML file, plus
the codebase parameter.
o in JDK 1.1 and after, java adds the system classes
(lib/classes.zip), so you don't have to.
o most versions of java add "." (current directory), so you don't
have to. (But jre doesn't - see below.)
o JDK 1.1 jre tool does not use the CLASSPATH variable or assume the
current directory. (On Solaris, CLASSPATH does work.)
From JDK 1.1.2 on, it is generally an error if the user sets the
CLASSPATH to include classes.zip. But CLASSPATH will need to be set to
o point to the roots of the programmer's own packages, and third
party packages
o use rmic
o use unbundled packages like Swing in JDK 1.1
o point to native code libraries.
If you're not doing any of these, do not set CLASSPATH. If you have set
it, unset it.
Below you'll find examples for Windows (basic application class),
Solaris (package class), javac (multiple packages), and browsers
(applet codebase).
-----------------------------
Here's some Windows examples, assuming the application class is
D:\src\tries\HelloWorld.class
## JDK 1.1, no CLASSPATH set
> cd D:\src\tries\
> D:\jdk11\bin\java HelloWorld
# OK: 1.1 implicitly adds classes.zip and current dir
> D:\jdk11\bin\jre HelloWorld
# FAILS: jre does not automatically add . to CLASSPATH
> cd D:\
> D:\jdk11\bin\jre -cp D:\src\tries HelloWorld
# OK: jre adds classes.zip, -cp adds class directory
## JDK 1.1, CLASSPATH set
> set CLASSPATH=D:\src\tries
> D:\jdk11\bin\java HelloWorld
# OK: java using CLASSPATH
> D:\jdk11\bin\jre HelloWorld
# FAILS: jre does not use CLASSPATH (on Windows)
## JDK 1.0.2, CLASSPATH set
> set CLASSPATH=D:\jdk102\lib\classes.zip;D:\src\tries
> D:\jdk102\bin\java HelloWorld
# OK:
> set CLASSPATH=D:\jdk102\lib\classes.zip;D:\src\tries
> D:\jdk11\bin\java HelloWorld
# FAILS: exception in thread NULL - wrong system classes
-----------------------------
Here's some Solaris examples, assuming the application class is
/usr/src/com/devjoes/killer/App.class
and it is in package com.devjoes.killer:
# JDK 1.1, no CLASSPATH set
$ /usr/bin/jdk11/bin/jre -cp /usr/src com.devjoes.killer.App
# OK:
$ cd /usr/src/com/devjoes/killer/
$ /usr/bin/jdk11/bin/java App
# fails: class name and path are wrong
$ CLASSPATH=/usr/src/
$ /usr/bin/jdk11/bin/java App
# fails: class name is com.devjoes.killer.App
$ /usr/bin/jdk11/bin/java com.devjoes.killer.App
# OK:
-----------------------------
Here's some javac examples, for both Solaris and Windows, based on the
following:
Source files package Makes the call
/usr/src/pack/Minimal.java package pack pack.sub.Try.run()
/usr/src/pack/sub/Try.java package pack.sub (nothing)
$ CLASSPATH=""
$ /usr/bin/jdk11/bin/javac /usr/src/pack/sub/Try.java
# OK: works fine
$ /usr/bin/jdk11/bin/javac /usr/src/pack/Minimal.java
# FAILS: can't find pack.sub.Try
$ cd /usr/src
$ /usr/bin/jdk10/bin/javac pack/Minimal.java
# OK: finds pack.sub.Try based on . as package root
$ cd /usr/src/pack
$ CLASSPATH=/usr/src
$ /usr/bin/jdk11/bin/javac Minimal.java
# OK: finds pack.sub.Try based on CLASSPATH
Now assume the killer application class
/usr/src/com/devjoes/killer/FastApp.java
(in package com.devjoes.killer) uses a third-party package in a jar
file
/usr/jars/JShapes.jar
but makes no other reference to other classes. The following works
fine:
$ CLASSPATH=/usr/jars/JShapes.jar
$ cd /usr/src/com/devjoes
$ /usr/bin/jdk11/bin/javac killer/FastApp.java
Finally, some applet examples. Many applets only use one class, in the
same directory as the html file: