What is a stack trace, and how can I use it to debug my application errors?

Sometimes when I run my application it gives me an error that looks like: Exception in thread "main" java.lang.NullPointerException at com.example.myproject.Book.getTitle(Book.java:16) at com.example.myproject.Author.getBookTitles(Author.java:25) at com.example.myproject.Bootstrap.main(Bootstrap.java:14) People have referred to this as a "stack trace". What is

什么是堆栈跟踪,以及如何使用它来调试我的应用程序错误?

有时当我运行我的应用程序时,它会给我一个如下所示的错误: Exception in thread "main" java.lang.NullPointerException at com.example.myproject.Book.getTitle(Book.java:16) at com.example.myproject.Author.getBookTitles(Author.java:25) at com.example.myproject.Bootstrap.main(Bootstrap.java:14) 人们称之为“堆栈跟踪”。 什么是堆栈跟踪? 有什么可以告诉我关于我的程序中发生的错误

How can I convert a stack trace to a string?

将Throwable.getStackTrace()的结果转换为描述堆栈跟踪的字符串的最简单方法是什么? One can use the following method to convert an Exception stack trace to String . This class is available in Apache commons-lang which is most common dependent library with many popular open sources org.apache.commons.lang.exception.ExceptionUtils.getStackTrace(Throwable) 使用Throwable.printStackTrace(PrintWriter p

如何将堆栈跟踪转换为字符串?

将Throwable.getStackTrace()的结果转换为描述堆栈跟踪的字符串的最简单方法是什么? 可以使用以下方法将Exception堆栈跟踪转换为String 。 这个类在Apache commons-lang中可用,这是最常见的依赖库,有许多流行的开源 org.apache.commons.lang.exception.ExceptionUtils.getStackTrace(Throwable) 使用Throwable.printStackTrace(PrintWriter pw)将堆栈跟踪发送到适当的写入器。 import java.io.StringWriter; import java.

Rethrowing exceptions in Java without losing the stack trace

In C#, I can use the throw; statement to rethrow an exception while preserving the stack trace: try { ... } catch (Exception e) { if (e is FooException) throw; } Is there something like this in Java ( that doesn't lose the original stack trace )? catch (WhateverException e) { throw e; } will simply rethrow the exception you've caught (obviously the surrounding method h

在Java中重新抛出异常而不会丢失堆栈跟踪

在C#中,我可以使用throw; 语句在保留堆栈跟踪的同时重新抛出异常: try { ... } catch (Exception e) { if (e is FooException) throw; } 在Java中是否有这样的东西( 不会丢失原始堆栈跟踪 )? catch (WhateverException e) { throw e; } 将简单地重新抛出您捕获的异常(显然周围的方法必须通过其签名等来允许)。 异常将保持原始堆栈跟踪。 我会比较喜欢: try { ... } catch (FooException fe){

What does JVM flag CMSClassUnloadingEnabled actually do?

I cannot for the life of me find a definition of what the Java VM flag CMSClassUnloadingEnabled actually does, other than some very fuzzy high-level definitions such as "gets rid of your PermGen problems" (which it doesn't, btw). I have looked on Sun's/Oracle's site, and even the options list doesn't actually say what it does. Based upon the name of the flag, I'm

JVM标志CMSClassUnloadingEnabled实际上做了什么?

除了一些非常模糊的高级定义,例如“摆脱PermGen问题”(它不会,顺便说一句),我无法在我的生活中找到Java VM标志CMSClassUnloadingEnabled实际所做的定义。 我查看了Sun的/ Oracle的网站,甚至连选项列表也没有说明它的功能。 根据国旗的名称,我猜测CMS垃圾收集器默认情况下不会卸载类,并且此标志将其打开 - 但我无法确定。 更新此答案与Java 5-7相关,Java 8已修复此问题:https://blogs.oracle.com/poonam/about-g1-

Hidden Features of Java

在阅读C#隐藏特性之后,我想知道,Java的一些隐藏特性是什么? Double Brace Initialization took me by surprise a few months ago when I first discovered it, never heard of it before. ThreadLocals are typically not so widely known as a way to store per-thread state. Since JDK 1.5 Java has had extremely well implemented and robust concurrency tools beyond just locks, they live in java.util.concurre

Java的隐藏特性

在阅读C#隐藏特性之后,我想知道,Java的一些隐藏特性是什么? 几个月前,当我第一次发现它时,Double Brace Initialization让我大吃一惊,之前从未听说过它。 通常ThreadLocals并不是广为人知的存储每个线程状态的方式。 由于JDK 1.5 Java具有非常好的实现且强大的并发工具,不仅仅是锁,它们存在于java.util.concurrent中,并且一个特别有趣的示例是java.util.concurrent.atomic子包,其中包含实现比较的线程安全基元并且

What are Dependency Injection & Spring Framework about?

Possible Duplicates: What is dependency injection? What exactly is Spring for? I want to know What is Spring Framework? Why and when should one use it in Java Enterprise development? The answer would be "A dependency injection framework". All right, what advantages do we have when using dependency injection frameworks? The idea of describing classes with setter values and/or c

什么是依赖注入和Spring框架?

可能重复: 什么是依赖注入? 春天到底是什么? 我想知道什么是Spring Framework? 为什么和什么时候应该在Java Enterprise开发中使用它? 答案是“依赖注入框架”。 好的,使用依赖注入框架时我们有什么优势? 用setter值和/或构造函数参数来描述类的想法对我来说似乎很陌生。 为什么这样做? 因为我们可以在不重新编译项目的情况下更改属性? 这就是我们所得到的一切吗? 那么,我们应该在beans.xml中描述哪些对

What is Dependency Injection?

Possible Duplicate: What is dependency injection? Spring is the framework from where the concept Dependency Injection came to picture. What is purpose of DI ? How does it benefit ? How is it implemented ? Start here. Also see A-beginners-guide-to-Dependency-Injection. Elsewhere on SO: what-is-dependency-injection dependency-injection-vs-factory-pattern when-to-use-dependency-i

什么是依赖注入?

可能重复: 什么是依赖注入? Spring是依赖注入概念出现的框架。 DI的目的是什么? 它如何受益? 它是如何实现的? 从这里开始。 另请参阅A-beginners-guide-to-Dependency-Injection。 在其他地方: 什么,是依赖注入 依赖喷射VS-工厂图案 当使用的依赖性注入 差依赖性之间-喷射和反转的控制 DI的目的是什么? 依赖注入的目的是为了减少应用程序中的耦合,使其更灵活,更易于测试。 它如何受益?

Benefit of dependency injection for dynamically created objects

In the context of an IoC container such as Spring, I am looking for a way to inject some dependencies/properties into a class' instantiation. Not all properties of the object can be set using dependency injection, and the object is created dynamically in response to an application event. If all dependencies can be injected via the container, then a Spring managed bean would be ideal. For

动态创建对象的依赖注入的好处

在Spring这样的IoC容器的上下文中,我正在寻找一种方法将一些依赖关系/属性注入到类的实例化中。 并非所有对象的属性都可以使用依赖注入来设置,并且对象是响应应用程序事件而动态创建的。 如果所有依赖可以通过容器注入,那么Spring管理bean将是理想的。 例如,下面定义的类必须注释为@Component (或更专门的注释),以便组件扫描和依赖注入工作。 但它有几个属性( name和attempts ),只能由应用程序代码动态设置,而不

How to inject a Mock in a Spring Context

This question already has an answer here: Injecting Mockito mocks into a Spring bean 23 answers Yes, you are on the right track, putting a mock @Bean in a @Configuration class is one approach, and I'll describe my experience: The trick is that you need to use a different set of .xml files purely for testing which exclude the live versions of those beans. @ContextConfiguration(locations

如何在Spring上下文中注入模拟

这个问题在这里已经有了答案: 将Mockito嘲笑注入Spring bean 23回答 是的,你在正确的轨道上,在@Configuration类中放置一个模拟@Bean是一种方法,我将描述我的经验: 诀窍是,您需要纯粹使用一组不同的.xml文件进行测试,这些文件不包括这些bean的实时版本。 @ContextConfiguration(locations = {"context1-test.xml", "context2-test.xml", ...}) 并且“-test-xml”文件进入src/test/resources 。 至少那是我在做同样

Role/Purpose of ContextLoaderListener in Spring?

I am learning Spring Framework which is being used in my project. I found the ContextLoaderListener entry in my web.xml file. But could not figure out how exactly it helps a developer? In the official documentation of ContextLoaderListener it says it is to start WebApplicationContext. Regarding WebApplicationContext JavaDocs say: Interface to provide configuration for a web application.

ContextLoaderListener在Spring中的角色/用途?

我正在学习我的项目中使用的Spring Framework。 我在我的web.xml文件中找到了ContextLoaderListener条目。 但无法弄清楚它对开发人员有何帮助? 在ContextLoaderListener的官方文档中,它表示它将启动WebApplicationContext。 关于WebApplicationContext JavaDocs说: 提供Web应用程序配置的接口。 但是我无法理解我用ContextLoaderListener实现的内部初始化WebApplicationContext的功能? 根据我的理解 ,ContextLoa