Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you catch a "package x does not exist" error in Java?

I'm using this code to set the macOS dock icon of my JavaFX app:

try {
    // Sets macOS dock icon:
    com.apple.eawt.Application.getApplication().setDockIconImage(SwingFXUtils.fromFXImage(appIcon, null));
} catch (Exception e) {
    // Doesn’t work on Windows and Linux
}

I can't run the app on Windows because it throws the error "java: package com.apple.eawt does not exist"

How can I catch a "package x does not exist" error or check if it exists at runtime?

like image 885
clickbait Avatar asked Feb 26 '26 07:02

clickbait


1 Answers

You could try to find out what your current OS is beforehand. There are basically two ways. Either System.getProperty("os.name") or SystemUtils.OS_NAME from Apache Commons Lang.

See also: https://www.baeldung.com/java-detect-os

like image 105
mipa Avatar answered Feb 27 '26 19:02

mipa