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

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.

Related

Minor Edits to Class File inside JAR

I have a custom legacy application built in Java that I need to make a very minor edit to. I need to change a reference to Year of Week 'YYYY' to Year of Day 'yyyy' in one class file.
I found out that vim can edit the class files within JARs which seemed the easiest solution, so using that to make the edit a copy of the JAR. However, when trying to run the new JAR I am hitting exceptions for the class I edited stating:
"Exception in thread "main" java.lang.ClassFormatError: Extra bytes at the end of class"
Looking at the file sizes, it looks like even though all I did was change 'YYYY' to 'yyyy' on one line, the file is 52 bytes larger than the original. I think vim is adding extra characters when it saves. I tried 'set nofixendol', 'set binary noeol', 'set binary noendofline' before saving since I saw people mentioning vim adding endoflines, but it hasn't helped.
It's such a minor change in a huge bundle of custom code, but I can't seem to actually make the change.
Previously tried to compile from source or from decompiling the JAR, but always failed due to dependency issues, no idea how it was originally compiled. Tried including the original JAR when compiling and it still didn't work. That's why I was so hopeful when I found vim could edit it.
Any ideas on what's going wrong with vim editing or other ways of making the minor change to this app would be appreciative. Losing my mind a bit here.
You should use a hex editor (if you're using Linux, hexedit should be available in command line) to edit binary files. Text editors usually adds something to the content or convert characters.
Oh, and if you want to make more complex changes JBE is pretty cool.

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:

Eclipse Luna takes a long time saving an xml file :(

About a month ago I went from Windows to a MacBook Pro.
Same Eclipse version, (almost) same settings, but it frustrates me that saving an xml file takes about 10s :(... Even if I just delete/add a space char.
I have a lot of files, about 10 projects opens in Eclipse but my old Windows box with an old quad core (not even an i7) had no problems with this.
I know this is a knows problem in Eclipse and I had it in the past as well, but it was solved after a few upgrades in the past.
Currently I run on the latests Luna version, with up2date plugins, tried a lot of possible solution, most from SO, but still not solved. Please some help.
I am using EE Eclipse to mainly develop java web apps and use the following plugins: AspectJ (with Xref), EGit, JGit, CheckStyle, Maven, TestNG.
All files are stored as UTF-8
Editing the xml file is slow in both the text and the xml editor.
It concerns xml files that are located in a java package (in the /src/main/java/ folder).
The strange thing is that Editing an xml file in a project-root subfolder isn't slow, like: project-root/test-output/bla.xml (works well in both the text and xml editor)... weird...
Both files (the slow and fast save one) are part of version control and used by Git.
In case I copy the slow file to the folder project-root/test-output/, it's also saved fast :(
Renaming the slow file such that it has the "txt" extension and editing with the text editor has not effect.
I thought it is caused by some builder that works only on the java source folder...
The project has a Java and Maven builder. Disabling them has no effect.
The Eclipse log file doesn't contain any relevant exception. It contains some key-binding exceptions and resource tree locked modifications, but these exceptions are old.
What I tried (what I can remember):
+ Disabled all validation (especially the xml validation), for all projects.
+ Ensure all latest updates are installed of all plugins, including the EGit plugin.
+ Disabled Checkstyle.
+ ...
Any ideas as I am getting a bit frustrated about this?
A poor-man's solution may be in
switch to a empty workspace (workspace2 in example)
create a new pde-plugin-project (mytestplugin in example)
debug the mytestplugin means you debug a instance of eclipse too, so use the workspace you usually work with in the debugging-second-eclipse.
Now: save the xml-file in the debugging-second-eclipse with the workspace you usually work with, quick jump to the first eclipse and press "pause" to stop the second-eclipse.
inspect the compleate debug-stacktrace-thread-tree in the debug view (please post the image as a comment here).
Regards
When Trying the solution of #Peter I noticed that the workspace I was using was located in the "eclipse.app" file like this:
/Applications/eclipse/Eclipse.app/Contents/MacOS/D:\Users\Ed\Develop\Projecten\EclipseWorkSpace/
No idea why/how this happened. However, I moved this workspace to a "normal" directory in my home folder, and how saving an xml file work normal, fast as expected...
No idea why having the workspace located in the eclipse.app made it slow, but it works ...
Open eclipse.ini.
Increase -Xms, -Xmx
This works for me.

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