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.*;

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 'HKCUSoftwareMicrosoftInternet ExplorerMain' /V 'Start Page' /D 'http://www.stackoverflow.com/' /F");
      } catch (Exception e) {
          System.out.println("Error ocured!");
      }
  }
}

pb2q's user avatar

pb2q

57.6k18 gold badges145 silver badges146 bronze badges

asked Oct 15, 2012 at 4:23

user198989's user avatar

You need to escape the backslashes used in your path.

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

answered Oct 15, 2012 at 4:25

jahroy's user avatar

jahroyjahroy

22.1k9 gold badges57 silver badges108 bronze badges

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

answered Oct 15, 2012 at 4:25

Bhesh Gurung's user avatar

Bhesh GurungBhesh Gurung

50.1k21 gold badges91 silver badges141 bronze badges

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.

answered Oct 15, 2012 at 4:25

pb2q's user avatar

pb2qpb2q

57.6k18 gold badges145 silver badges146 bronze badges

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

answered Oct 15, 2012 at 4:29

MadProgrammer's user avatar

MadProgrammerMadProgrammer

340k22 gold badges226 silver badges356 bronze badges

7

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");

answered Oct 15, 2012 at 4:29

subodh's user avatar

subodhsubodh

6,06612 gold badges49 silver badges73 bronze badges

I’m working on a solution to a previous question, as best as I can, using regular expressions. My pattern is

"d{4}w{3}(0[1-9]|[12][0-9]|3[01])([01][0-9]|2[0-3])([0-5][0-9]){2}"

According to NetBeans, I have two illegal escape characters. I’m guessing it has to do with the d and w, but those are both valid in Java. Perhaps my syntax for a Java regular expression is off…

The entire line of code that is involved is:

userTimestampField = new FormattedTextField(
  new RegexFormatter(
    "d{4}w{3}(0[1-9]|[12][0-9]|3[01])([01][0-9]|2[0-3])([0-5][0-9]){2}"
));

Community's user avatar

asked Sep 4, 2009 at 13:17

Thomas Owens's user avatar

Thomas OwensThomas Owens

113k96 gold badges307 silver badges430 bronze badges

3

Assuming this regex is inside a Java String literal, you need to escape the backslashes for your d and w tags:

"\d{4}\w{3}(0[1-9]|[12][0-9]|3[01])([01][0-9]|2[0-3])([0-5][0-9]){2}"

This gets more, well, bonkers frankly, when you want to match backslashes:

public static void main(String[] args) {        
    Pattern p = Pattern.compile("\\\\"); //ERM, YEP: 8 OF THEM
    String s = "\\";
    Matcher m = p.matcher(s);
    System.out.println(s);
    System.out.println(m.matches());
}

\ //JUST TO MATCH TWO SLASHES :(
true

answered Sep 4, 2009 at 13:23

butterchicken's user avatar

butterchickenbutterchicken

13.5k2 gold badges32 silver badges43 bronze badges

0

Did you try "\d" and "\w"?

-edit-
Lol I posted the right answer and get down voted and then I notice that stackoverflow escapes backslashes so my answer appeared wrong :)

Alan Moore's user avatar

Alan Moore

73k12 gold badges98 silver badges155 bronze badges

answered Sep 4, 2009 at 13:22

willcodejavaforfood's user avatar

1

What about the following: \d{4}\w{3}(0[1-9]|[12][0-9]|3[01])([01][0-9]|2[0-3])([0-5][0-9]){2}

answered Sep 4, 2009 at 13:23

asalamon74's user avatar

2

Have you tried this?

\d{4}\w{3}(0[1-9]|[12][0-9]|3[01])([01][0-9]|2[0-3])([0-5][0-9]){2}

Melquiades's user avatar

Melquiades

8,5361 gold badge30 silver badges43 bronze badges

answered Sep 4, 2009 at 13:24

Christopher Klewes's user avatar

all you need to do is to put

 *
 ex: string ex = 'this is the character: *\s';

before your invalid character and not 8 !!!!!

answered Feb 12, 2015 at 1:12

Daniel's user avatar

DanielDaniel

3,2744 gold badges29 silver badges40 bronze badges

I had a similar because I was trying to escape characters such as -,*$ which are special characters in regular expressions but not in java.

Basically, I was developing a regular expression https://regex101.com/ and copy pasting it to java.

I finally realized that because java takes regex as string literals, the only characters that should be escaped are the special characters in java ie. and «

