Reputation: 40336
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;
Upvotes: 36
Views: 52071
Reputation: 4897
In my case playing with strings broke the build somehow, doing build clean up fix it somehow in Android Studio
Upvotes: 0
Reputation: 65
This error can be also thrown if you have mentioned wrong package like - import static org.junit.jupiter.Assertions.assertThrows; instead of this correct one import static org.junit.jupiter.api.Assertions.assertThrows;
Upvotes: 1
Reputation: 11
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.
Upvotes: 0
Reputation: 11688
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.
Upvotes: 1
Reputation: 45475
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
Upvotes: 0
Reputation: 234795
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.*;
Upvotes: 28
Reputation: 103
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.
Upvotes: 7
Reputation: 15382
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'
Upvotes: 1
Reputation: 4633
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
Upvotes: 13