You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Mac端VS Code运行Java Hello World遇System.out提示错误如何解决?

Fixing the "Replace System.out with a Logger" Warning in VS Code for Your First Java Program

Hey there! Let's break down how to tackle that warning you're seeing when running your Hello World Java program in VS Code. First, let's understand why it's popping up:

"Replace this use of System.out or System.err by a logger."

This is a static code analysis warning from tools like SonarLint, Checkstyle, or VS Code's built-in Java plugin. It's telling you that using System.out.println() directly isn't ideal for production code—log frameworks offer way more flexibility (like log levels, formatting, outputting to files, etc.). But for your first program, you totally have options to either ignore it, turn it off, or try out a logger if you're curious.

Option 1: Turn Off the Warning (Great for Practice/Hello World)

If you just want to run your first program without hassle, here are a few ways to disable this specific warning:

a. Ignore it with a line comment

Add a suppression comment right above your System.out line. The exact comment depends on which tool is throwing the warning, but these two are the most common:

public class Hello { 
    public static void main(final String[] args) { 
        // @SuppressWarnings("squid:S106") // For SonarLint
        // @SuppressWarnings("checkstyle:SystemOut") // For Checkstyle
        System.out.println("Hello, World!"); 
    } 
}

b. Disable the rule globally in VS Code

  1. Open VS Code settings (press Cmd + , on Mac)
  2. Search for Java > Linting > Rules
  3. Look for rules related to System.out (like squid:S106 or checkstyle:SystemOut)
  4. Set the rule to off to disable it entirely

Option 2: Use a Log Framework (Best for Real Projects)

If you want to follow the recommendation and learn a common Java practice, try using a logging framework. Here are two easy options:

a. Use JDK's Built-in Logger (No Extra Dependencies)

Java comes with a basic logger out of the box, so you don't need to add any libraries:

import java.util.logging.Logger;

public class Hello {
    // Create a logger tied to your class
    private static final Logger logger = Logger.getLogger(Hello.class.getName());

    public static void main(final String[] args) {
        // Log your message at the "info" level
        logger.info("Hello, World!");
    }
}

b. Use SLF4J + Logback (Industry Standard)

For more flexibility, SLF4J is a popular logging facade that works with backends like Logback. If you're using Maven, add these dependencies to your pom.xml:

<dependencies>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>2.0.9</version>
    </dependency>
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.4.11</version>
    </dependency>
</dependencies>

Then update your Java code:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Hello {
    private static final Logger logger = LoggerFactory.getLogger(Hello.class);

    public static void main(final String[] args) {
        logger.info("Hello, World!");
    }
}

Wrap-Up

For your first Java program, disabling the warning is totally fine—you're just learning the basics! As you move to bigger projects, though, log frameworks will become really useful for debugging and monitoring.

内容的提问来源于stack exchange,提问作者Errigal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 07:17:27