我们尝试使用Java和UTF-8月的窗口。该应用程序写入记录的控制台上,我们想到使用UTF-8日志作为我们的应用程序已经国际化的记录。

它是可能的配置JVM所以它产生UTF-8、使用 -Dfile.encoding=UTF-8 作为参数JVM。它工作正常,但是输出上一个窗台乱码。

然后,我们可以设置的页码的控制台65001(chcp 65001),但在这种情况下, .bat 文件不起作用。这意味着当我们尝试推出我们的应用程序,通过我们的脚本(名开始。蝙蝠),绝对什么也不会发生。该命令简单的回报:

C:\Application> chcp 65001
Activated code page: 65001
C:\Application> start.bat

C:\Application>

但没有 chcp 65001, 没有问题,并应用程序可以启动。

任何提示吗?

有帮助吗?

解决方案

尝试 chcp 65001 && start.bat

其他提示

Java on windows不支持unicode输出的默认。我已经写了一个解决办法的方法通过调用司机API与南国防军图书馆。该方法将呼叫WriteConsoleW unicode的输出。

import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.win32.StdCallLibrary;

/** For unicode output on windows platform
 * @author Sandy_Yin
 * 
 */
public class Console {
    private static Kernel32 INSTANCE = null;

    public interface Kernel32 extends StdCallLibrary {
        public Pointer GetStdHandle(int nStdHandle);

        public boolean WriteConsoleW(Pointer hConsoleOutput, char[] lpBuffer,
                int nNumberOfCharsToWrite,
                IntByReference lpNumberOfCharsWritten, Pointer lpReserved);
    }

    static {
        String os = System.getProperty("os.name").toLowerCase();
        if (os.startsWith("win")) {
            INSTANCE = (Kernel32) Native
                    .loadLibrary("kernel32", Kernel32.class);
        }
    }

    public static void println(String message) {
        boolean successful = false;
        if (INSTANCE != null) {
            Pointer handle = INSTANCE.GetStdHandle(-11);
            char[] buffer = message.toCharArray();
            IntByReference lpNumberOfCharsWritten = new IntByReference();
            successful = INSTANCE.WriteConsoleW(handle, buffer, buffer.length,
                    lpNumberOfCharsWritten, null);
            if(successful){
                System.out.println();
            }
        }
        if (!successful) {
            System.out.println(message);
        }
    }
}

我们有一些类似的问题在Linux。我们的代码是ISO-8859-1(主要是cp-1252兼容的),但是控制台是UTF-8,使代码不要编译。简单地改变控制台ISO-8859-1号将使建立脚本,在UTF-8,以打破。我们发现一对夫妇的选择:
1-界定一些标准编码和粘到它。这是我们的选择。我们选择保留所有在ISO-8859-1,修改的建立脚本。
2-设置编码开始之前的任何任务,即使内部生成的脚本。一些代码就像埃里克森说。在Linux是这样的:

lang=pt_BR.ISO-8859-1 /usr/local/xxxx

我蚀仍然是这样。这两个做的工作。

你有没有试过 PowerShell 而不是老的cmd.exe.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top