Automatically pretty format large number of Java source files - java

I have many Java files from a project which have not been formatted properly. Also due to enforcement of a common style many people have introduced their own coding style. I want to bring all this code to one consistent format. Are there any auto-formatters available which I can run as script on all the files. I would like to rethink and fine tune following options:
Number of spaces used for indentation.
Spaces around operators like + - * / etc.
Separation between parentheses.
etc.
I was looking at Netbeans auto format and looked cool. Moreover the parameters can be set from the option dialog. However I am not able to completely grasp how to use the API

You can use the Eclipse JavaCodeFormatter from the command line:
The following example runs the formatting of code specified by the
configuration file D:/formatter.prefs on the files Java belonging
(directly or indirectly) to the directory D:/tmp/src :
eclipse -application org.eclipse.jdt.core.JavaCodeFormatter -config D:/formatter.prefs D:/tmp/src
See http://wiyoo.blogspot.com/2007/05/batch-formatting-java-source-code-with.html for details.

Why use the API? Bring it in as a NetBeans project. Same with Eclipse or IntelliJ; they can all format all source files in a project.
For standalone, try Jalopy. It can also be set up to run as part of a build, as a commit hook, etc.

Try JIndent. Or here're a bunch of open source code formatters for Java, some of them can be run as standalone products, others as plugins inside an IDE (Eclipse, NetBeans).

Well I don't know if you use eclipse, but to do spacing and parenthasis you just press ctrl + shift + f and to do auto indentation is ctrl + i

Related

Just installed vim-javacomplete2 but doesn autocomplete at all

I am newbie to plugins in vim. I am using vim for a year but without any experience with plugins. So i installed Vim-plug, then add Plug vim-javacomplete2' to my .vimrc and :PlugInstall it. It was installed. Then I add autocmd FileType java setlocal omnifunc=javacomplete#Complete to my .vimrc as well - as is in github. But then, ther is nothing else. So some commands works. I Tried for example JCgenerateAccessors - which generate getter and setter upon my private fields. So commands works. But The autocompletation doesn (such main function). I am making classes within a file in my directory. Then javac Test.java and java Test - which runs it. But still, when editting in vim the Test.java, no autocompletation. What I did wrong?
Might a a bit late
<C-x><C-o> (ctrl + x,ctrl + o) will bring up a list of options.
Press this in insert mode wherever you would press ctrl + space in other IDEs
i.e.: after System.
Expanding upon #iaquobe's answer, autocompletion while typing or more specifically after the dot notation, isn't enabled by default with this plugin, unless triggered manually using C-x,C-o, which can -and will- become tedious over time.
There're other autocompletion plugins that can be installed along with other completion engines, and be configured to integrate with them, namely: vim-mucomplet, youcompleteme, and neocomplete.vim.

Is there a possibility in IntelliJ (or other free IDE/Tool) to indent whole project (only indentation without formatting))

I want to format code in my project. First of all I want to make consistent indentation in all the java files.
I know there is CTRL+ALT+L option, but it also formats other things - spacing in braces and so on.
CTRL+ALT+I is what I want to do, but automatically on all classes in project.
I checked Eclipse plugin, JIndent tool and none of those tool meet the requirements.
What do I expect is having all java files in a consistent indentation style (2 spaces) and all other aspects of formatting not changed.
Thanks for any suggestions.
I would suggest to use Google Java code formatter which brings uniformity in both Eclipse and Intellij Idea IDE. I provide below the link.
https://github.com/google/google-java-format
You can install the plugin in eclipse and make changes in Eclipse Preference page. In case of Eclipse, you have to use the key command as CTRL+SHIFT+F
In case of Intellij IDE, you have to use the key command as CTRL+ALT+L to format the code.
You can see the screen shot for Eclipse preference Page below.
You can see the screen shot for Intellij Idea setting page below.

How to use sublime for java development

I want to use sublime as development tool instead of eclipse, but although sublime enables to highlight the code, but how to detect the errors and compile the java code as eclipse does?
Thanks!
Sublime Text is a text editor, not an IDE. You'll need to compile it from command line using javac File.java. And it won't highlight errors for you.
If you want a simpler text editor how Java, consider jGrasp or NetBeans.
All the answers above are totally correct: Sublime Text is a text editor, not an IDE. So you'll either use an IDE for your projects (for the ease) or use Sublime + cmd line (with a little bit of pain)
However, I personally kinda like it too, especially Sublime Text 3. So I use both: intellij for real/big projects and SBT3 for small things/Code Snippets(when I read books for example) (+ quick & repeatable unit tests to assert results, no need of a bare main()) that I want to try. And this can go very fast if you're used to SBT3 shortcuts (because it is a very lightweight and fast editor) instead of launching a full blown intellij instance + gradle + etc. just for those dimple things.
Another reason for using SBT for these simple things (especially when you are learning) is that you get rid of auto completion, auto import, etc. It forces you type everything and you learn a lot!
To enable highlighting + error check on SBT3, you need to install:
Package Control
SublimeLinter + SublimeLinter-javac from Package Control (https://packagecontrol.io/packages/SublimeLinter-javac )
Seti_UX theme from Package Control (for great highlighting)
You can refer to this as an example:
How to use JUnit with Sublime Text 3
Sublime is not really ideal (imo) for Java development. If you don't want to use eclipse, you can take a look at intellij, my favorite IDE for Java and web projects.
Of course, you can use sublime for you Java projects. But you have to compile the java files. The way you do that is from terminal/dos is to write:
javac myJavaFile.java
and then you run the javac file:
java myJavaFile
If you want all the multiple selection and other cool stuff in sublime for your java development then Intellij IDEA is your choise, you'll have more cool features that are just not possible with text editor.
I love sublime but that is not the case with compilable languages like java or C++...
There you can see Intellij in action http://tv.jetbrains.net/videocontent/refactoring-in-intellij-idea-live-by-robert-c-martin-uncle-bob

reformat java code

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é.
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.
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
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.
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.

Java Code Formatting in Textmate

Is there a way/bundle/shortcut to format the Java Source in TextMate?
(As we do with Shift+Command+F in Eclipse).
It must be
Command + Option + [
which is also Text>Indent Selection
Ctrl+Q
Although It may not be what you expect :(
alt text http://img27.imageshack.us/img27/97/capturadepantalla200910w.png
Warning: I got to the end and realized format was the one feature this DOESN'T have. I currently use this process, and then alt+tab to Eclipse to format. Neither TextMate nor Eclipse seem to have a problem with concurrently editing the same file and it works beautifully. However I realize this wasn't the original question.
I use eclim and the eclim TextMate bundle. I'm not gonna lie, you do have to run Eclipse to use it. However you can disable the GUI and run Eclipse as a headless daemon or just minimize Eclipse in case you need it for something later. It's a good compromise and well worth it for the features it provides to TextMate.
This gives me :
Code Completion
Auto-import missing classes
Java Rename (Refactor)
Window with build errors
Each of these features provides the same functionality as Eclipse itself.
Currently the bundle only supports Java, but eclim itself (which is an Eclipse plugin originally written for vim) supports many more features and languages, and is just waiting for the right person to come along and implement Eclipse features for everything.
You can find eclim here:
http://eclim.org/
And the eclim bundle is in GetBundles.
If you don't have GetBundles, I recommend it. It's a bundle that allows you easily download new bundles from several repositories, all from within TextMate.
run these commands in terminal to install it:
cd ~/Library/Application\ Support/TextMate/Bundles
svn co http://svn.textmate.org/trunk/Review/Bundles/GetBundles.tmbundle/
If you prefer not to install it, the eclim textmate bundle is here:
https://github.com/JulianEberius/Eclim.tmbundle
When you run the eclim setup, choose the Eclipse folder that contains 'plugins'. Then, when you launch Eclipse, go to Window->Show View->Other. Choose 'eclim' from 'eclim'. This view needs to be running for the TextMate bundle to detect the eclim server. You should see some information go through the console window, with the last line saying something like: Eclim Server Started on port 9091. If so, it should work in TextMate.
You know what, I just went to grab the keyboard shortcut for you, and realized 'format' is the one I always switch to Eclipse to use. Sorry bout that. But hey, eclim is fantastic anyway and maybe when I have some time I'll write the format command for TextMate. The source is in Python and looks relatively straightforward.
Probably what you want is this
Command + Shift + {
Works for any language

Categories

Resources