天天看點

解決IDEA java項目中文亂碼的三種方案

idea用了兩年,偶爾有些項目啟動時控制台會出現中文亂碼的問題,彙總了一些解決方案,能解決遇到過的所有中文亂碼問題。

解決方案一、maven項目設定pom檔案編譯的編碼格式為utf-8

在maven項目的pom.xml檔案設定編譯插件及項目編碼<encoding>UTF-8</encoding>,具體如下圖所示

<build>
        <!-- 插件 -->
        <plugins>
            <!-- 編譯插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <compilerVersion>${java.version}</compilerVersion>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <encoding>UTF-8</encoding>
                    <!-- prevents endPosTable exception for maven compile -->
                    <useIncrementalCompilation>false</useIncrementalCompilation>
                </configuration>
            </plugin>
        </plugins>
</build>
           

解決方案二、idea設定File Encodings為utf-8

idea打開配置,搜尋encode,配置如下圖所示:

解決IDEA java項目中文亂碼的三種方案

解決方案三、tomcat啟動配置設定VM編碼參數

編輯tomcat啟動配置,添加VM參數:-Dfile.encoding=UTF-8

解決IDEA java項目中文亂碼的三種方案