Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a flag to prevent system load library #456

Closed
wants to merge 2 commits into from

Conversation

abbasvalliani
Copy link

Currently, even if java.library.path is set to nothing, the System.loadLibrary is called which searches through all paths to load the libraries. This creates a significant delay in startup. The pull will allow a switch to control if System.loadLibrary is called and the fix is backwards compatible to prevent breaking existing codecase.

@saudet
Copy link
Member

saudet commented Feb 5, 2021

Why add a new switch for this? It sounds to me like it's a bug that "java.library.path" doesn't get respected, and we kind need to patch around Java SE for not doing this anyway. BTW, the only reason it's calling System.loadLibrary() is because mainly Android doesn't like System.load() to be called...

@saudet
Copy link
Member

saudet commented Feb 5, 2021

Actually, I'm not sure that's a bug at all. Java specs make it very clear that we shouldn't change, for example, environment variables, and this is probably why it doesn't respect changes in "java.library.path" after startup. It should be set correctly at startup time.

@abbasvalliani
Copy link
Author

Understood. Will update it.

@saudet
Copy link
Member

saudet commented Feb 5, 2021

Seems to be working as intended the way it is too:

public class TestLoad {
    public static void main(String[] args) {
        System.loadLibrary("openblas");
    }
}
$ java TestLoad
$ java -Djava.library.path= TestLoad
Exception in thread "main" java.lang.UnsatisfiedLinkError: no openblas in java.library.path
	at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1860)
	at java.lang.Runtime.loadLibrary0(Runtime.java:870)
	at java.lang.System.loadLibrary(System.java:1124)
	at TestLoad.main(TestLoad.java:3)

@abbasvalliani
Copy link
Author

Run it in windows with debug on.

Loader.load(opencv_java.class);
You'll see searches through the entire paths to find the libraries. Look, I think there is a major issue here. You will always have items in PATH in windows. For each missing library, you're searching the PATH folders which is incredibly slow and takes around 10 seconds plus to load OpenCV. This really slows the entire development process down. There must be a way to turnoff loadLibrary otherwise this is a rather tedious process.

@saudet
Copy link
Member

saudet commented Feb 7, 2021

I agree, we need a way to disable it, and setting java.library.path to an empty string works just fine for that purpose. If you have a version of Java or of Windows where that doesn't work for some reason though, please provide more details about everything that we need to reproduce the issue, and I'll test that see what we can do about it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants