⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 collect.html

📁 jdbc书
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<CODE>System.out.println</CODE> statements at strategic locations
in the application. This technique is fine during development, providing 
you remember to remove them all when you release your product. However, 
there are other approaches that are just as simple, do not affect the 
performance of your application, and do not display messages that you do 
not want your customers to see. The following are two techniques that 
overcome the problems with simple <CODE>System.out.println</CODE> statements.

<P>
<H3>Turning Debug Information On at Runtime</H3>

<P>
The first alternative to the classic <CODE>println</CODE> debug statements
is to turn on debugging information at runtime. One advantage to this is 
you do not need to recompile any code if problems appear at the testing 
stage or on a customer site. 
<p>
Another advantage is that sometimes software
problems can be attributed to race conditions where the same segment
of code behaves unpredictably due to timing between other program 
interactions.  If you control your debug code from the command line instead
of adding <CODE>println</CODE> debug statements, you can
rule out sequence problems caused by race conditions coming
from the <CODE>println</CODE> code. This technique also saves you 
adding and removing <CODE>println</CODE> debug statements and
having to recompile your code.   

<P>
This technique requires you to use a system property as a debug flag
and include application code to test that system property value.
To turn on debug information from the command line at run time, 
start the application and set 
the debug system property to <CODE>true</CODE> as follows:

<P>
<CODE>java -Ddebug=true TestRuntime</CODE>

<P>
The source code for the <CODE>TestRuntime</CODE> class needs to examine 
this property and set the debug boolean flag as follows:


</FONT>

<PRE><FONT SIZE="-1">
public class TestRuntime {
    boolean debugmode; //global flag that we test

    public TestRuntime () {

       String dprop=System.getProperty("debug");

       if ((dprop !=null) &amp;&amp; (dprop.equals("yes"))){
            debugmode=true;
       }

       if (debugmode) {
           System.err.println("debug mode!");
       }
    }
}
</FONT>
</PRE>

<FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<P>
<H3>Creating Debug and Production Releases at Compile Time</H3>

<P>
As mentioned earlier, one problem with adding 
<CODE>System.out.println</CODE> debug statements to your code is 
finding and removing them before you release the product.
Apart from adding unnecessary code, <CODE>println</CODE> debug 
statements can contain information you do not want your customers to 
see.

<P>
One way to remove <CODE>System.out.println</CODE> debug statements
from your code is to use the following compiler optimization to 
remove pre-determined branches from your code at compile time and achive
something similar to a debug pre-processor. 

<P>
This example uses a static <CODE>dmode</CODE>
boolean flag that when set to <CODE>false</CODE>
results in the debug code and the debug test statement being removed.
When the <CODE>dmode</CODE> value is set to <CODE>true</CODE>, 
the code is included in the compiled class file and is available
to the application for debugging purposes.

</FONT>

<PRE>
<FONT SIZE="-1">
class Debug {

  //set dmode to false to compile out debug code
  public static final boolean dmode=true;
}

public class TestCompiletime {

  if (Debug.dmode) {                       // These 
    System.err.println("Debug message");   // are 
    }                                      // removed
}
</FONT>
</PRE>

<FONT FACE="Verdana, Arial, Helvetica, sans-serif">

<P>
<H3>Using Diagnostic Methods</H3>

You can use diagnostic methods to request debug information from the
Java VM. The following two
methods from the <CODE>Runtime</CODE> class trace the method calls and Java VM 
byte codes your application uses. As both these methods produce a lot of output,
it is best to trace very small amounts of code, even as little as one
line at a time. 

<P>
To enable trace calls so you will see the output, you have
to start the Java VM with the
<CODE>java_g</CODE> or <CODE>java -Xdebug</CODE> interpreter commands.

<P>
To list every method as it is invoked at runtime, 
add the following line before the code you wish to start tracing and add
a matching <CODE>traceMethodCalls</CODE> line with the argument set to false to turn the tracing off. The tracing information is displayed on the standard output.

</FONT>

<PRE>
// set boolean argument to false to disable
Runtime.getRuntime().traceMethodCalls(true);
callMyCode();
Runtime.getRuntime().traceMethodCalls(false);
</PRE>

<FONT FACE="Verdana, Arial, Helvetica, sans-serif">

To see each line as bytecodes as they are executed, add
the following line to your application code:

</FONT>

<PRE>
// set boolean argument to false to disable
Runtime.getRuntime().traceInstructions(true);
callMyCode();
Runtime.getRuntime().traceInstructions(false);
</PRE>

