这是关于一个问题,我已经做了相当多的研究,但没有找到一个对我有用的答案。因此发布了一个新的问题。
摘要使用java代码执行powershell命令,而powershell 3不返回UTF-8 characters。当我使用java代码和powershell版本2执行相同的命令时,同样的情况也会发生。
场景:我有一个java代码,它正在启动一个新进程并执行powershell脚本。最后,是完整的java文件和powershell脚本,我在其中设置代码页和outputEncoding,以便能够在UTF-8中获得输出。下面是没有给出预期结果的java代码片段:
//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字符,这里是雪人字符):
C:\temp\Snowman☃folder\snowman☃file但是,当我取消注释第一行(使用最新版本的powershell)并运行java代码时,它会为雪人字符返回?。这是输出:
C:\temp\Snowman?folder\snowman?file我尝试了各种选择,比如
请建议我错过了什么或者一些我应该去探索的东西。提前谢谢。
这里是完整的代码
PowershellUtil {公共类
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脚本
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发布于 2018-07-23 05:31:33
在打印输出之前设置[Console]::OutputEncoding = [System.Text.Encoding]::UTF8。
https://stackoverflow.com/questions/51456006
复制相似问题