I am a bit confused by ImageIO.read(file)
. When I try to read a .png file into a BufferedImage, at least on macOS, the focus moves to a new application named after my main class. It appears in the Menu bar. It does so even when I run java from the command line.
The annoying thing is that it moves the focus out of my IDE and I have to return to it manually.
I looked at the source of ImageIO.read(file)
. I discovered that it is calling ImageIO.createImageInputStream(file)
and that is what triggers this behaviour.
My question is: what is ImageIO doing actually, why is my main class showing in the Menu bar when it is just loading information in memory. And most important, how can I avoid it?
Below the code to show the problem. Use any .png to test it.
package misc;
import javax.imageio.ImageIO;
import javax.imageio.stream.ImageInputStream;
import java.io.File;
import java.io.IOException;
public class ReadImageTest {
public static void main(String[] args) {
try {
File file = new File("out/production/resources/picture.png");
long time = System.currentTimeMillis();
ImageInputStream stream = ImageIO.createImageInputStream(file);
long delay = System.currentTimeMillis() - time;
System.out.println("stream: " + stream.length());
System.out.println("time: " + delay/1000.0);
} catch (IOException e) {
e.printStackTrace();
}
}
}