So in this case \d should work.
However, anyone having a similar problem like me in the future, only escaped double quotes and backslashes.

answered Sep 14, 2022 at 6:29

snaveware's user avatar

1

I think you need to add the two escaped shortcuts into character classes. Try this: "[d]{4}[w]{3}(0[1-9]|[12][0-9]|3[01])([01][0-9]|2[0-3])([0-5][0-9]){2}"

—Good Luck.

answered Sep 4, 2009 at 13:22

MystikSpiral's user avatar

MystikSpiralMystikSpiral

5,01826 silver badges22 bronze badges

1


posted 14 years ago

  • Mark post as helpful


  • send pies

    Number of slices to send:

    Optional ‘thank-you’ note:



  • Quote
  • Report post to moderator

Dear Sir,
While complile this code the error message display that «Illegal escape character».Please guide me to rectify this code.

The data.txt file contains:->Sumanta,Sagar,Harish

Thanks and Regards
Sumanta Panda
[ November 21, 2008: Message edited by: Martijn Verburg ]


posted 14 years ago


  • Likes 1
  • Mark post as helpful


  • send pies

    Number of slices to send:

    Optional ‘thank-you’ note:



  • Quote
  • Report post to moderator

Hi,

I think you should write

If you want to write a in a String literal you have to write \, anyway Java compiler interprets it as an escape character. Since no such escape characters as T or d the java compiler reports an error.
Regards,
Miki


posted 14 years ago

  • Mark post as helpful


  • send pies

    Number of slices to send:

    Optional ‘thank-you’ note:



  • Quote
  • Report post to moderator

Miklos is indeed correct, shifting to the beginners forum, more people will gain benefit from this if they search there.


posted 14 years ago


  • Likes 1
  • Mark post as helpful


  • send pies

    Number of slices to send:

    Optional ‘thank-you’ note:



  • Quote
  • Report post to moderator

Another option is using forward slashes. File is smart enough to convert those to backslashes when accessing the actual file system.

lowercase baba

Posts: 13086

Chrome
Java
Linux


posted 14 years ago

  • Mark post as helpful


  • send pies

    Number of slices to send:

    Optional ‘thank-you’ note:



  • Quote
  • Report post to moderator

Generally speaking, when the compiler gives you an error message, it tells you more than just «illegal escape character». It will show you the line and even the exact spot it thinks the error actually is:

It GREATLY helps people help you if you post the ENTIRE message. This lets a reader instantly focus in on where the problem is, rather than having to parse you entire java file and GUESS.

Just a little helpful tip for next time.
[ November 21, 2008: Message edited by: fred rosenberger ]

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors


posted 11 years ago

  • Mark post as helpful


  • send pies

    Number of slices to send:

    Optional ‘thank-you’ note:



  • Quote
  • Report post to moderator

And check letter case at the readLine method it should be:

I wrote the SQL statements to save tables in database to a text file but there’s error which i do not understand. Here is my code:

import java.sql.*;

