代币';d';Powershell在Java中出现意外表达式

代币';d';Powershell在Java中出现意外表达式,java,Java,我正在使用Java 我正在尝试在powershell.exe中执行dir命令,并尝试对每行执行-split以删除每行的所有“d” 好吧,它不起作用,这是错误: Directorio: C:\Users\2dam\Desktop\JavaApplication9 En l�nea: 1 Car�cter: 98 + ... Directorio: C:\Users\2dam\Desktop\JavaApplication9; $line -split d +

我正在使用Java

我正在尝试在powershell.exe中执行dir命令,并尝试对每行执行-split以删除每行的所有“d”

好吧,它不起作用,这是错误:

   Directorio: C:\Users\2dam\Desktop\JavaApplication9
En l�nea: 1 Car�cter: 98
+ ...  Directorio: C:\Users\2dam\Desktop\JavaApplication9;  $line -split  d
+                                                                       ~
Debe proporcionar una expresi�n de valor despu�s del operador '-split'.
En l�nea: 1 Car�cter: 100
+ ...  Directorio: C:\Users\2dam\Desktop\JavaApplication9;  $line -split  d
+                                                                         ~
Token 'd' inesperado en la expresi�n o la instrucci�n.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : ExpectedValueExpression
它是西班牙语的,但在sort中表示它不需要'd',并且在拆分后需要一个表达式,这是我的代码


package javaapplication9;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class JavaApplication9 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
      //String command = "powershell.exe  your command";
  //Getting the version
  String command = "powershell.exe  dir";
  // Executing the command
  Process powerShellProcess = Runtime.getRuntime().exec(command);
  // Getting the results
  powerShellProcess.getOutputStream().close();
  String line;
  System.out.println("Standard Output:");
  BufferedReader stdout = new BufferedReader(new InputStreamReader(
    powerShellProcess.getInputStream()));
  while ((line = stdout.readLine()) != null) {
      String[] commands = {"powershell.exe", "Set-Variable", "-Name \"line\" -Value \""+line+"\";", " $line -split  \"d\""};
      
  //Getting the version
      System.out.println(line);
  // Executing the command
  Process powerShellProcess2 = Runtime.getRuntime().exec(commands);
  // Getting the results
  powerShellProcess2.getOutputStream().close();
  String line2;

  BufferedReader stdout2 = new BufferedReader(new InputStreamReader(
    powerShellProcess2.getInputStream()));
  while ((line2 = stdout2.readLine()) != null) {
   System.out.println(line2);
  }
  BufferedReader stderr2 = new BufferedReader(new InputStreamReader(
    powerShellProcess2.getErrorStream()));
  while ((line2 = stderr2.readLine()) != null) {
   System.out.println(line2);
  }
  stderr2.close();
    
  }
}

    }
    
你知道我做错了什么吗


谢谢

您将powershell.exe的参数括在引号中,这很有意义。但这意味着您正在执行以下命令:

powershell.exe set-variable l abcdefg; $l -split "d"
在Windows命令提示符下输入,您将得到相同的错误。命令解释器将删除引号,因此当Powershell执行命令时,引号不再存在

以交互方式,我能够通过将双引号括在单引号中使其工作:

...>powershell.exe set-variable l abcdefg; $l -split '"d"'
abc
efg

您将powershell.exe的参数括在引号中,这很有意义。但这意味着您正在执行以下命令:

powershell.exe set-variable l abcdefg; $l -split "d"
在Windows命令提示符下输入,您将得到相同的错误。命令解释器将删除引号,因此当Powershell执行命令时,引号不再存在

以交互方式,我能够通过将双引号括在单引号中使其工作:

...>powershell.exe set-variable l abcdefg; $l -split '"d"'
abc
efg

由于第26行和翻译错误后,已经看到,该问题针对“您必须在“-split”运算符后提供一个值表达式”而提出,因此为了解决此问题,您可以遵循中的指南,并在-split后放置“d”,因为第26行和翻译错误后,已经看到,该问题针对“您必须在“-split”运算符之后提供一个值表达式,以便解决此问题,您可以按照中的指导原则,在-split之后加上“d”。我正在尝试,但我在字符串中,因此我不知道如何在引号中加引号。我尝试了以下方法:$line-split”+“+”d“+”+”不起作用,我正在尝试,但我在一个字符串中,所以我不知道如何用引号括起来。我试过:$line-split“+”“+”d“+”“+”不起作用