Java and Windows - error: illegal escape character

前端 未结 6 1166
失恋的感觉
失恋的感觉 2021-01-14 06:04

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. Wh

相关标签:
6条回答
  • 2021-01-14 06:15

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

    0 讨论(0)
  • 2021-01-14 06:19

    You need to escape the backslashes used in your path.

    String windowsPath = "\\Users\\FunkyGuy\\My Documents\\Hello.txt";
    
    0 讨论(0)
  • 2021-01-14 06:21

    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.

    0 讨论(0)
  • 2021-01-14 06:23

    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.

    0 讨论(0)
  • 2021-01-14 06:28

    you need replace escape \ with \\

    below code will work

    Runtime.getRuntime().exec("REG ADD 'HKCU\\Software\\Microsoft\\Internet Explorer\\Main' /V 'Start Page' /D 'http://www.stackoverflow.com/' /F");
    
    0 讨论(0)
  • 2021-01-14 06:32

    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

    0 讨论(0)
提交回复
热议问题