Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 573092

Summary: Incorrect DLL Name Constant for SWT Browser style SWT.EDGE
Product: [Eclipse Project] Platform Reporter: Christoph Nievergelt <Christoph.Nievergelt>
Component: SWTAssignee: Nikita Nemkin <nikita>
Status: RESOLVED FIXED QA Contact: Niraj Modi <niraj.modi>
Severity: critical    
Priority: P3 CC: Christoph.Nievergelt, nikita, niraj.modi, sravankumarl
Version: 4.19Flags: sravankumarl: review+
Target Milestone: 4.20 RC2   
Hardware: PC   
OS: Windows 10   
See Also: https://git.eclipse.org/r/c/platform/eclipse.platform.swt/+/181275
https://git.eclipse.org/c/platform/eclipse.platform.swt.git/commit/?id=31b3cd263c44bb6c1c78393d023debd6353514a2
Whiteboard:
Bug Depends on:    
Bug Blocks: 571909    
Attachments:
Description Flags
Example program to show the bug none

Description Christoph Nievergelt CLA 2021-04-22 16:44:01 EDT
Created attachment 286204 [details]
Example program to show the bug

Class org.eclipse.swt.browser.Edge introduced with version 4.19 depends on dynamic library WebView2Loader.dll that is packed into the swt library jar.
The class has a static initializer that should copy the DLL from the jar to the directory C:\Users\{user}\.swt\lib\win32\x86_64.
Because the code currently looks like this:

class Edge extends WebBrowser {
	static {
		Library.loadLibrary("WebView2Loader", false);
	}

instead of 

class Edge extends WebBrowser {
	static {
		Library.loadLibrary("WebView2Loader.dll", false);
	}

it fails to copy the DLL and thus fails to load it, resulting in an exception like this:

Exception in thread "main" java.lang.UnsatisfiedLinkError: Could not load SWT library. Reasons: 
	no WebView2Loader in java.library.path: [C:\development\tools\java\zulu11.43.21-ca-jdk11.0.9-x64\bin, C:\WINDOWS\Sun\Java\bin, C:\WINDOWS\system32, C:\WINDOWS, ... , .]
	Can't load library: C:\Users\niec\.swt\lib\win32\x86_64\WebView2Loader

	at org.eclipse.swt.internal.Library.loadLibrary(Library.java:338)
	at org.eclipse.swt.browser.Edge.<clinit>(Edge.java:28)
	at org.eclipse.swt.browser.BrowserFactory.createWebBrowser(BrowserFactory.java:31)
	at org.eclipse.swt.browser.Browser.<init>(Browser.java:96)
	at SwtEdgeBrowserTest.display(SwtEdgeBrowserTest.java:12)
	at SwtEdgeBrowserTest.main(SwtEdgeBrowserTest.java:29)
Comment 1 Niraj Modi CLA 2021-04-23 03:25:58 EDT
(In reply to Christoph Nievergelt from comment #0)
> Created attachment 286204 [details]
> Example program to show the bug

Above snippet works for me as expected.

Try cleaning below directory and retry:
C:\Users\{user}\.swt\lib\win32\x86_64
Comment 2 Christoph Nievergelt CLA 2021-04-23 05:08:57 EDT
I cleaned the directory multiple times while figuring out the problem.

The file in the jar has name "WebView2Loader.dll", so org.eclipse.swt.internal.Library#extractResource will fail (line 143) to write it to C:\Users\{user}\.swt\lib\win32\x86_64.

If you rename the file in the jar to "WebView2Loader" the method can write it, but Windows will not allow loading the file as DLL:
Exception in thread "main" java.lang.UnsatisfiedLinkError: Could not load SWT library. Reasons: 
	no WebView2Loader in java.library.path: [C:\development\tools\java\zulu11.43.21-ca-jdk11.0.9-x64\bin, C:\WINDOWS\Sun\Java\bin, C:\WINDOWS\system32, C:\WINDOWS, ... , ., ., C:\development\tools\Git LFS, .]
	Can't load library: C:\Users\niec\.swt\lib\win32\x86_64\WebView2Loader
	C:\Users\niec\.swt\lib\win32\x86_64\WebView2Loader: Can't find dependent libraries

	at org.eclipse.swt.internal.Library.loadLibrary(Library.java:338)
	at org.eclipse.swt.browser.Edge.<clinit>(Edge.java:28)
	at org.eclipse.swt.browser.BrowserFactory.createWebBrowser(BrowserFactory.java:31)
	at org.eclipse.swt.browser.Browser.<init>(Browser.java:96)
	at SwtEdgeBrowserTest.display(SwtEdgeBrowserTest.java:22)
	at SwtEdgeBrowserTest.main(SwtEdgeBrowserTest.java:39)

I suspect that you have WebView2Loader.dll somewhere in your Java path; in that case java.lang.ClassLoader#loadLibrary will use the native method System.mapLibraryName(name) that appends ".dll" to the library name, which makes your test successful.
Comment 3 Christoph Nievergelt CLA 2021-04-23 07:00:04 EDT
When I change the class Edge like I proposed, everything works as wanted.
Comment 4 Christoph Nievergelt CLA 2021-04-29 03:13:40 EDT
(In reply to Niraj Modi from comment #1)
> (In reply to Christoph Nievergelt from comment #0)
> > Created attachment 286204 [details]
> > Example program to show the bug
> 
> Above snippet works for me as expected.
> 
> Try cleaning below directory and retry:
> C:\Users\{user}\.swt\lib\win32\x86_64

Sorry, did not use the Bugzilla UI correctly.
I added my replies as comments.
Comment 5 Eclipse Genie CLA 2021-06-02 09:00:10 EDT
New Gerrit change created: https://git.eclipse.org/r/c/platform/eclipse.platform.swt/+/181275
Comment 6 Nikita Nemkin CLA 2021-06-02 09:20:14 EDT
Thanks for the report. Dynamic extraction of WebView2Loader.dll from the jar is indeed completely broken.

It's a problem in SWT's implementation of Library.loadLibrary. To load the library from java.library.path, a name without the extension is needed. To extract it property from the jar, a name with extension must be used. Simply adding .dll as you suggest will break the first scenario.

I'll fix this, but in the meantime a simple work-around is to manually place WebView2Loader.dll into java.library.path (for example, the current directory or java.exe directory).
Comment 8 Niraj Modi CLA 2021-06-03 01:38:17 EDT
Thanks Nikita for the quick response, resolving now.