<FONT FACE="Verdana, Arial, Helvetica, sans-serif">

<P>
You can also add the following line to your application to
dump your own stack trace using the <CODE>dumpStack</CODE>
method from the <CODE>Thread</CODE> class. The output from a stack trace 
is explained in 
<A HREF="stack.html">Analyzing Stack Traces</A>, but for now you can think
of a stack trace as a snapshot of the
current threads running in the Java VM.

</FONT>

<PRE>
Thread.currentThread().dumpStack();
</PRE>

<FONT FACE="Verdana, Arial, Helvetica, sans-serif">

<H3>Adding Debug Information</H3>

Local variable information is not included in the core
Java platform system classes. So, if you use a debug tool
to list local variables for system classes where you place
<CODE>stop</CODE> commands, you will get the following
output, even when you compile with the <CODE>-g</CODE> flag
as suggested by the output. This output is from a <CODE>jdb</CODE> 
session:

</FONT>

<PRE>
main[1] locals
No local variables: try compiling with -g
</PRE>

<FONT FACE="Verdana, Arial, Helvetica, sans-serif">

<P>
To get access to the local variable information, you have to obtain the 
source (<CODE>src.zip</CODE> or <CODE>src.jar</CODE>)  and recompile it 
with a <CODE>debug</CODE> flag.  
You can get the source for most java.* classes with the binary
downloads from <A HREF="http://java.sun.com">java.sun.com</A>. 

<P>
Once you download the <CODE>src.zip</CODE> or <CODE>src.jar</CODE> file, extract only the files you need. 
For example, to extract the <CODE>String</CODE> class, type the
following at the command line:

</FONT>

<PRE>
unzip /tmp/src.zip src/java/lang/String.java
</PRE>

<FONT FACE="Verdana, Arial, Helvetica, sans-serif">
or<p>
</FONT>

<PRE>
jar -xf /tmp/src.jar src/java/lang/String.java
</PRE>

<FONT FACE="Verdana, Arial, Helvetica, sans-serif">

<P>
Recompile the extracted class or classes with the <CODE>-g</CODE> option. 
You could also add your own additional diagnostics to the source file at this 
point.

</FONT>

<PRE>
javac -g src/java/lang/String.java
</PRE>

<FONT FACE="Verdana, Arial, Helvetica, sans-serif">

<BLOCKQUOTE>
<HR>
The Java 2 <CODE>javac</CODE> compiler gives you more options than just the
original <CODE>-g</CODE> option for debug code, and you can reduce the size
of your classes by using <CODE>-g:none</CODE>, which gives you
on average about a 10 percent reduction in size.
<HR>
</BLOCKQUOTE>

<P>
To run the application with the newly compiled debug class or classes,
you need to
use the <CODE>bootclasspath</CODE> option so these new classes
are picked up first. 

<P>
Type the following on one line with a space before <CODE>myapp</CODE>. 

<P>
<STRONG>Win95/NT Java 2 Platform:</STRONG>
<P>
This example assumes the Java platform is installed in <CODE>c:\java</CODE>,
and the source files are in <CODE>c:\java\src</CODE>:

</FONT>

<PRE>
jdb -Xbootclasspath:c:\java\src;c:\java\jre\lib\rt.jar;c:
\java\jre\i18n.jar;.  myapp
</PRE>

<FONT FACE="Verdana, Arial, Helvetica, sans-serif">

<P>
<STRONG>Unix Systems:</STRONG>
<P>
This example assumes the Java platform is installed in 
<CODE>c:\java</CODE>, and the source files are in
<CODE>c:\java\src</CODE>.

</FONT>

<PRE>
jdb -Xbootclasspath:/usr/java/src;
/usr/java/jre/lib/rt.jar;
/usr/java/jre/i18n.jar;.  myapp
</PRE>

<FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<P>
The next time you run the <CODE>locals</CODE> command you will see
the internal fields of the class you wish to analyze.


<P ALIGN="RIGHT">
<FONT SIZE="-1">[<A HREF="#top">TOP</A>]</FONT>

<P>
_______<BR>
<A NAME="TJVM"><SUP>1</SUP></A> As used on this web site, 
the terms &quot;Java virtual 
machine&quot; or &quot;JVM&quot; mean a virtual machine 
for the Java platform.

</FONT>
</TD>
</TR>
</TABLE>



