bytedeco / javacpp

The missing bridge between Java and native C++
Other
4.49k stars 583 forks source link

Show some information if user didn't add javacpp-platform #585

Open bepopov opened 2 years ago

bepopov commented 2 years ago

Hello.

I would like to suggest some enhancment that I think can make javacpp a little bit more user-friendly. I mean when user tries to use javacpp in some Gradle project he can forget to add javacpp-platform dependency and therefore some stuff won't work. For now if I don't add javacpp-platform I don't see any error or warning message because javacpp logs it in debug level and I don't know how to turn it on. Anyway there might be a situation when user forgot to turn on debug level for logger and also forgot to add javacpp-platform so some java.lang.UnsatisfiedLinkError: no jnijavacpp in java.library.path will happen and never be shown to user. It can be hard to debug what is wrong. I would suggest to add some informative message saying that javacpp-platform dependency is missed or something like that

public class Loader {
  //...
  static {
      try {
          Loader.load();
      } catch (Throwable t) {
          if (logger.isDebugEnabled()) {
              logger.debug("Could not load Loader: " + t);
          }
      }
  }

  public static String load(Class cls, Properties properties, boolean pathsFirst, String executable) {
    try {
        //...
    } catch (UnsatisfiedLinkError e) {
        Throwable t = e;
        while (t != null) {
            if (t instanceof UnsatisfiedLinkError &&
                    t.getMessage().contains("already loaded in another classloader")) {
                librarySuffix++;
                continue tryAgain;
            }
            t = t.getCause() != t ? t.getCause() : null;
        }
        if (preloadError != null && e.getCause() == null) {
            e.initCause(preloadError);
        }
        if (!checkPlatform(cls, properties, false)) {
            // this is an optional library
            return null;
        } else {
            throw e;
        }
    }
  }
  //...
}
saudet commented 2 years ago

The javacpp-platform artifact is not actually required. If you're not having any problems without it, you can leave it out.

saudet commented 2 years ago

When it's potentially required, we're going to get messages like this one: https://github.com/bytedeco/javacpp/blob/master/src/main/java/org/bytedeco/javacpp/BytePointer.java#L131 However, we can still fix that without adding a dependency on javacpp-platform, which is essentially an optional "presets", but included in this repository only for convenience.