0

I've realized that my code has a problem with setting a program icon (instead of the java one) in different platforms. I had written this code for windows and mac:

private void putIcon() {
    URL url = ClassLoader.getSystemResource("resources/icon.png");

    String name = System.getProperty("os.name");
    if (name.startsWith("Win")) {
        Toolkit kit = Toolkit.getDefaultToolkit();
        Image img = kit.createImage(url);
        this.setIconImage(img);
    } else {
        Application.getApplication().setDockIconImage(new ImageIcon(url).getImage());
    }
}

While I was testing it in mac there was not problem, but when I tried it windows I realized that won't work because that class isn't in windows:

import com.apple.eawt.Application;

What could I do to solve this issue? For what I've researched it's not possible to have some kind of "if" in the import section of the code, and if that class is there in windows it won't compile.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
nck
  • 1,673
  • 16
  • 40

1 Answers1

1

Check if the specified class Application can be found by using

Class.forName("com.apple.eawt.Application");

If this method does not throw a ClassNotFoundException, invoke the methods you want by using reflection only. Seems a bit hacky but it should work.

Also make sure that you are not importing the class.

Yannick Rot
  • 196
  • 2
  • 12
  • Yes, reflection works; examples may be found [here](http://stackoverflow.com/a/30308671/230513) and in `OSXAdapter` cited [here](http://stackoverflow.com/a/2061318/230513). – trashgod Dec 17 '16 at 00:32