I've been having problems with imports for a while and finally did some investigation. Sometimes adding imports fails and you do not get any quick fix suggestions to add it either. For me this happens most often for java.util.Optional and I had to resort to adding the import manually.
Turns out packages with $ in the name (for example foo.$bar$) causes index out of bounds in PackageImportComparator.java. Seemingly something is turning "$" into "." somewhere creating an invalid package name which triggers the bug in PackageImportComparator.java. In any case PackageImportComparator probably should cater for such anomalies so it wouldn't crash badly if that happens to be the case.
You can re-produce the issue simply by creating a java project with two files:
package java.util.foo.$bar$;
public class Optional {
}
package test;
import java.util.List;
public class Test {
public List<?> foo;
public Optional<String> bar;
}
If you hover over Optional in the above file to import java.util.Optional it fails just because the java.util.foo.$bar$.Optional class exists on the classpath. Note that you need another existing import in the same import "section", in this case "java".
I've realized I can workaround the issue by adding "java.util.foo" to Preferences->Java->Code Style->Organize imports so it would put that import into it's own section.
Related if you try to import java.util.foo.$bar$.Optional, eclipse imports it as import java.util.foo..bar..Optional; where you see the bug turning "$" into "." causing other errors.
In the error log the exception looks like:
java.lang.StringIndexOutOfBoundsException: Index 19 out of bounds for length 19
at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:55)
at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:52)
at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:213)
at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:210)
at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:98)
at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:106)
at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:302)
at java.base/java.lang.String.checkIndex(String.java:4822)
at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:46)
at java.base/java.lang.String.charAt(String.java:1544)
at org.eclipse.jdt.internal.core.dom.rewrite.imports.PackageImportComparator.determinePackageName(PackageImportComparator.java:61)
at org.eclipse.jdt.internal.core.dom.rewrite.imports.PackageImportComparator.compare(PackageImportComparator.java:40)
at org.eclipse.jdt.internal.core.dom.rewrite.imports.PackageImportComparator.compare(PackageImportComparator.java:1)
at org.eclipse.jdt.internal.core.dom.rewrite.imports.ImportComparator.compare(ImportComparator.java:63)
at org.eclipse.jdt.internal.core.dom.rewrite.imports.ImportComparator.compare(ImportComparator.java:1)
at java.base/java.util.TreeMap.compare(TreeMap.java:1605)
at java.base/java.util.TreeMap.getLowerEntry(TreeMap.java:527)
at java.base/java.util.TreeMap.lowerKey(TreeMap.java:1023)
at java.base/java.util.TreeSet.lower(TreeSet.java:415)
at org.eclipse.jdt.internal.core.dom.rewrite.imports.OrderPreservingImportAdder.determineAdjacentNewImports(OrderPreservingImportAdder.java:135)
at org.eclipse.jdt.internal.core.dom.rewrite.imports.OrderPreservingImportAdder.addImports(OrderPreservingImportAdder.java:87)
at org.eclipse.jdt.internal.core.dom.rewrite.imports.ImportRewriteAnalyzer.computeImportOrder(ImportRewriteAnalyzer.java:601)
at org.eclipse.jdt.internal.core.dom.rewrite.imports.ImportRewriteAnalyzer.analyzeRewrite(ImportRewriteAnalyzer.java:553)
at org.eclipse.jdt.core.dom.rewrite.ImportRewrite.rewriteImports(ImportRewrite.java:1520)
at org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposalCore.addEdits(ASTRewriteCorrectionProposalCore.java:91)
at org.eclipse.jdt.core.manipulation.CUCorrectionProposalCore.createTextChange(CUCorrectionProposalCore.java:188)
at org.eclipse.jdt.core.manipulation.CUCorrectionProposalCore.createChange(CUCorrectionProposalCore.java:195)
at org.eclipse.jdt.core.manipulation.ChangeCorrectionProposalCore.getChange(ChangeCorrectionProposalCore.java:153)
at org.eclipse.jdt.internal.ui.text.correction.UnresolvedElementsBaseSubProcessor.getCompositeChangeProposal(UnresolvedElementsBaseSubProcessor.java:1079)
at org.eclipse.jdt.internal.ui.text.correction.UnresolvedElementsBaseSubProcessor.getCompositeChangeProposal(UnresolvedElementsBaseSubProcessor.java:1072)
at org.eclipse.jdt.internal.ui.text.correction.UnresolvedElementsBaseSubProcessor.addSimilarTypeProposals(UnresolvedElementsBaseSubProcessor.java:1037)
at org.eclipse.jdt.internal.ui.text.correction.UnresolvedElementsBaseSubProcessor.collectTypeProposals(UnresolvedElementsBaseSubProcessor.java:791)
at org.eclipse.jdt.internal.ui.text.correction.UnresolvedElementsSubProcessor.getTypeProposals(UnresolvedElementsSubProcessor.java:143)
at org.eclipse.jdt.internal.ui.text.correction.QuickFixProcessor.process(QuickFixProcessor.java:450)
at org.eclipse.jdt.internal.ui.text.correction.QuickFixProcessor.getCorrections(QuickFixProcessor.java:370)
at org.eclipse.jdt.internal.ui.text.correction.JavaCorrectionProcessor$SafeCorrectionCollector.safeRun(JavaCorrectionProcessor.java:381)
at org.eclipse.jdt.internal.ui.text.correction.JavaCorrectionProcessor$SafeCorrectionProcessorAccess.run(JavaCorrectionProcessor.java:341)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:47)
at org.eclipse.jdt.internal.ui.text.correction.JavaCorrectionProcessor$SafeCorrectionProcessorAccess.process(JavaCorrectionProcessor.java:336)
at org.eclipse.jdt.internal.ui.text.correction.JavaCorrectionProcessor.collectCorrections(JavaCorrectionProcessor.java:465)
at org.eclipse.jdt.internal.ui.text.correction.JavaCorrectionProcessor.collectProposals(JavaCorrectionProcessor.java:284)
at org.eclipse.jdt.internal.ui.text.correction.JavaCorrectionProcessor.computeQuickAssistProposals(JavaCorrectionProcessor.java:247)
at org.eclipse.jface.text.quickassist.QuickAssistAssistant$ContentAssistProcessor.computeCompletionProposals(QuickAssistAssistant.java:72)
at org.eclipse.jface.text.contentassist.ContentAssistant$2.lambda$0(ContentAssistant.java:2087)
at java.base/java.util.Collections$SingletonSet.forEach(Collections.java:5126)
at org.eclipse.jface.text.contentassist.ContentAssistant$2.run(ContentAssistant.java:2086)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:47)
at org.eclipse.jface.text.contentassist.ContentAssistant.computeCompletionProposals(ContentAssistant.java:2083)
at org.eclipse.jface.text.contentassist.CompletionProposalPopup.computeProposals(CompletionProposalPopup.java:591)
at org.eclipse.jface.text.contentassist.CompletionProposalPopup.lambda$0(CompletionProposalPopup.java:519)
at org.eclipse.swt.custom.BusyIndicator.showWhile(BusyIndicator.java:67)
at org.eclipse.jface.text.contentassist.CompletionProposalPopup.showProposals(CompletionProposalPopup.java:514)
at org.eclipse.jface.text.contentassist.ContentAssistant.showPossibleCompletions(ContentAssistant.java:1896)
at org.eclipse.jface.text.quickassist.QuickAssistAssistant.showPossibleQuickAssists(QuickAssistAssistant.java:114)
at org.eclipse.jdt.internal.ui.text.correction.JavaCorrectionAssistant.showPossibleQuickAssists(JavaCorrectionAssistant.java:194)
at org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor$AdaptedSourceViewer.doOperation(CompilationUnitEditor.java:201)
at org.eclipse.jdt.internal.ui.javaeditor.JavaSelectAnnotationRulerAction.runWithEvent(JavaSelectAnnotationRulerAction.java:101)
at org.eclipse.ui.texteditor.AbstractRulerActionDelegate.runWithEvent(AbstractRulerActionDelegate.java:117)
at org.eclipse.ui.internal.handlers.ActionDelegateHandlerProxy.execute(ActionDelegateHandlerProxy.java:273)
at org.eclipse.core.commands.Command.executeWithChecks(Command.java:488)
at org.eclipse.core.commands.ParameterizedCommand.executeWithChecks(ParameterizedCommand.java:484)
at org.eclipse.ui.actions.ContributedAction.runWithEvent(ContributedAction.java:176)
at org.eclipse.ui.texteditor.AbstractTextEditor$1.triggerAction(AbstractTextEditor.java:3231)
at org.eclipse.ui.texteditor.AbstractTextEditor$1.lambda$0(AbstractTextEditor.java:3247)
at org.eclipse.swt.widgets.Display.runTimers(Display.java:4626)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:4005)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$5.run(PartRenderingEngine.java:1160)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:339)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:1051)
at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:153)
at org.eclipse.ui.internal.Workbench.lambda$3(Workbench.java:684)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:339)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:583)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:173)
at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:185)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:219)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:149)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:115)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:467)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:298)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
at java.base/java.lang.reflect.Method.invoke(Method.java:580)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:615)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:563)
at org.eclipse.equinox.launcher.Main.run(Main.java:1415)
Eclipse IDE for Enterprise Java and Web Developers (includes Incubating components)
Version: 2026-03 (4.39.0)
Build id: 20260305-0817
I've been having problems with imports for a while and finally did some investigation. Sometimes adding imports fails and you do not get any quick fix suggestions to add it either. For me this happens most often for java.util.Optional and I had to resort to adding the import manually.
Turns out packages with $ in the name (for example
foo.$bar$) causes index out of bounds inPackageImportComparator.java. Seemingly something is turning "$" into "." somewhere creating an invalid package name which triggers the bug inPackageImportComparator.java. In any case PackageImportComparator probably should cater for such anomalies so it wouldn't crash badly if that happens to be the case.You can re-produce the issue simply by creating a java project with two files:
If you hover over Optional in the above file to import
java.util.Optionalit fails just because thejava.util.foo.$bar$.Optionalclass exists on the classpath. Note that you need another existing import in the same import "section", in this case "java".I've realized I can workaround the issue by adding "java.util.foo" to Preferences->Java->Code Style->Organize imports so it would put that import into it's own section.
Related if you try to import
java.util.foo.$bar$.Optional, eclipse imports it asimport java.util.foo..bar..Optional;where you see the bug turning "$" into "." causing other errors.In the error log the exception looks like:
Eclipse IDE for Enterprise Java and Web Developers (includes Incubating components)
Version: 2026-03 (4.39.0)
Build id: 20260305-0817