How to delete specific import from each file in application? - java

In eclipse, how can I delete a specific import from each file in a package?
For example, I need to remove the following from an entire package:
import android.R;
I am NOT asking how to remove unused imports, just how to remove a specific line of code..in this case, an import.

Select the package in your Package Explorer View.
Choose Menu->Search->File
in the dialog set Containing text to import android.R; and File name patterns to *.java
for Scope - choose "Selected resources"
Click on Replace
The Search view will show all matches and you get a new dialog where you'll have to enter the replacement (leave it empty in your case)
Note, that this is not a refactoring. Replacing text may lead to dozens of compiletime errors, that have to be fixed manually.

Preferences->Java->Editor->Save Actions->Configure...
You can configure the removal of unused imports when saving from the Unnecessary Code tab.
Adding imports would normally be done as you are writing the code otherwise it won't compile.

ctrl-Shift-O-I is the shortcut to organize import in a particular file in eclipse..
Also you can right the entire package and select the Organize Import option available there to rmove unsed imports in entire package.

Related

How to remove all unused imports from a Java file in VS code

I use VS Code with the Extension Pack for Java.
When I have unused imports like
import java.util.ArrayList;
import java.util.stream.Collectors;
in my .java file I can remove each import individually via the "Quick Fix" action (Ctrl+.).
But how can I remove all unused imports from a Java file in VS Code?
UPDATA
Now that vs code Java has added a code action, you can use Remove all unused imports directly in quickfix to remove all unused imports.
You can use the shortcut key Shift+Alt+O
. This will format your import code and of course remove unused imports.
For example, the original import code is like this
After using the Shift+Alt+O to format the import code
Fix them on just SAVING the file, the easiest way
Follow these steps to setup settings in VS Code.
Press [Ctrl + , ] to open setting
Click on that top-right icon (Open settings JSON) as shown in the below image marked in red.
Add this below code in settings.json file.
"editor.codeActionsOnSave": {
"source.organizeImports": true
}
Now just save it and its done

Intellij class Import being added to object not the top of class

I've just started a new IntelliJ project. When I try and import a java class with the IntelliJ import shortcut, the import is being added to the object and not to the top of the class like it normally would.
Where in the settings can I change this?
See Preferences/Settings > Editor > Code Style > Java if this option ...
Use fully qualified class names
... is ticked then IntelliJ will always use fully qualified class names. If you disable this option then Intellij will include the import statement and refer to the class by its 'simple' name.
Here's a screenshot showing this configuration item:
If the accepted answer is not working for anyone, Try the below points too.
Check whether the class file is in correct package and resolve if there is any issues in the class file.
Click File -> Invalidate Caches/ Restart and Click Invalidate and Restart button, Once it restarted U'll be able to import properly.
I was facing this issue with my custom / generated(protobuf) java packages.
Solution (TDLR)
Whenever you face this issue, just check the package or class that getting imported doesn't have any error(red marking).
If so try to fix that.
I was having an issue with package naming and its actual location. By generated-sources -> Right Click -> Mark Directory As -> Sources Root corrected my issue.
Check whether generated-sources or folder under the generated sources need to mark us Sources Root.
My issue was with generated java files (Adding this just to help someone else with same issue)
Intellij shows red mark for Import with wildchar(import pkg1.subpkg1.*;). Like editor was unable to recognizing 'import all' under a package.
But direct import to the inner package was fine.
On top of that an resolving issue with import class add full package to the Class itself, instead of adding import statement on top the java file.
This issue didn't affect the code compilation, only in editor it was showing red mark.

Java - Hidden import folders in Intellij

I'm trying to work off of an existing code base, and one of the files has an import statement accessing the path game.format.noFlash.*. However the format folder only has files for the other classes; the noFlash file folder is nowhere to be found.
I know it must be there because when I begin to type in the import statement in intelliJ, one of the autofill options is the noFlash folder. Is there a way to access it, and if so how?
The auto-fill options are display packages and also class names
I suspect the noFlash is a class and not a folder and Intellij is suggestion that you can import it, for example:
Classes can be specified explicitly on import instead of using the wildcard character.
import javax.swing.JOptionPane; // Make a single class visible.

Default imports in Eclipse

Is there a way to customize the default imports in Eclipse?
For example if I open a new JUnit Test Class by default I get these imports:
import static org.junit.Assert.*;
import org.junit.Test;
What I'd like to get:
import static org.junit.Assert.*;
import org.junit.Test;
import static org.hamcrest.Matchers.*;
Unfortunately, Eclipse is quite lacking in the customizability of code generation when refactoring and creating new entities.
You might want to check out Eclipse Optimize Imports to Include Static Imports for information how to make content assist find static methods in predefined classes. That might be what you actually want. In the accepted answer Joey Gibson writes that you can add org.hamcrest.Matchers to Window » Preferences » Java » Editor » Content Assist » Favorites.
Another solution to the specific problem of statically importing Hamcrest methods, is to configure a Code Template named hamcrest instead. That way you can simply type ham and follow up with ctrl + space to get the import at the top.
The template should look like
${staticImport:importStatic('org.hamcrest.Matchers.*')}${cursor}
An even more convenient hack is to add this template to the already existing test code template which generates a new test method. If you change this template to:
#${testType:newType(org.junit.Test)}
public void ${testName}() throws Exception {
${staticImport1:importStatic('org.hamcrest.Matchers.*')}
${staticImport2:importStatic('org.junit.Assert.*')}${cursor}
}
and use this each time you make a new test method you will never have to care about adding the hamcrest import manually again.
Image to show where you configure it:
The closest preference I can find is the one under Window --> Preferences --> Java --> Code Templates. Expand the Code section and select the New Java files option to view the pattern for newly created Java files. You can then click Edit to add the import, for instance:
${filecomment}
${package_declaration}
import org.hamcrest.*;
${typecomment}
${type_declaration}
In all cases you still need to write the code that uses the org.hamcrest package. Alternatively, just organize the imports by pressing Ctrl+Shift+O after adding the code that uses the package.
I recommend you to add org.hamcrest.Matchers.* into “Favorites” (Window -> Preferences -> Java -> Editor -> Content Assist -> Favorites).
This way content assist will propose static members even if the import is missing and add corresponding import when you use the member.
It means you can type the method/field you’d like to use and let content assist add the import automatically.
Organize Imports
Modern IDEs offer a feature called Organize Imports. Using this feature you can no longer be worried about those import statements, the IDE itself manage those imports.
How should you do
As you write your codes, whenever you want to make the IDE to organize your imports, you should just press its shortcut keys.
Keyboard: Ctrl+Shift+O
Menu: Source → Organize Imports
How it works
IDE search through your codes and lookup each class and add their corresponding import statements. Also unused imported classes will be removed.
In Netbeans you can navigate through this , Tools-->templates-->java Folder--> you can give what you need to want while opening a page Example :there will be Java class,interface,enum,exception etc

"redirect" packages

Is there a way to "redirect" packages?
Is there any possibility to tell Android to get the class-files from e.g. com.java.rmi instead of java.rmi?
Some additional libraries depend on this package so I repacked it with jarjar - otherwise dalvik-exc would occur.
Just change your import statements. If you are using Eclipse, you can just remove the java.rmi import lines and press Control-Shift-O (Organize Imports). It will ask you which package to import.
Edit: yes, you have to do this in all files that use said package.

Categories