in test classes I have the following import
import static org.junit.Assert.*;
when I do organize import via ctrl + shift + o then it automatically changes to following
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
How can I configure eclipse not to do it ?
PS: I only want junit imports not be handled in that way
=============
I added a save action to remove unused imports. [properties -> java editor -> save actions]
so everytime I save unused imports are removed (since I used ctrl + shift + o mainly to remove unused imports this looks like a way forward..)
Change the number of static imports to 1
Under Window, Preferences, Java, Code Style, Organize Imports there's an option called "Number of static imports needed for .*" - set that to 1. (Another way to find it quickly is just to type "static" into the search box in preferences.)
Note that this will mean that hitting Ctrl-Shift-O will always turn any static imports into an static import-on-demand form, which may not be what you want. If you have separate projects for test and non-test code, you could configure it on a per project basis.
Personally I'd just live with the explicit imports - I usually end up with static imports by starting off with the class-qualified call, and then hitting Ctrl-Shift-M on the method name to import it statically.
Related
Trying to learn Java from Bruce Eckel's book, I don't understand why the compiler doesn't find the library I want to import. I have first done this on Windows/Cygwin and now on Centos 7, using OpenJDK 1.8.0. Same result on both platforms.
This line:
import static t.b.u.*;
causes compiler error
$ javac TestPrint.java
TestPrint.java:2: error: package t.b does not exist
import static t.b.u.*;
^
I agree that package t.b doesn't exist, but I actually wanted to import package t.b.u. Why does the compiler ignore the u?
CLASSPATH is set as follows:
$ export|grep CLASS
declare -x CLASSPATH="/home/bbausch/thinking-in-java"
The package is a single file:
$ cat /home/bbausch/thinking-in-java/t/b/u/Print.java
package t.b.u;
import java.io.*;
public class Print {
... etc ...
The error is probably so obvious that I don't see it. Can somebody help?
This is specifically related to the Java Language Specification: https://docs.oracle.com/javase/specs/jls/se7/html/jls-7.html#jls-7.5.3
These two lines inherently ask for different things:
import static t.b.u.*;
This statement asks to import all static methods from a class named u from a package t.b.
import t.b.u.*;
This statement asks to import all classes underneath t.b.u.
Static imports target a TypeName only. Normal imports target a package, or a specific class.
The roughly equivalent general import to the static import would be this:
import t.b.u;
This asks to import just the class u from a package t.b.
In your specific example, you'd probably want this statement to import all static methods of the Print class.
import static t.b.u.Print.*;
I know that import example.* will import all the classes and interfaces in the package example. So is it necessary to use the * only when there are 2 or more classes in the example package?
Because in my program there was only 1 class, xyz, in the package example, and when I tried to use it in other program by import example.*;, the xyz class was not accessible.
Instead if I used it like - import example.xyz, then only it was accessible in other programs. But the usual import java.util.* and other commands work just fine. So is it because they have multiple classes and interfaces in those packages and only then should the * be used?
edit:package code
package myPackage;
public class abcd
{
public void show()
{
System.out.println("this is from package");
}
}
program
import myPackage.*;
class ghg
{
public static void main(String args[])
{
abcd x=new abcd();
x.show();
}
}
error
ghg.java:7: cannot access abcd
bad class file: .\abcd.class
class file contains wrong class: myPackage.abcd
Please remove or make sure it appears in the correct subdirectory of the classpath
EDIT: So i was keeping ghg.java in bin folder of jdk..I moved them out both package and ghg.java and put them in different directory and it worked.
Those import statements are just an indication for the compiler to know where to lookup classes you are using within your source code.
In that sense, this is only about style. There are no good technical reasons to use one or the other style.
But many people consider using "import x.y.*" to be bad practice; they recommend to have "import x.y.Class1", "import x.y.Class2", ...
Reasoning behind that: you, as the human reader are very much interested in understanding the dependencies into other packages. When the wild-card import is used; you have no mean to understand how many classes are really used. As in: if your class needs 50, 100 imports ... that alone would tell you that something is wrong (because your class has way too many dependencies when it is importing 50, 100 other classes). If wildcards are used, you wouldn't know.
As a consequence, tools such as eclipse will by default "roll out" wildcard imports and turn them into specific class imports instead.
Lets be precise: using "import *" is never required. It just seems convenient at times! And when "import x.y.*" does not work; but "import x.y.z" works; then well, you must have done something wrong!
I want to enable static import wildcards for classes usually used in tests, e.g. org.hamcrest.Matchers.
So, when I'll execute the Optimize Imports action instead of this :
import static org.hamcrest.Matchers.any;
import static org.hamcrest.Matchers.is;
I would get this :
import static org.hamcrest.Matchers.*;
Is it possible to do that in IDEA ?
Solution
Settings :: Editor :: Code Style :: Java => Imports.
I have a "Sprites" folder with some class files and a "Launcher" folder with some class files. I tried the following code for import:
package Sprites;
and it lead to the following
hw9\Launcher>javac *.java
TowerDefense.java:2: error: class, interface, or enum expected
package Sprites;
^
1 error
Am I doing this incorrectly? My Sprites and Launcher are in the hw9 directory, so I assumed it would work. A picture for clarification:
You can use a wildcard import to import all classes within the immediate directory:
import Sprites.*;
This opposed to something like:
import Sprites.Class1;
import Sprites.Class2;
import Sprites.Class3;
...
Generally, wildcard imports can produce conflicts and errors (for example java.awt.List and java.util.List), so usually better to avoid them.
Packages should also be lower-cased.
The error is due to syntax, usually when you see something like ...expected that is syntax error indicator.
In the class in your launcher package, include the import statements for the classes which are being referred to.
It should look something like the following:
package the.name.of.your.package;
import Spirites.NameOfclass; //quialify the import parth as is
class YourLauncherClass{
//class definition
}
Also make sure that semicolons aren't missing at the end of import and package.
Hope that helps.
Best practice is to import the specific class you require rather than importing the complete package.
import Spirites.NameOfclassRequired;
class YourClass{
//class definition
}
If you are using eclipse you can do that using CTRL+SHIFT+O When you do that eclipse imports the specific class you require. For an instance if you using an ArrayList rather than importing java.util.*; it will import java.util.ArrayList;
If you need multiple classes from a package then for sure you can import the entire package
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;
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.*;
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
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.
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'
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