Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Great, and thanks for the confirmation.

Do you have any idea how easy it is to provide a Java fallback on platforms where a native implementation isn't available? (I find it's often possible to call out to a command line tool as a workaround for things I'm trying to do in native code.)



Well, it is going to be the same as in any other language, you are the one that needs to take care of that fallback exists.

To read more about Panama, https://jdk.java.net/panama/

Check Foreign-Memory Access API and Foreign Linker API related entries.


With Panama, you get a really low level API and first have to load in the shared library - where you can easily handle this case.

Though I’m not sure I would want this behavior, or at most for really optional deps; ideally software should be “installed” in an ideal environment.


> ideally software should be “installed” in an ideal environment.

Native implementations with non-native fallbacks can be an important decoupling mechanism.

I used to be a developer for a Linux desktop application with a peak of something like 60 million unique users over a trailing 3 month window. We had Windows, OS X, RPM, and dpkg installers with native libraries, but people also ran our software on FreeBSD, Gentoo, etc.

It's handy to have gradual enhancement/graceful degradation in non-ideal installs where the native API's you'd like either don't exist or are buggy and shouldn't be used.

In my day job, we sometimes have a business need to iterate new features at a faster tempo than our native code release schedule, so it's nice to have a similar sort of fallback implementation.


I'm not very familiar with Java's native interface, but you could potentially implement the native and non-native approaches as separate classes with a common interface, and choose to inject the correct dependency to the calling code at runtime by detecting OS features?


It clearly can be done, perhaps by using reflection and catching a class loading exception, but it's pretty common boilerplate that should optionally be handled by the language runtime's FFI.

Let's say you want to have a native-looking prompt window via wxWidgets, and fall back to Swing if wx isn't installed. Or, you want to use some particular optimized native BLAS library if it's installed, or fall back to doing the linear algebra in Java if it's not available.

In these cases, and many others, "detecting OS features" involves checking LD_LIBRARY_PATH (or PATH on Win32) for libraries, checking they're for the correct architecture, etc. The OS already provides dlopen() or its equivalent for this purpose. Properly detecting the OS and emulating the OS's algorithm for finding native libraries is just wasteful when the OS already exposes the functionality.

As an added bonus, an FFI that replaces a method body allows someone who doesn't have the C/C++/Rust/FORTRAN handy, or can't read it, to see roughly what the native code should be doing.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: