views:

90

answers:

4
+1  Q: 

reformat java code

Hi again! I was working with a project that was saved in linux I opened it in a windows IDE and the strange thing is that it inserted break lines in all .java .jsp and other text files

I like to remove all those empty lines recursively at the root directory of the project.

Does anybody have some shell script that can I run to change these files?

Thanks in advance, André.

+2  A: 

You might just need to run dos2unix on the files. Pass it through a hex viewer to see if the line-endings are 0D 0A (CR/LF) or are actually two newlines or something else. If the former, dos2unix will work. Something like:

find . -name \*.java -exec dos2unix \{\} \;

From your root source directory. Test first, make backups, etc.

Mark Peters
It sounds like he's in the converse situation: his source is from a Linux system (lines terminated with LF) and now he's on Windows. Is there a `unix2dos`?
TMN
There is, but no guarantee it's available on Windows. I'd need more information to know exactly what's going on (what is "it", what were the binary changes that were made to the files, etc).
Mark Peters
+1  A: 

Which Windows IDE are you using? Is this Eclipse? If yes.

Window -> Preferences -> Java -> Code Style -> Clean Up -> Edit -> Remove Trailing whitespace

Hopefully this will work

sjt
I'm using NetBeans, it have a similar function Menu: Source -> Remove Trailing whitespace... But it doesn't remove the empty lines.
AndreDurao
A: 

On a sidenote, not directly being an answer to your question (it seems like there already are good answers), I would recommend, if possible, to use a bit of time to uniformly setup your IDE environments so that they are compatible.

I have great experience in using Eclipse as it is available for all major OS platforms, and has the option of saving essential project settings in whichever repository you are using, thus enabling you to have identical setups so that for example line breaks are handled properly.

Also, to ensure everybody has the same Eclipse configuration, plugin versions, even basic workspace configurations, I strongly recommend using Eclipse configuration management tools like Yoxos, as it enables you to specifically configure a Eclipse configuration per project, and distribute said configuration in a simple XML-based file associated with the Yoxos Launcher program which handles dependencies, updates etc.

I have been involved in quite a few projects thus far, and even in teams where we are developing from Linux, Windows and Mac OS X, there are no (or well as minimal as possible anyway) problems with configurations, line breaks, plugin versions, etc. due to the extra effort in streamlining the IDE setups.

micdah
A: 

You might want to see if you can configure your IDE/editor to deal with the files instead of converting them. If you use source control, changing all your line endings will flag every line in every file as "modified", and that will make it difficult (if not impossible) to track changes if/when you migrate the code back to your Linux system.

TMN
This project it's not on source control (for a while), but as soon as I fix this double space problem I'll import it to SVN.
AndreDurao
others answers none of them worked, because when I take a look with a hex editor the break lines in files are mixed (OD) and (ODOA) in the same files, so unix2dos didn't solve, and Remove Trailing whitespace neither. I also tried run shed command but it didn't help either.
AndreDurao