“Unmappable character for encoding UTF-8” error

后端 未结 10 786
失恋的感觉
失恋的感觉 2020-11-27 03:21

I\'m getting a compile error at the following method.

public static boolean isValidPasswd(String passwd) {
    String reg = \"^(?=.*[0-9])(?=.*[a-z])(?=.*[A-         


        
相关标签:
10条回答
  • 2020-11-27 03:32

    "error: unmappable character for encoding UTF-8" means, java has found a character which is not representing in UTF-8. Hence open the file in an editor and set the character encoding to UTF-8. You should be able to find a character which is not represented in UTF-8.Take off this character and recompile.

    0 讨论(0)
  • 2020-11-27 03:36

    I'm in the process of setting up a CI build server on a Linux box for a legacy system started in 2000. There is a section that generates a PDF that contains non-UTF8 characters. We are in the final steps of a release, so I cannot replace the characters giving me grief, yet for Dilbertesque reasons, I cannot wait a week to solve this issue after the release. Fortunately, the "javac" command in Ant has an "encoding" parameter.

     <javac destdir="${classes.dir}" classpathref="production-classpath" debug="on"
         includeantruntime="false" source="${java.level}" target="${java.level}"
    
         encoding="iso-8859-1">
    
         <src path="${production.dir}" />
     </javac>
    
    0 讨论(0)
  • 2020-11-27 03:39

    Thanks Michael Konietzka (https://stackoverflow.com/a/4996583/1019307) for your answer.

    I did this in Eclipse / STS:

    Preferences > General > Content Types > Selected "Text" 
        (which contains all types such as CSS, Java Source Files, ...)
    Added "UTF-8" to the default encoding box down the bottom and hit 'Add'
    

    Bingo, error gone!

    0 讨论(0)
  • 2020-11-27 03:39

    For IntelliJ users, this is pretty easy once you find out what the original encoding was. You can select the encoding from the bottom right corner of your Window, you will be prompted with a dialog box saying:

    The encoding you've chosen ('[encoding type]') may change the contents of '[Your file]'. Do you want to reload the file from disk or convert the text and save in the new encoding?

    So if you happen to have a few characters saved in some odd encoding, what you should do is first select 'Reload' to load the file all in the encoding of the bad characters. For me this turned the ? characters into their proper value.

    IntelliJ can tell if you most likely did not pick the right encoding and will warn you. Revert back and try again.

    Once you can see the bad characters go away, change the encoding select box in the bottom right corner back to the format you originally intended (if you are Googling this error message, that will likely be UTF-8). This time select the 'Convert' button on the dialog.

    For me, I needed to reload as 'windows-1252', then convert back to 'UTF-8'. The offending characters were single quotes (‘ and ’) likely pasted in from a Word doc (or e-mail) with the wrong encoding, and the above actions will convert them to UTF-8.

    0 讨论(0)
  • 2020-11-27 03:39

    The following compiles for me:

    class E{
       String s = "^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[~#;:?/@&!\"'%*=¼.,-])(?=[^\\s]+$).{8,24}$";
    }
    

    See:

    enter image description here

    0 讨论(0)
  • 2020-11-27 03:42

    In eclipse try to go to file properties (Alt+Enter) and change the Resource → 'Text File encoding' → Other to UTF-8. Reopen the file and check there will be junk character somewhere in the string/file. Remove it. Save the file.

    Change the encoding Resource → 'Text File encoding' back to Default.

    Compile and deploy the code.

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