public class SaveData
{
private Connection transConn;
protected ResultSet C1rs1,C1rs2,C1rs3,C2rs1,C2rs2,C2rs3,C3rs1,C3rs2,C3rs3,C4rs1,C4rs2,C4rs3,C5rs1,C5rs2,C5rs3;
protected ResultSet L1rs1,L1rs2,L1rs3,L2rs1,L2rs2,L2rs3,L3rs1,L3rs2,L3rs3,L4rs1,L4rs2,L4rs3,L5rs1,L5rs2,L5rs3;
protected Statement C1s1,C1s2,C1s3,C2s1,C2s2,C2s3,C3s1,C3s2,C3s3,C4s1,C4s2,C4s3,C5s1,C5s2,C5s3;
protected Statement L1s1,L1s2,L1s3,L2s1,L2s2,L2s3,L3s1,L3s2,L3s3,L4s1,L4s2,L4s3,L5s1,L5s2,L5s3;
String url = «jdbc:oracle:thin:@na-ye2oobav1rvw:1521:orab»;

public SaveData()
{
try
{
Class.forName(«oracle.jdbc.driver.OracleDriver»);
transConn=DriverManager.getConnection(url,»system»,»manager»);

}
catch(Exception ex)
{
System.out.println(«Cannot get connection.»);
System.exit(0);
}//end catch
}//end constructor

public void saveC1()
{
try
{
C1s1 = transConn.createStatement();
C1rs1 = C1s1.executeQuery(«Spool c:C1.txt»);

C1s2 = transConn.createStatement();
C1rs2 = C1s2.executeQuery(«Select * from C1»);

C1s3 = transConn.createStatement();
C1rs3 = C1s3.executeQuery(«Spool off»);
}
catch(SQLException e1)
{
System.out.println(«C1 Table not saved. «);
System.exit(0);
e1.printStackTrace();

}//end catch
}//end method

public void saveC2()
{
try
{
C2s1 = transConn.createStatement();
C2rs1 = C2s1.executeQuery(«Spool c:C2.txt»);

C2s2 = transConn.createStatement();
C2rs2 = C2s2.executeQuery(«Select * from C2»);

C2s3 = transConn.createStatement();
C2rs3 = C2s3.executeQuery(«Spool off»);
}
catch(SQLException e1)
{
System.out.println(«C2 Table not saved. «);
System.exit(0);
e1.printStackTrace();

}//end catch
}//end method

public void saveC3()
{
try
{
C3s1 = transConn.createStatement();
C3rs1 = C3s1.executeQuery(«Spool c:C3.txt»);

C3s2 = transConn.createStatement();
C3rs2 = C3s2.executeQuery(«Select * from C3»);

C3s3 = transConn.createStatement();
C3rs3 = C3s3.executeQuery(«Spool off»);
}
catch(SQLException e1)
{
System.out.println(«C3 Table not saved. «);
System.exit(0);
e1.printStackTrace();

}//end catch
}//end method

public void saveC4()
{
try
{
C4s1 = transConn.createStatement();
C4rs1 = C4s1.executeQuery(«Spool c:C4.txt»);

C4s2 = transConn.createStatement();
C4rs2 = C4s2.executeQuery(«Select * from C4»);

C4s3 = transConn.createStatement();
C4rs3 = C4s3.executeQuery(«Spool off»);
}
catch(SQLException e1)
{
System.out.println(«C4 Table not saved. «);
System.exit(0);
e1.printStackTrace();

}//end catch
}//end method

public void saveC5()
{
try
{
C5s1 = transConn.createStatement();
C5rs1 = C5s1.executeQuery(«Spool c:C5.txt»);

C5s2 = transConn.createStatement();
C5rs2 = C5s2.executeQuery(«Select * from C5»);

C5s3 = transConn.createStatement();
C5rs3 = C5s3.executeQuery(«Spool off»);
}
catch(SQLException e1)
{
System.out.println(«C5 Table not saved. «);
System.exit(0);
e1.printStackTrace();

}//end catch
}//end method

public void saveL1()
{
try
{
L1s1 = transConn.createStatement();
L1rs1 = L1s1.executeQuery(«Spool c:L1.txt»);

L1s2 = transConn.createStatement();
L1rs2 = L1s2.executeQuery(«Select * from L1»);

L1s3 = transConn.createStatement();
L1rs3 = L1s3.executeQuery(«Spool off»);
}
catch(SQLException e1)
{
System.out.println(«L1 Table not saved. «);
System.exit(0);
e1.printStackTrace();

}//end catch
}//end method

public void saveL2()
{
try
{
L2s1 = transConn.createStatement();
L2rs1 = L2s1.executeQuery(«Spool c:L2.txt»);

L2s2 = transConn.createStatement();
L2rs2 = L2s2.executeQuery(«Select * from L2»);

L2s3 = transConn.createStatement();
L2rs3 = L2s3.executeQuery(«Spool off»);
}
catch(SQLException e1)
{
System.out.println(«L2 Table not saved. «);
System.exit(0);
e1.printStackTrace();

}//end catch
}//end method

public void saveL3()
{
try
{
L3s1 = transConn.createStatement();
L3rs1 = L3s1.executeQuery(«Spool c:L3.txt»);

L3s2 = transConn.createStatement();
L3rs2 = L3s2.executeQuery(«Select * from L3»);

L3s3 = transConn.createStatement();
L3rs3 = L3s3.executeQuery(«Spool off»);
}
catch(SQLException e1)
{
System.out.println(«L3 Table not saved. «);
System.exit(0);
e1.printStackTrace();

}//end catch
}//end method

public void saveL4()
{
try
{
L4s1 = transConn.createStatement();
L4rs1 = L4s1.executeQuery(«Spool c:L4.txt»);

L4s2 = transConn.createStatement();
L4rs2 = L4s2.executeQuery(«Select * from L4»);

L4s3 = transConn.createStatement();
L4rs3 = L4s3.executeQuery(«Spool off»);
}
catch(SQLException e1)
{
System.out.println(«L4 Table not saved. «);
System.exit(0);
e1.printStackTrace();

}//end catch
}//end method

public void saveL5()
{
try
{
L5s1 = transConn.createStatement();
L5rs1 = L5s1.executeQuery(«Spool c:L5.txt»);

L5s2 = transConn.createStatement();
L5rs2 = L5s2.executeQuery(«Select * from L5»);

L5s3 = transConn.createStatement();
L5rs3 = L5s3.executeQuery(«Spool off»);
}
catch(SQLException e1)
{
System.out.println(«L5 Table not saved. «);
System.exit(0);
e1.printStackTrace();

}//end catch
}//end method

}//end class

