[Fixed] Arrays cannot be resolved? Is this a Build path issue? – Arrays

Photo of author
Written By M Ibrahim
arrays eclipse java-8

Quick Fix: (Ctrl)+(Shift)+(o) to import dependencies. Or manually add ‘import java.util.Arrays;’

The Problem:

In Eclipse programming environment, when attempting to utilize the Arrays.sort(arr) method, an error message stating, Arrays cannot be resolved is encountered. Furthermore, upon selecting the underlined Arrays word, the anticipated import java.util.Arrays suggestion does not appear. This issue arises despite setting up the Project build path and installing both JRE and JDK 1.8.0_20. What could be causing this issue and preventing the recognition of the Arrays class?

The Solutions:

Solution 1: Manually Add Import Statement

Step 1: Locate the Java File (.java) you’re working on in Eclipse.

Step 2: Check for the package declaration. If it exists, place the cursor right after it. If not, position the cursor at the beginning of the file.

Step 3: Manually add the following line:

import java.util.Arrays;

Ensure that the line is added outside of any class or method declarations.

Step 4: Save the file.

This manual addition of the import statement should resolve the Arrays cannot be resolved error.

Alternatively, you can use the following keyboard shortcut to automatically import required dependencies:

Step 1: Place the cursor on the red underlined Arrays word.

Step 2: Press <kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>o</kbd> (Windows and Linux) or <kbd>⌘</kbd>+<kbd>Option</kbd>+<kbd>o</kbd> (Mac).

This shortcut should automatically add the necessary import statement, resolving the error.

Solution 2: Changing JDK Compiler Compliance Level

Sometimes, you may encounter a similar issue where Eclipse fails to resolve the `Arrays` class and throws the `Arrays cannot be resolved` error. This can be due to a mismatch between the Java compiler compliance level and the version of Java used for compiling your project.

To resolve this issue, follow these steps:

  1. Open Eclipse and navigate to the "Window" menu.
  2. Select "Preferences" from the dropdown menu.
  3. In the Preferences window, expand the "Java" section and select "Compiler."
  4. Under "Compiler Compliance Level," select a version that is compatible with the version of Java that you are using to compile your project. For example, if you are using JDK 1.8, select "1.8" or "Java 8" as the compiler compliance level.
  5. Click "Apply" and then "OK" to save the changes.

After making these changes, try typing `Arrays.sort(arr)` again. The error should now be gone, and Eclipse should be able to resolve the `Arrays` class correctly.

Solution 3: Use Arrays.sort() method inside a method

The Arrays.sort() method should be called inside a method of your program, not at the class level. Make sure to call this method inside a method, such as the main() method. Here’s an example:

public class Main {

    public static void main(String[] args) {
        int[] arr = {1, 3, 4, 2, 5};

        Arrays.sort(arr); // Call Arrays.sort() method inside the main method
    }
}

After making this change, the error message "Arrays cannot be resolved" should disappear.