<!-- ================ -->
<!-- End Main Content -->
<!-- ================ -->

</TD>
</TR>
</TABLE>

<!-- Copyright Insert -->

<BR CLEAR="ALL">

<FORM ACTION="/cgi-bin/search.cgi" METHOD="POST">
<TABLE WIDTH="100%" CELLPADDING="0" BORDER="0" CELLSPACING="5">   
  <TR>
    <TD VALIGN="TOP">
	
    <P ALIGN=CENTER>
    <FONT SIZE="-1" COLOR="#999999" FACE="Verdana, Arial, Helvetica, sans-serif">
    [ This page was updated: <!-- new date --> 13-Oct-99 ]</font></P>
    </TD>
  </TR>
  
  <TR>
    <TD BGCOLOR="#CCCCCC">
    <IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD>
  </TR>
  
  <TR>
    <TD>
    <CENTER>
    <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">
    <A HREF="http://java.sun.com/products/">Products &amp; APIs</A> | 
    <A HREF="/developer/index.html">Developer Connection</A> | 
    <A HREF="/developer/infodocs/index.shtml">Docs &amp; Training</A> | 
    <A HREF="/developer/support/index.html">Online Support</A><BR>
    <A HREF="/developer/community/index.html">Community Discussion</A> |
    <A HREF="http://java.sun.com/industry/">Industry News</A> | 
    <A HREF="http://java.sun.com/solutions">Solutions Marketplace</A> | 
    <A HREF="http://java.sun.com/casestudies">Case Studies</A>
    </FONT>
    </CENTER>
    </TD>
  </TR>
  
  <TR>
    <TD BGCOLOR="#CCCCCC">
    <IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD>
  </TR>

  <TR>
    <TD ALIGN="CENTER">
    <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">
    <A HREF="http://java.sun.com/docs/glossary.html">Glossary</A> - 
    <A HREF="http://java.sun.com/applets/">Applets</A> - 
    <A HREF="http://java.sun.com/docs/books/tutorial/">Tutorial</A> - 
    <A HREF="http://java.sun.com/jobs/">Employment</A> - 
    <A HREF="http://java.sun.com/nav/business/">Business &amp; Licensing</A> - 
    <A HREF="http://java.sun.com/javastore/">Java Store</A> -
    <A HREF="http://java.sun.com/casestudies/">Java in the Real World</A>
    </FONT>
    </TD>
  </TR>

  <TR>
    <TD>
    <CENTER>
    <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">
    <a href="/siteinfo/faq.html">FAQ</a> |
    <a href="/feedback/index.html">Feedback</a> | 
    <a href="http://www.dynamicdiagrams.net/mapa/cgi-bin/help.tcl?db=javasoft&dest=http://java.sun.com/">Map</a> | 
    <A HREF="http://java.sun.com/a-z/index.html">A-Z Index</A>
    </FONT>
    </CENTER>

    </TD>
  </TR>
  
  <TR>
    <TD>

    <TABLE WIDTH="100%" CELLPADDING="0" BORDER="0" CELLSPACING="0">
      <TR>
        <TD WIDTH="50%">
        <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">
        For more information on Java technology<BR>
        and other software from Sun Microsystems, call:<BR>
        </FONT>
        <FONT SIZE="-1" FACE="Verdana, Arial, Helvetica, sans-serif">
        (800) 786-7638<BR></FONT>
        <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">
        Outside the U.S. and Canada, dial your country's 
        <A HREF="http://www.att.com/business_traveler/attdirecttollfree/">AT&amp;T&nbsp;Direct&nbsp;Access&nbsp;Number</A> first.<BR>
        </FONT>
        </TD>

        <TD ALIGN="RIGHT" WIDTH="50%">
        <A HREF="http://www.sun.com"><IMG SRC="/images/lgsun.gif" width="64" height="30" border="0" ALT="Sun Microsystems, Inc."></A><BR>
        <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">
        Copyright &copy; 1995-99
        <A HREF="http://www.sun.com">Sun Microsystems, Inc.</A><BR>
        All Rights Reserved. 
        <a href="http://www.sun.com/share/text/SMICopyright.html">Legal Terms</a>. 
        <A HREF="http://www.sun.com/privacy/">Privacy&nbsp;Policy</A>.
        </FONT>
        </TD>
      </TR>
    </TABLE>
	
    </TD>
  </TR> 
</TABLE>
</FORM>

<!-- End Copyright Insert -->


</BODY>
</HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -