Java and Windows - error: illegal escape character

48,066

Solution 1

You need to escape the backslashes used in your path.

String windowsPath = "\\Users\\FunkyGuy\\My Documents\\Hello.txt";

Solution 2

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

Solution 3

You need to escape the backslash characters in your registry path string:

"REG ADD `HKCU\\Software\\ ...

The backslash character has a special meaning in strings: it's used to introduce escape characters. if you want to use it literally in a string, then you'll need to escape it, by using a double-backslash.

Solution 4

Back slashes in Java are special "escape" characters, they provide the ability to include things like tabs \t and/or new lines \n and lots of other fun stuff.

Needless to say, you to to "escape" them as well by adding an addition \ character...

'HKCU\\Software\\Microsoft\\Internet Explorer\\Main'

On a side note. I would use ProcessBuilder or at the very least, the version of Runtime#exec that uses array arguments.

It will save a lot of hassle when it comes to dealing with spaces within command parameters, IMHO

Solution 5

Probably because you didn't escape the backslash in your string. Have a look at http://docs.oracle.com/javase/tutorial/java/data/characters.html for more information about proper escaping.

Share:
48,066
user198989
Author by

user198989

Updated on July 09, 2022

Comments

  • user198989
    user198989 almost 2 years

    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!");
          }
      }
    }
    
  • Stephen C
    Stephen C over 11 years
    Almost everyone calls "\" a backslash not a forward slash. en.wikipedia.org/wiki/Backslash
  • jahroy
    jahroy over 11 years
    When I hear people say forward slash on the radio when talking about URLs I want to go bananas!!
  • Stephen C
    Stephen C over 11 years
    Not if you want people to understand what you are saying ... which I'd have thought was a prerequisite for answering Questions on SO.
  • Stephen C
    Stephen C over 11 years
    On the other hand, maybe the problem is: Mae eich hofrenfad yn llawn llyswennod trydan.
  • MadProgrammer
    MadProgrammer over 11 years
    @StephenC 30 years playing with computers and still can't get my head around back and forward slashes...they've just become windows and unix slashes :P
  • MadProgrammer
    MadProgrammer over 11 years
    @StephenC Besides, if you can't dazzle 'em with brilliance, baffle 'em with B*** ***t
  • Stephen C
    Stephen C over 11 years
    (Damn ... you rumbled me :-) :-) )