文档章节

idea调试第一个java小程序

maketc
 maketc
发布于 2017/07/20 15:57
字数 713
阅读 66
收藏 0

上一篇创建了,这一篇肯定要调试啊,呵呵

 

Before you start...

You have already created and executed your first Java application. Now it's time todebug it.

However, it would be nice to add one more line to the application - let it be

System.out.println("it's me, Wombat!");Copy

Putting breakpoints

To start a debugger session, first of all you need to place a breakpoint at the statement where you want to suspend the execution of your application. The existing source code does not give you much of a choice - the only place, where you can put breakpoints, are the statements

System.out.println("Hello World!"); System.out.println("it's me, Wombat!");Copy

So let's do it. Click the left gutter at the line of the statement you want to put a breakpoint on, or just press Ctrl+F8:

/help/img/idea/2017.2/HWDebug_create_breakpoint.png

As you see, the new breakpoint is added to the source code. The line where the breakpoint is set changes its color to pink.

If you just hover your mouse pointer over the breakpoint, you will see its properties in the tooltip:

/help/img/idea/2017.2/HWDebug_view_breakpoint.png

Suppose you want to change some properties of this breakpoint. Then right-click it and see the following dialog box:

/help/img/idea/2017.2/HWDebug_breakpoint_properties.png

Finally, you would like to explore and change all the available properties of a breakpoint and see its location among the other breakpoints (if any). In this case, press Ctrl+Shift+F8:

/help/img/idea/2017.2/HWDebug_breakpoint_properties_full.png

Starting a debugger session

Now that the breakpoints are added, you can debug your application. This can be done in numerous ways; however, let's follow the easiest one.

Mind the icon /help/img/idea/2017.2/left_gutter_run.png that marks a class with the main() method. Clicking this icon reveals a menu that enables running and debugging such a class:

/help/img/idea/2017.2/HWDebug_start_session.png

What does it mean?

IntelliJ IDEA launched the debugger session with the temporary run/debug configuration. This run/debug configuration has the default name "HelloWorld.main()'. To view and change the settings of this run/debug configuration, choose Run | Edit Configurations... on the main menu:

/help/img/idea/2017.2/ij_runConfigMenu.png

or click the run/debug configuration selector, and then choose Edit Configurations...:

/help/img/idea/2017.2/HWDebug_runconfig.png

IntelliJ IDEA compiles your application (which takes time!), and then suspends the application at the first breakpoint.

The IntelliJ IDEA window now looks different:

  • The first thing that has changed is the color of the first line with the breakpoint. Now this line is rendered blue:

    /help/img/idea/2017.2/HWDebug_breakpoint_hit.png

    It means that (according to the breakpoint properties) the application has reached this breakpoint, hit it and suspended before the statementprintln.

  • Next, in the lower part of the IntelliJ IDEA window, a special tool window has emerged. This is the Debug tool windowthat features the stepping toolbar and shows all the necessary information you might need during the debugger session:

    /help/img/idea/2017.2/HWDebug_toolwindow.png

Stepping through the application

Stepping through the statements directly

Let's step through the application. Click /help/img/idea/2017.2/frames_step_over.pngon the stepping toolbar, or just press F8.

The next line now becomes blue. If you look at the Debug tool window, you notice the following changes:

  • In the Frames pane, the next line number is shown.
  • The Console tab is marked with the icon /help/img/idea/2017.2/console_info_icon.png, which means that it contains new output.

/help/img/idea/2017.2/HWDebug_breakpoint2_hit.png

Click the Console tab. You see the message of the first line with the breakpoint "Hello, World!". The second message is not yet visible:

/help/img/idea/2017.2/HWDebug_console.png

Click /help/img/idea/2017.2/frames_step_over.png again on the stepping toolbar, or press F8. Now the second message appears in the console. After you click /help/img/idea/2017.2/frames_step_over.pngthe next time, the application stops:

/help/img/idea/2017.2/HWDebug_console2.png

This debugging session is over.

Stepping through the method calls

Now let's explore a more complicated way and step into the println() call.

First, restart the debugger session. To do that, just click /help/img/idea/2017.2/debug.png on the toolbar of theDebug tool window. Thus you'll rerun the latest run/debug configuration, namely, HelloWorld.

The application again pauses at the first breakpoint. This time, click /help/img/idea/2017.2/frames_force_step_into.png, or press Shift+Alt+F7. You see a different picture:

/help/img/idea/2017.2/HWDebug_step_into_class.png

It means that IntelliJ IDEA has stepped into the method println(String x) of the library class PrintStream.java. Note that a new thread appears in the list of threads.

Click /help/img/idea/2017.2/frames_step_out.png or press Shift+F8 to return to the next breakpoint:

/help/img/idea/2017.2/HWDebug_breakpoint3_hit.png

Note that the Console tab has again got the marker /help/img/idea/2017.2/console_info_icon.png, which means that new output is available. Next, click /help/img/idea/2017.2/frames_step_over.png. You see that the process terminates:

/help/img/idea/2017.2/HWDebug_application_stop.png

© 著作权归作者所有

maketc
粉丝 3
博文 75
码字总数 25566
作品 0
渝北
程序员
私信 提问
IntelliJ IDEA 2019.1 发布 RC 候选版

IntelliJ IDEA 2019.1 首个候选版已发布,不过各位不用急着升级,因为官方表示计划于下周发布 2019.1 正式版。 正式版和候选版的功能并无差别,不妨就以候选版为例看看 2019.1 中值得关注的新...

局长
03/22
0
21
IntelliJ IDEA 2019.1 正式发布,“JetBrains 全家桶”齐更新

IntelliJ IDEA 2019.1 现已正式发布,作为今年的首个重大更新版本,2019.1 带来了许多新功能和改进,下面不妨来看看主要概述。 可通过官网下载 IntelliJ IDEA 2019.1 的最新安装包来升级,也...

局长
03/28
0
101
IntelliJ IDEA 10.0 64位运行方法

IntelliJ IDEA 10.0 默认的快捷方式是以32位运行的.虽然性能上面感觉不出什么变化,但让人心里不很爽.IDEA是JAVA程序,如果我们直接从JAVA命令运行的话那就默认是64位了.还好,IDEA支持以命令调...

SeanCai
2010/12/19
0
1
探秘IntelliJ IDEA 13测试版新功能——调试器显示本地变量

IntelliJ IDEA在业界被公认为最好的Java开发平台之一,JetBrains公司将在12月正式发布IntelliJ IDEA 13版本。 现在,小编将和大家一起探秘密IntelliJ IDEA 13测试版本新功能——调试器显示本...

kouxunli1
2013/10/23
0
2
IntelliJ IDEA 2018.1.3 + jdk1.8 安装教程

1.写在前面 由于同学的项目缺人手,欲拉我参与其中,该项目由java编写,所以开始了我的java学习。当然首先是安装java环境啦。 2.安装IDEA 由于这个是收费的,我等穷逼哪搞得起。只能破解使用...

我只是一只小小鸟
2018/05/21
0
0

没有更多内容

加载失败,请刷新页面

加载更多

Java编程学习-Java虚拟机(JVM)以及跨平台原理

相信大家已经了解到Java具有跨平台的特性,可以“一次编译,到处运行”,在Windows下编写的程序,无需任何修改就可以在Linux下运行,这是C和C++很难做到的。 那么,跨平台是怎样实现的呢?这...

Java领航员
27分钟前
0
0
学JFinal不迷路,JFinal优质资源列表(欢迎反馈更新)

学JFinal不迷路,记录一下JFinal相关的资源、产品、讲师等信息(所有信息排名不分先后)。 一、相关站点: 1、JFinal官网-问答、分享、文档、交流、俱乐部 http://www.jfinal.com 2 、JFina...

山东-小木
31分钟前
0
0
项目学习(2)-order-job

在当前的系统中,因为并发量并不高,服务之间发起异步请求或者异步调用时,没有使用到消息中间件。 而是在各个服务(子系统)的数据库中,创建了event_queue事件对列表和event_handler事件处...

太猪-YJ
40分钟前
1
0
gradle grovvy中的闭包

1. 无参数的闭包 //这b1就是一个闭包def b1={ println "hello b1"}//定义方法,包含闭包类型的参数def method1(Closure closure){closure()}//执行method1method1(b1) 执行结果 ...

edison_kwok
今天
3
0
基于Spring Boot + Dubbo的全链路日志追踪(一)

一、 概要 当前公司后端整体架构为:Spring Boot + Dubbo。由于早期项目进度等原因,对日志这块没有统一的规范,基本上是每个项目自己管自己的日志。这也对后面的问题排查带来了很大的困难,...

明天以后
今天
7
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部