📄 collect.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Writing Advanced Applications, Chapter 7: Collecting Evidence</TITLE>
<META NAME="AUTHOR" CONTENT="Monica Pawlan and Calvin Austin">
<META NAME="KEYWORDS" CONTENT="programming, advanced, Java 2">
<META NAME="OWNER" CONTENT="Editorial/JDC">
<META NAME="revision" CONTENT="@(#)collect.src 1.13 09/21/99 JDC">
</HEAD>
<!-- Start Body Insert-->
<BODY BGCOLOR="#ffffff">
<!-- End Body Insert-->
<!-- Start PageTop Insert -->
<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="0" WIDTH="100%">
<TR ALIGN="CENTER" VALIGN="TOP">
<TD WIDTH="157" ALIGN="LEFT">
<IMG SRC="/images/pixel.gif" HEIGHT="40" WIDTH="40" ALT="">
<A HREF="http://java.sun.com/index.html"><IMG SRC="/images/javalogo52x88.gif" WIDTH="52" HEIGHT="88" ALT="Java Technology Home Page" BORDER="0"></A>
<BR>
<IMG SRC="/images/pixel.gif" WIDTH="157" HEIGHT="1" ALT=""></TD>
<TD>
<FORM NAME="seek1" METHOD="GET" ACTION="http://search.java.sun.com/query.html">
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" WIDTH="100%">
<TR>
<TD ALIGN="RIGHT">
<IMG SRC="/images/stripelt.gif" WIDTH="6" HEIGHT="14" ALT=""></TD>
<TD WIDTH="100%">
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" WIDTH="100%">
<TR>
<TD BGCOLOR="#CC9966" WIDTH="100%">
<IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
</TR>
<TR>
<TD>
<IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
</TR>
<TR>
<TD BGCOLOR="#CC9966">
<IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
</TR>
<TR>
<TD>
<IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
</TR>
<TR>
<TD BGCOLOR="#CC9966">
<IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
</TR>
<TR>
<TD>
<IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
</TR>
<TR>
<TD BGCOLOR="#CC9966">
<IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
</TR>
</TABLE>
</TD>
<TD ALIGN="LEFT">
<IMG SRC="/images/stripert.gif" WIDTH="6" HEIGHT="14" ALT=""></TD>
<TD>
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0">
<TR>
<TD VALIGN="CENTER">
<A HREF="http://java.sun.com/a-z/index.html"><IMG SRC="/images/azindex.gif" BORDER="0" WIDTH="72" HEIGHT="11" ALT="A-Z Index"></A></TD>
<TD VALIGN="CENTER">
<FONT FACE="Helvetica" SIZE="1">
<INPUT TYPE="text" SIZE="15" MAXLENGTH="128" NAME=qt></FONT></TD>
<TD VALIGN="CENTER">
<INPUT TYPE="image" SRC="/images/search.button.gif"
value="search" BORDER="0" WIDTH="55" HEIGHT="14" ALT="Search"></TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
</FORM>
<P>
<TABLE BORDER="0" WIDTH="100%" CELLPADDING="0" CELLSPACING="0">
<TR VALIGN="TOP">
<TD WIDTH="100%"><IMG SRC="/images/chiclet.row.gif" WIDTH="55"
HEIGHT="18" ALT=""></TD>
<TD ROWSPAN="4" ALIGN="RIGHT" WIDTH="152">
<A HREF="/developer/index.html"><IMG SRC="/images/developer.connection.header.gif" BORDER="0" HEIGHT="42" WIDTH="319" ALT="Java Developer Connection(SM)"></A></TD>
</TR>
<TR VALIGN="TOP">
<TD BGCOLOR="#FFFFFF" HEIGHT="1" WIDTH="100%">
<IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD>
</TR>
<TR VALIGN="TOP">
<TD BGCOLOR="#CC9966" HEIGHT="1" WIDTH="100%">
<IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD>
</TR>
<TR VALIGN="TOP">
<TD><A HREF="/developer/onlineTraining/"><IMG SRC="/images/online-training.gif" ALT="Online Training" BORDER=0></A></TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
<!-- End PageTop Insert -->
<!-- Start NavBar Insert -->
<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="3" BGCOLOR="#FFFFFF" WIDTH="157" ALIGN="LEFT">
<!-- tab categories -->
<TR>
<TD><A HREF="http://java.sun.com/products/"><IMG SRC="/images/side.tab.products.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Downloads, APIs, Documentation"></A></TD>
</TR>
<TR>
<TD><A HREF="/developer/index.html"><IMG SRC="/images/side.tab.developer.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Java Developer Connection"></A></TD>
</TR>
<TR>
<TD><A HREF="/developer/infodocs/index.shtml"><IMG SRC="/images/side.tab.docs.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Tutorials, Tech Articles, Training"></A></TD>
</TR>
<TR>
<TD><A HREF="/developer/support/index.html"><IMG SRC="/images/side.tab.support.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Online Support"></A></TD>
</TR>
<TR>
<TD><A HREF="/developer/community/index.html"><IMG SRC="/images/side.tab.community.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Community Discussion"></A></TD>
</TR>
<TR>
<TD><A HREF="http://java.sun.com/industry/"><IMG SRC="/images/side.tab.news.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="News & Events from Everywhere"></A></TD>
</TR>
<TR>
<TD><A HREF="http://java.sun.com/solutions"><IMG SRC="/images/side.tab.solutions.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Products from Everywhere"></A></TD>
</TR>
<TR>
<TD><A HREF="http://java.sun.com/casestudies"><IMG SRC="/images/side.tab.case.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="How Java Technology is Used Worldwide"></A></TD>
</TR>
<TR><TD> </TD></TR>
<!-- End NavBar Insert -->
<!-- START SUB-NAV -->
<TR>
<TD><!-- INSERT SUB-NAV INFO -->
</TD>
</TR>
<!-- END SUB-NAV -->
</TABLE>
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0">
<TR>
<TD>
<!-- Template Version 2.0 -->
<!-- ================== -->
<!-- Start Main Content -->
<!-- ================== -->
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0">
<TR>
<TD VALIGN="TOP">
<FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A NAME="top"></A>
<DIV ALIGN="RIGHT">
<FONT SIZE="-1"><A HREF="/developer/onlineTraining/index.html">Training Index</A></FONT>
<H2>Writing Advanced Applications<BR>
<FONT SIZE="3">Chapter 7 Continued: Collecting Evidence</FONT></H2>
<FONT SIZE="-1">[<A HREF="debug.html"><<BACK</A>] [<A HREF="index.html#contents">CONTENTS</A>] [<A HREF="running.html">NEXT>></A>]</FONT></DIV>
<P>
The first step in trying to solve any problem is to gather as much
evidence and information as possible. If you can picture a crime scene,
you know that everything is checked, cataloged and analyzed before any
conclusions are reached. When debugging a program, you do not have weapons,
hair samples, or fingerprints, but there is plenty of evidence you can
gather that might contain or ultimately lead to the solution. This section
explains how to gather that evidence.
<UL>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#install">Installation and Environment</A></FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#path">Class Path</A></FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#load">Class Loading</A></FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#debug">Including Debug Code</A> </FONT>
</UL>
<HR>
<A NAME="install"></A>
<H3>Installation and Environment</H3>
The Java<FONT SIZE="-2"><SUP>TM</SUP></FONT> platform is a fast-moving and
changing technology. You might have more than one release installed
on your system, and those releases might have been installed as part
of another products installation. In an environment with mixed releases,
a program
can experience problems due to changes to the platform in a new version
or release.
<P>
For example, if classes, libraries, or Windows registry entries from previous
installations remain on your system after an upgrade, there is
a chance the new software mix is causing your problems and needs to be
investigated and ruled out. Opportunities for problems related to mixed
software releases have increased with the use of
different release tools to deliver the Java platform software.
<P>
The section on
<A HREF="version.html">Version Issues</A> at the end of this chapter
provides a complete list of major
Java platform release and version information to help you rule out software
release issues. This next section highlights the most common problems you are
likely to encounter.
<A NAME="path"></A>
<H3>Class Path</H3>
<P>
In the Java 2 platform, the <CODE>CLASSPATH</CODE> environment variable is
needed to specify the application's own classes only,
and not the Java platform classes as was required in earlier
releases. So it is possible your <CODE>CLASSPATH</CODE> environment
variable is pointing at Java platform classes from earlier
releases and causing problems.
<P>
To examine the <CODE>CLASSPATH</CODE>, type the following at the
command line:
<P>
<STRONG>Windows 95/98/NT:</STRONG><BR>
<TT>echo %CLASSPATH%</TT>
<P>
<STRONG>Unix Systems:</STRONG><BR>
<TT>echo $CLASSPATH</TT>
<P>
Java classes are loaded on a first come, first served basis from the
<CODE>CLASSPATH</CODE> list.
If the <CODE>CLASSPATH</CODE> variable contains a reference to a
<CODE>lib/classes.zip</CODE> file, which in turn points to a different
Java platform installation, this can cause incompatible classes to be
loaded.
<BLOCKQUOTE>
<HR>
<STRONG>Note:</STRONG>
In the Java 2 platform, the system classes are chosen
before any class on the <CODE>CLASSPATH</CODE> list to minimize the
possibility of any old broken Java classes being loaded instead of a
Java 2 class of the same name.
<HR>
</BLOCKQUOTE>
<P>
The <CODE>CLASSPATH</CODE> variable can get its settings from
the command line or from configuration settings such as those
specified in the User Environment on Windows NT, an
<CODE>autoexec.bat</CODE> file, or a shell startup file like
<CODE>.cshrc</CODE> on Unix.
<P>
You can control the classes the Java<A HREF="#TJVM"><SUP>1</SUP></A> Virtual Machine (VM) uses
by compiling your program with a special command-line option
that lets you supply the <CODE>CLASSPATH</CODE> you want.
The Java 2 platform option and parameter is
<CODE>-Xbootclasspath classpath</CODE>, and earlier releases
use <CODE>-classpath classpath</CODE> and
<CODE>-sysclasspath classpath</CODE>.
Regardless of which release you are running,
the <CODE>classpath</CODE> parameter specifies the system and user
classpath, and zip or Java ARchive (JAR) files to be used
in the compilation.
<P>
To compile and run the <CODE>Myapp.java</CODE> program with a system
<CODE>CLASSPATH</CODE> supplied on the command line,
use the following instructions:
<P>
<STRONG><STRONG>Windows 95/98/NT:</STRONG><BR></STRONG>
<P>
In this example, the Java platform is installed in the
<CODE>C:\java</CODE> directory. Type everything on one line:
</FONT>
<PRE>
javac -J-Xbootclasspath:c\java\lib\tools.jar;c:
\java\jre\lib\rt.jar;c:\java\jre\lib\i18n.jar;.
Myapp.java
</PRE>
<FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<P>
You do not need the <CODE>-J</CODE> runtime flag to run the
compiled <CODE>Myapp</CODE> program, just type the following
on one line:
</FONT>
<PRE>
java -Xbootclasspath:c:\java\jre\lib\rt.jar;c:
\java\jre\lib\i18n.jar;. Myapp
</PRE>
<FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<P>
<STRONG>Unix Systems:</STRONG>
<P>
In this example, the Java platform is installed in the
<CODE>/usr/local/java</CODE> directory. Type everything on one line:
</FONT>
<PRE>
javac -J-Xbootclasspath:/usr/local/java/lib/tools.jar:
/usr/local/java/jre/lib/rt.jar:
/usr/local/java/jre/lib/i18n.jar:. Myapp.java
</PRE>
<FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<P>
You do not need the <CODE>-J</CODE> runtime flag to run the
compiled <CODE>Myapp</CODE> program, just type the following
on one line:
</FONT>
<PRE>
java -Xbootclasspath:/usr/local/java/jre/lib/rt.jar:
/usr/local/java/jre/lib/i18n.jar:. Myapp<BR>
</PRE>
<FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A NAME="load"></A>
<H3>Class Loading</H3>
Another way to analyze <CODE>CLASSPATH</CODE> problems
is to locate where your application is loading its classes.
The <CODE>-verbose</CODE> option to the <CODE>java</CODE> command shows
which <CODE>.zip</CODE> or <CODE>.jar</CODE> file a class comes from when
it is loaded. This way, you will be able to tell if it came from the Java
platform zip file or from some other application's JAR file.
<P>
For example, an application might be using the
<CODE>Password</CODE> class you wrote for it or it might
be loading a <CODE>Password</CODE> class
from an installed integrated development environment (IDE) tool.
<P>
You should see each jar and zip file named as in the example below:
</FONT>
<PRE>
<FONT SIZE=-1>
$ java -verbose SalesReport
[Opened /usr/local/java/jdk1.2/solaris/jre/lib/rt.jar
in 498 ms]
[Opened /usr/local/java/jdk1.2/solaris/jre/lib/i18n.jar
in 60 ms]
[Loaded java.lang.NoClassDefFoundError from
/usr/local/java/jdk1.2/solaris/jre/lib/rt.jar]
[Loaded java.lang.Class from
/usr/local/java/jdk1.2/solaris/jre/lib/rt.jar]
[Loaded java.lang.Object from
/usr/local/java/jdk1.2/solaris/jre/lib/rt.jar]
</FONT>
</PRE>
<FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A NAME="debug"></A>
<H3>Including Debug Code</H3>
A common way to add diagnostic code to an application is to use
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -