首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尽管设置了代码页和outputEncoding,但Java代码使用Powershell 3返回问号来执行命令。

尽管设置了代码页和outputEncoding,但Java代码使用Powershell 3返回问号来执行命令。
EN

Stack Overflow用户
提问于 2018-07-21 12:19:42
回答 1查看 257关注 0票数 0

这是关于一个问题,我已经做了相当多的研究,但没有找到一个对我有用的答案。因此发布了一个新的问题。

摘要使用java代码执行powershell命令,而powershell 3不返回UTF-8 characters。当我使用java代码和powershell版本2执行相同的命令时,同样的情况也会发生。

场景:我有一个java代码,它正在启动一个新进程并执行powershell脚本。最后,是完整的java文件和powershell脚本,我在其中设置代码页和outputEncoding,以便能够在UTF-8中获得输出。下面是没有给出预期结果的java代码片段:

代码语言:javascript
复制
//String command = "powershell.exe -ExecutionPolicy RemoteSigned \"C:\\temp\\ListDirectory.ps1\"";
String command = "powershell.exe -Version 2.0 -ExecutionPolicy RemoteSigned \"C:\\temp\\ListDirectory.ps1\"";
Process powerShellProcess = Runtime.getRuntime().exec(command);
powerShellProcess.getOutputStream().close();
String line;

System.out.println("Standard Output:");
//PrintStream out = new PrintStream(System.out, true, "UTF-8");
//CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();

BufferedReader stdout = new BufferedReader(new InputStreamReader(powerShellProcess.getInputStream(), "UTF-8"));
while ((line = stdout.readLine()) != null) {
   System.out.println(line);
}
stdout.close();

现在的问题是,当我使用powershell version 2.0显式运行它并给出预期的输出时,这段代码工作得很好(例如,显示UTF-8字符,这里是雪人字符):

代码语言:javascript
复制
C:\temp\Snowman☃folder\snowman☃file

但是,当我取消注释第一行(使用最新版本的powershell)并运行java代码时,它会为雪人字符返回?。这是输出:

代码语言:javascript
复制
C:\temp\Snowman?folder\snowman?file

我尝试了各种选择,比如

  • 启动java进程时设置envp,但不起作用
  • 设置charDecoder,但不起作用
  • 将字符集设置为"UTF-8",但不起作用
  • 我还试图将输出输送到一个文件中,令人惊讶的是,文件内容中有UTF-8字符。我不明白为什么在用Java获取输出时,它不能工作。

请建议我错过了什么或者一些我应该去探索的东西。提前谢谢。

这里是完整的代码

PowershellUtil {​公共类

代码语言:javascript
复制
public static void main(String[] args) throws IOException {

    invokePowershell();
    //printString();
}

static void printString() throws IOException {
    String snowman = "Ê ☃ é";
    PrintStream out = new PrintStream(System.out, true, "UTF-8");
    out.println(snowman);
}

static void invokePowershell() throws IOException {

    // String command = "powershell.exe your command";
    // Getting the version
    // String command = "powershell.exe  $PSVersionTable";
    //String command = "powershell.exe -Version 2.0 Get-ChildItem -LiteralPath 'C:\\temp\\Snowman☃folder' -Recurse -Filter 'snowman☃file' | ForEach-Object {$_.FullName}";
    //String command = "powershell.exe -ExecutionPolicy RemoteSigned \"C:\\temp\\ListDirectory.ps1\"";
    String command = "powershell.exe -Version 2.0 -ExecutionPolicy RemoteSigned \"C:\\temp\\ListDirectory.ps1\"";

    // Executing the command
    /*String[] envp = { "file.encoding=UTF8" };
    Process powerShellProcess = Runtime.getRuntime().exec(command, envp);*/
    Process powerShellProcess = Runtime.getRuntime().exec(command);

    // Getting the results
    powerShellProcess.getOutputStream().close();
    String line;
    System.out.println("Standard Output:");
    //PrintStream out = new PrintStream(System.out, true, "UTF-8");
    //CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();

    BufferedReader stdout = new BufferedReader(
            new InputStreamReader(powerShellProcess.getInputStream(), "UTF-8"));
    while ((line = stdout.readLine()) != null) {
        System.out.println(line);
    }
    stdout.close();
    /*InputStreamReader isr = new InputStreamReader(powerShellProcess.getInputStream());
    int data = isr.read();
    while (data != -1) {
        char theChar = (char) data;
        System.out.println("read character: " + String.valueOf(theChar));
        data = isr.read();
    }
    isr.close();*/

    System.out.println("Standard Error:");
    BufferedReader stderr = new BufferedReader(
            new InputStreamReader(powerShellProcess.getErrorStream()));
    while ((line = stderr.readLine()) != null) {
        System.out.println(line);
    }
    stderr.close();
    System.out.println("Done");
}

}​

这里是powershell脚本

代码语言:javascript
复制
Set-Variable UTF8_CODEPAGE 65001
Set-Variable UTF8_ENCODING "System.Text.UTF8Encoding"
chcp $UTF8_CODEPAGE > $null
$OutputEncoding = New-Object -typename $UTF8_ENCODING
# $OutputEncoding = [System.Text.Encoding]::UTF8
# Get-ChildItem -LiteralPath 'C:\temp\Snowman☃folder' -Recurse -Filter 'snowman☃file' | ForEach-Object FullName | Out-File 'C:\temp\result.txt'
Get-ChildItem -LiteralPath 'C:\temp\Snowman☃folder' -Recurse -Filter 'snowman☃file' | ForEach-Object {$_.FullName}
# Get-Content -Path "C:\temp\result.txt" -Encoding UTF8
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-23 05:31:33

在打印输出之前设置[Console]::OutputEncoding = [System.Text.Encoding]::UTF8

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51456006

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档