简体   繁体   English

System.out.println() 不会打印到 Eclipse 中的控制台

[英]System.out.println() does not print to the console in Eclipse

I am developing a test automation script using Cucumber, Selenium, and JUnit in Java.我正在使用 Java 中的 Cucumber、Selenium 和 JUnit 开发测试自动化脚本。 To quickly detect any issues with my custom code, I am placing System.out.println("success") and System.err.println("failure") in certain places.为了快速检测我的自定义代码的任何问题,我在某些地方放置了System.out.println("success")System.err.println("failure") The problem is that neither print to the console in Eclipse.问题是在 Eclipse 中都没有打印到控制台。 I've done this hundreds of times all though not necessarily with these tools.尽管不一定使用这些工具,但我已经完成了数百次。 I suspect either Cucumber or JUnit is the culprit, but I couldn't find anything confirming this after some Google queries.我怀疑 Cucumber 或 JUnit 是罪魁祸首,但在进行了一些 Google 查询后,我找不到任何证实这一点的信息。

I did see this:我确实看到了这个:

System.out.println doesn't print anything inside eclipse console System.out.println 不在 Eclipse 控制台内打印任何内容

But that isn't the problem.但这不是问题。 None of my consoles are displaying my println()'s.我的控制台都没有显示我的 println() 。

UPDATE What I've tried so far:更新到目前为止我尝试过的:

  1. PrintStream out = System.out; System.setOut(out); System.out.println("hello");
  2. Eclipse Console not showing output Eclipse 控制台不显示输出
  3. Explicitly throw an exception (resulted in no stack trace in console)显式抛出异常(导致控制台中没有堆栈跟踪)

So it turns out there is a bug with the version of Eclipse I was using (Eclipse Oxygen).所以事实证明我使用的 Eclipse 版本(Eclipse Oxygen)存在一个错误。 Typically, when running code in Eclipse, the code is executed as javaw.exe instead of java.exe.通常,在 Eclipse 中运行代码时,代码作为 javaw.exe 而不是 java.exe 执行。 The difference between those two are ONLY that javaw.exe does not use the CMD console where as java.exe does use the CMD console.这两者之间的区别仅在于 javaw.exe 不使用 CMD 控制台,而 java.exe 使用 CMD 控制台。 Usually Eclipse will execute with javaw since it has its own console, however there is a known bug with some Eclipse versions, perhaps just Oxygen, where Eclipse does not handle this properly so you need to make it use java instead.通常 Eclipse 将使用 javaw 执行,因为它有自己的控制台,但是某些 Eclipse 版本存在一个已知错误,可能只是 Oxygen,Eclipse 无法正确处理此问题,因此您需要使用 java 代替。 To do this:去做这个:

Go to Run > Run Configurations > make sure the desired class with the main method is selected on the left > select JRE on the right > click Alternate JRE under the Runtime JRE section > select Alternate under Java executable and type "java" in the field > Apply转到运行 > 运行配置 > 确保在左侧选择了具有主要方法的所需类 > 在右侧选择 JRE > 单击运行时 JRE 部分下的备用 JRE > 在 Java 可执行文件下选择备用并在字段中键入“java” > 申请

在此处输入图片说明

Does it work in an unrelated project in Eclipse but not your current project?它在 Eclipse 中的一个不相关的项目中工作,而不是您当前的项目吗? I assume some of the library code you use calls System.setOut() (and Err analogously) to change the print stream.我假设您使用的一些库代码调用System.setOut() (和 Err 类似)来更改打印流。 What you can try is:您可以尝试的是:

PrintStream out = System.out;
...
insert your library calls here
....
System.setOut(out);
System.out.println("Test out println");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM