java file in Eclipse got messed up with weird characters - java

My java file got messed up. It has some binary encoding according to this site: http://lajm.eu/emil/encoding/. It says it has: application/octet-stream; charset=binary
So how is my java file suddenly encoded in binary? And more important, how can I recover my code?

You can check your file encoding by going to edit->set encoding. see what does it say. Before you panic also be sure to restart Eclipse. It sometimes happens that strange characters are shown, but usually a restart fixes it.
EDIT As for how can you restore a version of your file: Every time you modify a file in Eclipse, a copy of the old contents is kept in the local history. (see here). You can find your file in: .metadata\.plugins\org.eclipse.core.resources\.history of your workspace. Hopefully this will help you.
EDIT 2 Ah actually the file there is ugly looking binary, but at least is stored. You can try from Eclipse: select your file -> right click -> replace with -> local history.

Related

IntelliJ keeps switching to UTF8 (I want to set CP-1252)

I have some projects which are encoded with Windows-1252/CP-1252 and I can't change the encoding. The problem is, no matter what I do, intelliJ will keep trying to read these files as UTF-8 unless I manually put every single file in the encoding list.
That requires a lot of time and effort, it's error-prone and it's not a solution at all. I have set the entire project and IDE encoding as CP-1252 but it keeps trying to read files as UTF-8 anyway.
I don't know what causes that. We are using Subversion to commit files and maven to compile (which uses UTF-8 to read files except for the super POM which uses CP-1252).
Any idea how to solve the problem? I gave a look at other posts but I found no real solution yet. I'm currently using the last IntelliJ version (2017.1.2)
I actually found out what was the problem. Maven project encoding was overriding Intellij configurations. I tried to edit the source encoding property before but it didn't work because I misspelled Cp1252. Now it seems to work.

File was loaded in the wrong encoding:'UTF-8' in IntelliJ IDEA

Before asking the question, I searched for a similar question on StackOverflow:
file was loaded in the wrong encoding: 'UTF-8' in android studio
But it did not give an explicit answer there.
My issue is that the project is downloaded from the network, and it is an eclipse project, I open it with IntelliJ IDEA, and get this issue:
As Tarik point out, click the Reload in another encoding, and if you want UTF-8 encoding, then click the more -> UTF-8.
in my case , when i uploaded the first .ttf file in assert folder , android studio asked me to choose one file type , and when I selected text type . this problem start to appear .
the right file type is : 'Files Opened In Associated Applications'.
to fix your problem , you have to open preferences ->Editor ->File Types and search for .ttf extension then remove it and add it again under 'Files Opened In Associated Applications'
I was having the same problem..
and i solved it by changing the CR to CRLF
AND then refresh the project
like done in following image
You can try opening your file with an other text editor, like Notepad++. Sometimes it can detect the correct encoding of your file, using a different pattern than IntelliJ.
Then you will be able to use the "Reload..." of IntelliJ.
Click on setting symbol just right side of error and select suppress for class.
That works for me and resolve my issue.
My colleague works on Windows while I work on Ubuntu 16.04
Click the Reload in another encoding, and click the more -> GBK.
Then we can see the Chinese character.
I tried all possibilities posted here but any success with them, what worked was just to re import the project
Though the encoding setting was present in the pom.xml, IntelliJ was trying to load all test files using UTF-8 instead of ISO-8859-1, so I found this in the IntelliJ docs: Encoding in IntelliJ
Which, it basically was...
Open preferences (in macOS hit, command + ,)
Go to Editor / File Encodings
Select the project correct encoding if it defers from your pom.xml - project.build.sourceEncoding
By doing so, it did stop asking me to reload the file with the correct encoding.
If you have previously loaded this file without problems, and this is a new issue for you, try invalidating the caches in your IDE.
An easy way to open this option is by opening the super-search functionality (hit shift twice) and search for 'cache' in the 'actions' tab.
When prompted, choose the following option:

Java File Corrupted

Yesterday, i've Closed Eclipse & then Turned Off The Computer, Now it's appear that a Crash has occured, when i started Eclipse again, Project List was empty So i've imported the Projects Again,
Problem : a Java Files is now corrupted, the file size seem correct, i can Open the File but the File is filed with "NUL" when i open it with a Text Editor & is empty when i open it in Eclipse,
I've tried to use the History Features of eclipse but there is No Any Backup in the .History Folder, only empty folders,
Is there any way to recover this Damaged .Java File ?
Thanks
if you use SVN, or other version controls, or ever back up your PC, you could look at restoring it from there. However, by your description, this doesnt seem likely.
Noting from your comments you have tried a system restore.. System restore points and backing up your PC are two different things.
Right click on your file -> Properties.
The click "Previous Versions" along the top.
It may say "There are no previous versions available", or may offer a restore choice.
Perhaps if you had previously deployed the project you can get at the class file and de-compile the class back to code but that may still result in some loss. Also just try doing a search on your machine for that file name perhaps it was backed up by you at an earlier time that you forgot about.

IntelliJ 13 Ult. Changes encoding randomly

So I have many modules that have different encodings (UTF-8 and ISO-8859-7)
And I set their respective encodings in the File encoding section of settings
and my maven builds state the encoding of each project as well. I also have these files in git and I am not alone in these project ie other people commit in them as well.
What happens for the last month is this:
Randomly on some files the encoding changes to UTF-8 and when I try to change it back to ISO-8859-7 it doesnt change when I select the option "Reload" it does nothing. The encoding does not change.
What I have to do is the following steps:
Select ISO-8859-7 as encoding
Press Convert in the options
then when the file is marked as changed in git I revert it
only then does the encoding go back to normal
My IntelliJ version:
My question is this:
Is this a bug or am I doing something wrong/missing something that I should have configured?
UPDATE:
I want to add some more info I have found useful
The problem appears every time I open IntelliJ and on files that I have fixed their encoding the day before.
(I dont know if this is related but it happens as well) When I
convert the file to the ISO encoding the encoding and then revert the
file so that I can see the text, the git branch becomes detached
Also I am the only one working on this project so noone else changes
the files commited in git
I was able to reproduce this by committing a project with mixed files to Git without IntelliJ IDEA project files, which is what should be done usually because people use different IDEs.
Then if I clone the project to another directory and create a new IDEA project, the encoding settings are different, but even if I correct them, the first time a Greek file is loaded IDEA thinks it is UTF-8 because "Autodetect UTF-encoded files" is active. If the setting is inactive it is the other way around: Greek files will be displayed correctly but UTF-8 ones are shown as Greek.
Whatever went wrong, I was easily able to switch the encoding and select "reload" from the pop-up dialogue.
Then I committed the IDEA files (.idea subdirectory plus MyProject.iml file) to the same repository and cloned again. This time the files were both displayed correctly. So this is my suggested workaround. Hopefully it works for all IDEA users in your project. I have no idea what happens if e.g. an Eclipse user adds a new file to the repo which is still unknown to IDEA when you pull/fetch next time. Probably you will have to assign the right encoding again.
So how does IDEA remember which files or directories have which encoding? Look at the file .idea/encodings.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding" useUTFGuessing="true" native2AsciiForPropertiesFiles="false" defaultCharsetForPropertiesFiles="ISO-8859-7">
<file url="file://$PROJECT_DIR$/src/utf8/Unicode.java" charset="UTF-8" />
<file url="PROJECT" charset="ISO-8859-7" />
</component>
</project>
There you can see that I have ISO-8859-7 set as the default for normal and properties files and UTF guessing switched on, just like you in your screenshots. You also see that project file $PROJECT_DIR$/src/utf8/Unicode.java has an UTF-8 encoding. What you do not see listed there are files with Greek encoding because that is the project default anyway.
Bottom line: If you at least commit .idea/encodings.xml you can preserve the encoding information. This is not a bug in IDEA in my opinion but a normal thing. How can IDEA know the encoding if you do not have a saved project state? Encodings are not stored in Git or on the file system, it is meta information. This is probably why XML files mention their own encoding right at the beginning on the very first line (as you can also see above in my snippet). Java files do not have such an encoding tag. So for mixed encoding projects you just need to be careful. ;-)
Update: My test repo is on GitHub if you like to play around.

Linux - Java/File encoding

I've searched around the web a while now and haven't found anything giving me a proper answer.
I've got a linux server running debian and a bukkit server, I've rusn my server on windows before and my files seems to go right with UTF-8 encoding. I uploaded my files via winscp and now they seems to be ASCII or something else. Because ingame and also in the files every special char, like umlauts changed to placeholders and ingame to questionmarks.
I've tried to change encoding of a file (would be hard to do this for every file... asspecially if I need to to that everytime uploading a new one) but it only changed to a single questionmark instead of these placeholder stuff.
For jenkins I needed to change encoding via encoding=... in the javac execution in my build.xml but I don't know any flag to change encoding for the java cmd.
I also read it should be possible to change the encoding for the whole java but the tried cmds didn't worked at all.
I would be happy to get some tips how to fix this or in general how to avoid converting every file I upload...
Thank you very much :)
~Julian
You can try
java -Dfile.encoding=UTF-8 *.jar
to run a java project in specific encoding no matter what default encoding the current system use.
if you intend to change all files in a project to a specific encoding in eclipse
right click on your project in project explorer -> Properties(or Alt+Enter) -> Resource -> look on the right, you can see Text File Encoding, Then you can choose UTF-8 as needed.
Remember to check all your packages(right click and check Text File Encoding part) that they all inherited from container.
Hope this help!

Categories