Nikunj Gupta
Nikunj Gupta

Reputation: 126

TerminatorExpectedAtEndOfString error while executing powershell command in java

String cd = "";
    String file="E:/Code.java";
    cd+="powershell\n";
    cd+="$username=\"[email protected]”;\n";
    cd+="$password=”pwd”;\n";
    cd+="$smtpServer = “smtp.gmail.com”;\n";
    cd+="$msg = new-object Net.Mail.MailMessage;\n";
    cd+="$smtp = new-object Net.Mail.SmtpClient($smtpServer,587);\n";
    cd+="$smtp.Credentials = New-Object System.Net.NetworkCredential(\"[email protected]\", \"password\");\n";
    cd+="$smtp.EnableSsl = $true;\n";
    cd+="$msg.From = \"mail@gmail\";\n";
    cd+="$msg.To.Add(“[email protected]”);\n";
    cd+="$msg.Body=”Your message Body”;\n";
    cd+="$msg.Subject = “"+System.getProperty("user.name")+"”;\n";
    cd+="$file=“"+file+"”;\n";
    cd+="$att = new-object Net.Mail.Attachment($file);\n";
    cd+="$msg.Attachments.Add($att);\n";
    cd+="$smtp.Send($msg);";
    String line;

    Process p=Runtime.getRuntime().exec(cd);
     System.out.println("Standard Error:");
      BufferedReader stderr = new BufferedReader(new InputStreamReader(
        p.getErrorStream()));
      while ((line = stderr.readLine()) != null) {
       System.out.println(line);
      }
      stderr.close();

    System.out.println("Done");

The output is :


Standard Error: The string is missing the terminator: ". + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString

Done

Upvotes: 4

Views: 10143

Answers (1)

Ranadip Dutta
Ranadip Dutta

Reputation: 9341

Open the code with Notepad++

See the encoding type.

It seems that the quotes are ASCII quotes. Try using the backtick symbol followed by the double quotes like :

`"

Upvotes: 5

Related Questions