Is there something similar to the Eclipse cleanup rules (Preferences > Java > Code Style > Clean Up) in NetBeans?
The cleanup rules in eclipse will allow you to clean things up like organizing imports, removing unnecessary casts, adding missing override annotations etc.
Also can you do that on a whole set of classes/packages instead of individual classes?
Refactor > Inspect and Transform
Is there something similar to the
Eclipse cleanup rules ((Preferences >
Java > Code Style > Clean Up) in
NetBeans?
In NetBeans 8.0, the powerful batch tool is Refactor > Inspect and Transform. See the tutorial page, Refactoring with Inspect and Transform in the NetBeans IDE Java Editor.
Individual tools are also available:
Fixing importsFor a single file, namespace, or project (depending on what's selected in Project window or has the focus): Source > Organize Imports (to sort and remove unused imports) or Source > Fix Imports (to sort, remove unused imports, and add missing imports). Or during every save: Tools > Options > Editor > On Save > Organize Imports.
Removal of trailing spacesFor a single file (place the carret in the code file): Source > Remove Trailing Spaces. Or during every save: Tools > Options > Editor > On Save > Remove Trailing Whitespace.
Code FormatFor a single file, namespace, or project: Source > Format. (Customize the rules in Tools > Editor > Formatting). There is also a plugin called Jindent you can install (I have not used it myself). Or during every save: Tools > Options > Editor > On Save > Reformat.
By default, Netbeans will display hint icons next to problematic lines of code and in the scrollbar, allowing you to perform an automatic fix if desired. These can be configured via Tools > Options > Editor > Hints. Netbeans can search all problems in the project using Source > Inspect. Or, to reiterate, many of these problems can be batch fixed with Refactor > Inspect and Transform.
The equivalent of Eclipse's "Code Cleanup" in NetBeans is "Format". It's under Source > Format, and the keyboard shortcut is Alt+Shift+F (on Windows). Unfortunately, unlike Eclipse, it doesn't seem like this can be configured in NetBeans.
For Netbeans use Ctrl+Shift+I to remove unused import from the file.
NetBeans 7.2 has Inspect & Transform refactoring to do this:
http://netbeans.org/kb/docs/java/editor-inspect-transform.html
tools -> options -> Tab "Editor" -> Tab "Hints" -> select Java (talking about) in JComboBox
and then you'd see yellow ocean ..., great and quick from ver 6.9
I know that netbeans points out these things automatically, but beyond merely pointing them out I don't know.
Right click on the project (or a package if you want), then choose "Inspect and Transform". In the dialog box, choose "Organize Imports" , from browse choose imports --> then --> Unused Imports
I don't know a way to do this en mass, but if you delete all imports from a source file and then right click in the source editor, you can select fix imports. This will import all the classes for you alphabetically, asking when it encounters package ambiguities.
As Soldier.moth pointed out, Netbeans will point out other issues, like casts and override annotations, in line, by use of a light bulb to the left of the source.
There is an Organize Imports plugin Read the DZone article for more info.
EDIT: I see there is a bug report to get this as part of the standard distribution.
If you just want to make your code have proper indentations on a whole project, simply click the project name, go to "Source," and then click "Format." Hope this helps!
Related
I'd like to configure the organize import in Eclipse with one caveat: I do NOT want Eclipse to CHANGE the imports related to star imports (like import javax.xml.parsers.*).
I would like Eclipse not to expand (replace the star import with each specific ones) if there is already a star import, but also not automatically add the star when there is not.
In fact, I would like the organize import to just organize the imports (ie reorder), not change them. Any idea ?
I like it when IDE do not mess with developer's will.
The Organize Imports command can only be executed as a whole
(see source code) and the Sort Members command ignores import statements.
As workaround to reorder/sort the import statements alphabetical use the AnyEdit Tools plug-in:
Select the lines of import statements to reorder/sort
Right-click selection and choose Sort > Case-Insensitive A-Z
For .* import statements only, set the number of imports needed for .* to 0 (see Eclipse help - Organize Imports Preferences):
Open Window > Preferences: Java > Organize Imports
Set Number of imports needed for .* (e.g. 'org.eclipse.*') to 0 (instead of 99)
Click Apply and Save
For adding import statements manually or via content assist only, disable Organize Imports on save (see Eclipse help - Clean Up Preferences):
Open Window > Preferences: Java > Code Style > Clean Up (or for project specific settings: right-click project, choose Properties: Code Style > Clean Up)
Click Edit...
In the Code Organize tab uncheck the Organize imports checkbox
In the Unnecessary Code tab uncheck the Remove unused imports checkbox
Click OK and Apply and Save to close both dialogs
I am working on a Java project that has a lot of compiler warnings. I can't fix all of them at once, but I would like to ensure that each file that becomes warning-free remains warning-free. Is there a way to turn warnings into errors in specific files or packages? I'm interested in generating the errors with either Eclipse or javac.
In your Eclipse, go to Window > Preferences > Java > Compiler > Errors/Warnings:
Here you could switch any Warning entry to Error. Beware some entries may be hidden by expandable panels, so make sure you expand them all.
At the top right corner you can also select "Configure Project Specific Settings.." if you want to apply this configuration to only a project and not to all projects by default. Alternatively, you can achieve the same by right click on the concerned project > Properties > Java Compiler > Errors/Warnings entry.
Hence, this will be applied to either the whole workspace or just a project, but not to specific files or packages, you don't have this granularity in Eclipse.
This should help you to achieve it, although it might become time and effort consuming to keep it clean, but that's another story.
Moreover, from the Problems view you can create a new Problem View, then from its menu (right top corner of the view, as shown by the picture below), you can configure it.
Once created a new Problems View, you can again use the same menu and select the Configure Contents.. entry. From there, you can create a New configuration (see picture below) and define it only for the select element (if you previously selected the concerned class or package), and define whether you want to see any of Errors, Warnings, Info and from which category.
This configuration could help you out to narrow down and filtering errors and warnings on a specific package or class as a complement or as a different solution/approach than the description above.
Final note: I used Eclipse Mars to try it out, but the functionality should be there since many past versions.
Due to a massive refactoring, i ended up having a lot of unused imports across many files. Using Eclipse, is there a way to remove them all?
I think there is no one shot solution through cleanup.
1) right click on package then Source->Organize Imports
2)`Ctrl+Shift+O` .
Here is Save Actions another way which can even avoid Ctrl+Shift+O
Select all your projects and then hit Ctrl+Shift+O. It's also available at Source -> Organize Imports if you right click on the selected projects.
You can right-click on source folder in the project and select Source > Organize Imports. This will organize the imports for (recursively) all source files contained in that source folder. This can also be done on individual packages.
As for tying the "Organize Imports" action to a "Clean", that will be trickier. I think the closest thing would be to attach an Ant build.xml to the "Clean" stage for the project. You could set up the build.xml to invoke Jalopy, which can organize imports. I don't recommend this approach, as it modifies files behind Eclipse's back, which can cause frustrating issues.
As a compromise, you could go to Window > Preferences > Java > Editor > Save Actions >
[x] Perform the selected actions on save
[x] Organize imports
Right click the package then select Source->Organize Imports.
Visit http://www.ibm.com/developerworks/library/os-eclipse-clean/ .
Detailed HOW TO work with profiles and clean up your code.
Its very simple Genius!
Import packages : Ctrl + Shift + O and
Remove Unused packages : Ctrl + Shift + O
Does IntelliJ have an Organize Imports feature similar to that in Eclipse?
What I have is a Java file with multiple classes missing their imports. Example:
package com.test;
public class Foo {
public Map map;
public JTable table;
}
In Eclipse I could use Organize Imports and it would automatically import both java.util.Map and javax.swing.JTable. In IntelliJ I have to individually go to each class, select it, then press Alt-Enter. There is an Optimize Imports feature but all it seems to do is sort and expand the existing imports.
I'm using IntelliJ 10.5.
Ctrl + Alt + O (Code → Optimize Imports...) is what you're looking for, both on Windows/Linux and macOS keymaps.
It says "Optimize", but, if configured to do so, it will also:
organize existing imports
remove unneeded imports
add new required imports
add unambiguous imports on the fly
You can tune the auto-import settings under "Settings → Editor → General → Auto Import" as described by Dave.
You can also modify how the imports are auto-ordered under "Settings → Editor → Code Style → Java → Imports"
Under "Settings -> Editor -> General -> Auto Import" there are several options regarding automatic imports. Only unambiguous imports may be added automatically; this is one of the options.
Simple & short solution worked for me.
Go to File -> Settings -> Editor -> Auto Import -> Java (left panel) and make the below things:
Select check box for "Add unambigious imports on the fly" and "Optimize imports on the fly"
Refer this.
In addition to Optimize Imports and Auto Import, which were pointed out by #dave-newton and #ryan-stewart in earlier answers, go to:
IDEA <= 13: File menu > Settings > Code Style > Java > Imports
IDEA >= 14: File menu > Settings > Editor > Code Style > Java > Imports (thanks to #mathias-bader for the hint!)
There you can fine tune the grouping and order or imports, "Class count to use import with '*'", etc.
Note:
since IDEA 13 you can configure the project default settings from the IDEA "start page": Configure > Project defaults > Settings > .... Then every new project will have those default settings:
July 2015 - I have concluded that IntelliJ does not support automatically resolving imports with a single function. "Organize imports" simply removes unused imports, it does not resolve unimported types. Control-Space resolves a single unimported type. There does not exist a single action to resolve all types' imports.
navigate to the file where you want to organize imports or just click on the whole package or even click on the project than press CTRL + ALT + O
In IntelliJ 14, the path to the settings for Auto Import has changed. The path is
IntelliJ IDEA->Preferences->Editor->General->Auto Import
then follow the instructions above, clicking
Add unambiguous imports on the fly
I can't imagine why this wouldn't be set by default.
Shortcut for the Mac: (ctrl + opt + o)
Goto Help -> Find Action (Short Cut for this is Cntl + Shift + A) and type Optimize imports (Short cut for this is Cntl + Alt + O)
Just move your mouse over the missing view and hit keys on windows ALT + ENTER
ALT+ENTER was far from eclipse habit ,in IDEA for me mouse over did not work , so in setting>IDESetting>Keymap>Show intention actions and quick-fixes I changed it to mouse left click , It did not support mouse over! but mouse left click was OK and closest to my intention.
That plugin will automatically do the "organize import" action on file save: https://github.com/dubreuia/intellij-plugin-save-actions.
To install: "File > Settings > Plugins > Browse repositories... > Search 'Save Actions' > Category 'Code tools'". Then activate the "organize import" save action.
I finally created a workaround around this frustrating issue. I'm not completely happy with the workaround, but it's better than nothing.
Basically, after you paste the source code and unambigous imports are fixed, just press F2 to highlight the next compiler error. If the current error is an import-missing error, press Alt+Enter, then Enter to select the Import option, then pick the correct import. Then, press F2 again.
If you are missing just one import (the class name has red underline), click and hover the mouse over it, and a blue suggested import statement will appear. If you hit, Alt + Enter at this point, the import will be included in the file and the red underline should disappear.
Shortcut on Android Studio on MacOS: Control + Option + O
I did not have any wildcard * as mentioned in one of the answers, neither did any of the formatting through Android Studio mentioned worked
What helped was running this:
./gradlew ktlintFormat
I've downloaded, unzipped and setup Eclipse 3.4.2 with some plugins (noteable, EPIC, Clearcase, QuantumDB, MisterQ).
Now I find when I'm editing Java projects the code completion is not working. If I type String. and press ctrl+space a popup shows "No Default Proposals" and the status bar at the bottom shows "No completions available".
Any ideas?
Try restoring the default options in 'Windows > Preferences > Java > Editor > Content Assist > Advanced'
An example of the kind of data you see in this preference screen, however not necessarily what you currently have.
(From Vadim in this blog post " Content Assist Duplicates in Eclipse (Mylyn)":
if have duplicate Mylyn entries, uncheck the duplicate entries that do not contain "(Mylyn)" in their name)
The Eclipse help page defines the default list to restore:
Select the proposal kinds contained in the 'default' content assist list:
Other Java Proposals,
SWT Template Proposals,
Template Proposals,
Type Proposals
I'm adding an answer here in case someone else finds this on Google. Same symptoms; different problem. For me, the type caches had become corrupt.
From http://mschrag.blogspot.co.nz/2009/01/open-type-cant-find-your-class.html
Quit Eclipse
Go to workspace/.metadata/.plugins/org.eclipse.jdt.core
Remove *.index and savedIndexNames.txt
Restart Eclipse and search Ctrl+T for the offending type. The indexes will be rebuilt.
In case someone comes here and want to activate the autocomplete function, go to
Preferences -> Java -> Editor -> Content Assist.
Then in the Auto Activation section fill in Auto activation triggers for Java:
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ._
For those running Xfce + having IBus plugin activated, there might be keyboard shortcut conflict.
See more info on my blog: http://peter-butkovic.blogspot.de/2013/05/keyboard-shortcut-ctrlspace-caught-in.html
UPDATE:
as suggested by #nhahtdh's comment, adding the some more info to answer directly: IBus plugin in Xfce uses by default Ctrl+Space shortcut for keyboard layout switching. To change it, go to: Options and change it to whatever else you prefer.
Check the lib of your project. It may be that you have include two such jar files in which same class is available or say one class in code can be refrenced in two jar files. In such case also eclipse stops assisting code as it is totally confused.
Better way to check this is go to the file where assist is not working and comment all imports there, than add imports one by one and check at each import if code-assist is working or not.You can easily find the class with duplicate refrences.
Another solution which worked for me is to go to Java--> Appearence --> Type Filters and do disable all
None of these worked for me.
I was experiencing this issue in only once particular class. What finally worked for me was to delete the offending class and recreate it. Problem solved... mystery not so much!
If you have installed Google Toolbar for IE, may be you can face the same problem. Because, the toolbar capture the shortcut ctrl+Space.
I had this problem and like #Marc, only on a particular class. I discovered that I needed to designate Open With = Java Editor. As a Eclipse newbie I hadn't even realized that I was just using a plain text editor.
In the package explorer, right-click the file and chose "Open With".
I faced this problem, and spent hours trying to figure out the issue. tried to follow the steps mentioned in the different answers above, the solution I found is on the same lines as Mona suggested, but slightly different. Tried to add as a comment to Mona's answer but no option was available.
Issue with my eclipse was, classpath somehow got corrupted and all the jars and dependent projects were missing. after taking the latest .classpath from repository it worked fine.
Check that you did not filter out many options inside the Window > Preferences > Java > Appearance > Type Filters
Items in this list will not be appear in quick fix, be autocompleted, or appear in other various places like the Open Type dialog.
I also face this issue but it is resolved in different way.
Steps that I follow may be helpful for others.
Right click on project (the one you are working on)
Go to Properties > Java Build Path > JRE System Library
Click Edit... on the right
Choose the JRE 7
Once you have you configuration checked and completion is still not working:
make sure you have the right directory structure.
Do you see the right icon beside the file?:
It will tell you how the file will be treated by Eclipse:
I am posting this answer as I had that story with with Maven webapp artifact. By default Maven-WebApp does not create folder for sources and I put my Java into resources, wondering for 5 minutes what was going on... :)
Running STS on Java Spring Boot projects, here's what works for me :
Maybe this helps other people who come across the same issue.
My setup: old Gradle project (version Gradle 2.12) made by someone else, imported using the Gradle Import Wizard into STS (Eclipse Oxygen.2 (4.7.2)).
Code completion did not work either (and I still have hollow Js at the Java files), but at least I got the code completion to work by doing:
right click on the project folder > Properties > Gradle > Configure Workspace Settings > Java > Editor > Content Assist > Advanced
check "Java Proposals in upper window.
2x Apply & Close
I have run into this problem since upgrading to Eclipse 2019-09. Based on some of the suggestions above, this is what worked for me.
I had to go to Eclipse -> Preferences -> Java -> Editor -> Content Assist -> Advanced.
I found out that if I turn on any of the key binding proposals, Java Non-Type, Java, Java (Task-Focused) or Java Type proposal, then I was able to use auto complete. If I turned them all on, then not only did auto complete work, but I got duplicate methods listed. I am guessing, but I will probably used Java Type Proposals. Any clarification of what differs for these four types would be appreciated.
In my case, Intellisense had only disappeared in a few classes in one project. It turned out this was because of a missing library on the build path (although it worked previously).
So definitely check all the errors or problems in Eclipse and try to find if a library may be missing
For those who use the latest 3-19 eclipse build:
It just happened to me when upgrading from Oxygen to 3-19 eclipse version, so I assume the auto-complete feature does not migrated correctly during the upgrade process.
The only solution that worked for me was to create a new eclipse workspace, and import the project/s to it. It might take a few minutes, but it worth it - comparing to the time spent on other solutions...
I ran into this and it ended up being I was opening the file with the text editor and not the java editor.
For me the issue was a conflict between several versions of the same library. The Eclipse assist was using an older version than maven.
I had to go to the .m2 directory and delete the unwanted lib version + restart eclipse.
I experience problems on Eclipse Neon when editing a file which does not belong to the project directory. When I copy the same file to the project root directory, not even to the src directory, the completion starts working.
When the file is opened from a different directory, only completion for JRE works. That is for example: java. completes, but junit. does not.
Just in case anyone got to a desperate point where nothing works... It happened to us that the content assist somehow shrunk so no suggestion was shown, just the "Press Ctrl+Space for non-Java..." could be seen.
So, it was just a matter of dragging the corner of the content assist to enlarge the pop-up.
I know, embarrassing. Hope it helps.
Note: this was an Ubuntu server with Xfce4 using Eclipse Oxygen.
If you're experiencing this in an enum, or when initializing an array with anonymous classes, it's a known bug in Eclipse. See Eclipse content assist not working in enum constant parameter list.
We can change the settings as per our requirement.
Suppose we want to make java proposal as highest priority we need to do changes as shown below.
Windows > Preferences > Java > Editor > Content Assist > Advanced
Choose Java proposal and click on up button
For me in Sep 2021 it was an odd Eclipse bug. I had a multi-line string inside an annotation in my Class. This caused just that particular class to fail when trying to code complete (even though the class compiled just fine).