static import only from classes and interfaces

46.7k Views Asked by At

My code compiles fine in Eclipse, but when I try to compile from the commandline (via our ruby-based buildr system), I get this error message:

static import only from classes and interfaces

Suggesting that static import of public static fields is not permitted. What should I look for to help diagnose this problem? How can I fix it?

Update: per @Ted's request, the constant declaration in the referenced file:

public static final String NULL = "<NULL>";

and the (bowdlerized) reference in the referring file:

import static my.path.MyClass.NULL;
7

There are 7 best solutions below

0
On

In my case, I have two packages in VSCode and I was importing a method from a different package in another package class.

You can see it in the pics attached.

Now this import statement

import static Packages.B.Message.message;

is working fine in IntelliJ idea as it is an IDE, but VSCode uses CLI so we need to do this.

Go to your root directory (in my case it was Object-oriented_Programming) and then run these commands:

javac -cp . Packages/A/Greeting.java
java -cp . Packages.A.Greeting

This is just an example: you need to write the path of your directory and file.

The cause of error was that because of this import statement we need to include the path of this import on compile time, which was missing.

0
On

I also had this error and my issue turned out to be a wayward static import of a junit 4 package in my test source file.

I had the following:

import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTimeout;

I removed the import static org.junit.Assert.fail; (no idea how I managed to get that in there in the first place) and all is now working.

0
On

My guess is that Eclipse and buildr are using either different Java compiler versions or different compiler flags. There's a bug in the Java 7 compiler (bug ID: 715906) that generates this error when you statically import specific fields. The work-around is to use a wildcard static import. So instead of:

import static pkg.Class.staticField;

do this:

import static pkg.Class.*;
2
On

I accidentally set test directory as source. And Test sources were considered as source files.

sourceSets.main.java.srcDirs 'src'

| -- src
  | -- main
  | -- test

Fix:

sourceSets.main.java.srcDirs 'src/main'
0
On

Some how same solution mentioned by @m-watson

I have replaced

import static org.junit.Assert.assertThrows;

With

import static org.junit.jupiter.api.Assertions.assertThrows;

and it worked

0
On

Late answer but I just got a similar issue and figured it out. I'll post in case it helps anyone else who finds this page...

I got a similar error when, after a big merge and refactor, I accidentally put a test class into src/main/java instead of src/test/java. Since the JUnit dependency was scope=tests, it didn't work in pure maven. Maybe you are having the same issue

0
On

This is not the most likely cause, but did the trick for me. I've seen this error message that was caused from cached data that wasn't removed properly during code changes (like moving constants from one file to another).

In this case, simply clean the project and rebuild it from scratch. This will take away stale cached data that's causing this compile error.