Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从Windows上的Java控制台应用程序确定当前活动的代码页?_Java_Windows_Jna_Windows Console_Codepages - Fatal编程技术网

如何从Windows上的Java控制台应用程序确定当前活动的代码页?

如何从Windows上的Java控制台应用程序确定当前活动的代码页?,java,windows,jna,windows-console,codepages,Java,Windows,Jna,Windows Console,Codepages,这是一个简单的Java应用程序,在Windows上显示默认代码页: package doscommand; import java.io.IOException; import java.io.InputStream; public class DosCommand { public static void main(String[] args) throws IOException { InputStream in = Runtime.getRuntime().

这是一个简单的Java应用程序,在Windows上显示默认代码页:

package doscommand;

import java.io.IOException;
import java.io.InputStream;

public class DosCommand {

    public static void main(String[] args) throws IOException {

        InputStream in = Runtime.getRuntime().exec("chcp.com").getInputStream();
        int ch;
        StringBuilder chcpResponse = new StringBuilder();
        while ((ch = in.read()) != -1) {
            chcpResponse.append((char) ch);
        }
        System.out.println(chcpResponse); // For example: "Active code page: 437"
    }
}
在我的Windows 10计算机上,此应用程序始终显示“活动代码页:437”,因为Cp437是默认值,并且
Runtime.getRuntime().exec()
在运行
chcp.com
时启动一个新的
进程

是否可以创建一个Java应用程序来显示运行代码的现有命令提示符窗口的当前活动代码页

我希望能够在命令提示符下执行类似操作:

chcp 1252
java -jar "D:\NB82\DosCommand\dist\DosCommand.jar" REM Shows current code page is "1252".

chcp 850
java -jar "D:\NB82\DosCommand\dist\DosCommand.jar" REM  Shows current code page is "850".
问了一个类似的问题,尽管在这种情况下OP正在寻找一个非Java的解决方案

我更喜欢纯Java的解决方案,但作为替代方案:

  • 使用JNI,通过调用一些具有Windows API访问权限的C/C++/C#代码,可以做到这一点吗?被调用的代码只需返回活动代码页的数值
  • 我会接受一个有说服力地认为这是不可能的答案

    • 解决方案原来只是一行代码,Windows API函数返回的值提供控制台的活动代码页:

      import com.sun.jna.platform.win32.Kernel32;
      
      public class JnaActiveCodePage {
      
          public static void main(String[] args) {
              System.out.println("" + JnaActiveCodePage.getActiveInputCodePage());
          }
      
          /**
           * Calls the Windows function GetConsoleCP() to get the active code page using JNA.
           * "jna.jar" and "jna-platform.jar" must be on the classpath.
           *
           * @return the code page number.
           */
          public static int getActiveInputCodePage() {
              return Kernel32.INSTANCE.GetConsoleCP();
          }
      }
      

      如果这是我的问题,我会在MSDN中搜索C/C++解决方案,然后使用JNA在Java中完成。或者更好的方法是使用C#并完全跳过Java步骤。chcp.com显示
      GetConsoleCP
      中的当前输入代码页。当前输出代码页是
      GetConsoleOutputCP
      ,但chcp.com不使用它。当设置代码页时,它通过
      SetConsoleCP
      SetConsoleOutputCP
      @HovercraftFullOfEels设置与输入和输出代码页相同的值。我按照您的建议执行。由于工作正常,我将其中一个标记从JNI切换到JNA。@eryksun
      GetConsoleCP()
      工作正常。