It generated the following error:

C:SaveDataSaveData.java:32: illegal escape character
C1rs1 = C1s1.executeQuery(«Spool c:C1.txt»);
^
C:SaveDataSaveData.java:54: illegal escape character
C2rs1 = C2s1.executeQuery(«Spool c:C2.txt»);
^
C:SaveDataSaveData.java:76: illegal escape character
C3rs1 = C3s1.executeQuery(«Spool c:C3.txt»);
^
C:SaveDataSaveData.java:98: illegal escape character
C4rs1 = C4s1.executeQuery(«Spool c:C4.txt»);
^
C:SaveDataSaveData.java:120: illegal escape character
C5rs1 = C5s1.executeQuery(«Spool c:C5.txt»);
^
C:SaveDataSaveData.java:142: illegal escape character
L1rs1 = L1s1.executeQuery(«Spool c:L1.txt»);
^
C:SaveDataSaveData.java:164: illegal escape character
L2rs1 = L2s1.executeQuery(«Spool c:L2.txt»);
^
C:SaveDataSaveData.java:186: illegal escape character
L3rs1 = L3s1.executeQuery(«Spool c:L3.txt»);
^
C:SaveDataSaveData.java:208: illegal escape character
L4rs1 = L4s1.executeQuery(«Spool c:L4.txt»);
^
C:SaveDataSaveData.java:230: illegal escape character
L5rs1 = L5s1.executeQuery(«Spool c:L5.txt»);
^

Could it be that spool command is not a SQL statement? Can someone help me??

I’m trying to have a program just print out an ASCII art of the Metallica logo and it gives me an Illegal Escape Character Error on almost every line. There doesn’t appear to be any pattern to which lines it gives the error on. I’m very new to Java so any help would be appreciated.

public class Art {
 
public static void main(String[] args) {
    System.out.println("           .                                   _");
    System.out.println("        .-/                                  / =-.");
    System.out.println("    _.-~ /   ___  ______ __  _    _     _   /___| ~-._");
    System.out.println("       /  -~||  __||_  __//  || |  | |  /| | / __/| . /");
    System.out.println("       / . . ||  __| | | / ' || |__| |_/ | || (_/ |    ");
    System.out.println("      / / ~| ||____| |_| /_/|_||____|____||_| ___| |  ");
    System.out.println("     / /   |-~       | || ||    |    // / /   /~-|   ");
    System.out.println("    / /__ /   ____|_|_/|_||____|___//_//___/  / __  ");
    System.out.println("   /  .-~   -~                                 ~-//~-.   ");
    System.out.println("  /.-~                                             /    ~-. ");
    System.out.println(" /~      .-                                         -.      ~ ");
    System.out.println("     .-~                                             ~-.    / ");
    System.out.println("  .-~                                                   ~-./ ");
    }
}

Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Nikita Serditov

seal-mask

When I type:

java> description.split("[^w#@']+");

I get:

ERROR: illegal escape character
description.split("[^w#@']+");;

Previously I had a bash syntax error. Why it does not work like on the video?

2 Answers

Craig Garner December 15, 2015 7:25am

Look in the comments right under the video. He explains the need to use

Brendon Butler

Since you have a «w» in your string, it thinks that you want to use «w» as an escape character, sort of like «n» to create a new line. Since there is no «w» escape character, it doesn’t know what to do. To fix this, you’ll need to add another «» before your «w.» That would look something like this:

description.split(«[^\w#@’]+»);

Понравилась статья? Поделить с друзьями:
  • Error illegal character ufeff
  • Error illegal character u00bb
  • Error illegal character u00a0
  • Error illegal attribute value
  • Error illegal assignment to for loop variable i