Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java and Windows - error: illegal escape character

I have done my .java file that changes registry data. But I am getting "illegal escape character" error on the line where Runtime.getRuntime().exec exists. Where is my mistake ?

import java.util.*;
import java.applet.Applet; 
import java.awt.*; 

class test {
  public static void main(String args[]) {
      try {
          Runtime.getRuntime().exec("REG ADD 'HKCU\Software\Microsoft\Internet Explorer\Main' /V 'Start Page' /D 'http://www.stackoverflow.com/' /F");
      } catch (Exception e) {
          System.out.println("Error ocured!");
      }
  }
}
like image 693
user198989 Avatar asked Nov 28 '22 09:11

user198989


2 Answers

You need to escape the backslashes used in your path.

String windowsPath = "\\Users\\FunkyGuy\\My Documents\\Hello.txt";
like image 106
jahroy Avatar answered Dec 05 '22 21:12

jahroy


You need to escape \ with another \, so replace \ with \\ in your input string.

like image 25
Bhesh Gurung Avatar answered Dec 05 '22 23:12

Bhesh Gurung