Search code examples
javajxbrowser

Jxbtrowser retrieve platform specific artefact at runtime


I'm writing an intelij plugin and would like to download the platform specific artefact at runtime.

I've loaded the platform specific jar into a class loader but the ChromiumExtractor cannot access the nested resources when prefixed with "/". So I can access the resource as "chromium-mac.zip" but the library cannot.

I've tried to unzip the nested zipped chromium artefact into the correct directory but this does not leading to a working solution. So now I've been trying to piece together the way the library extracts the artefact but it's rather tedious as the code is obfuscated.

Does the jxbrowser plugin have some support for retrieving the artefact at runtime. Could such support be added (jxbtrowser devs use SO for support questions etc, this is a message to them :D ) ?

Approach taken :

// inside intelij plugin . The plugin has the jxbrowser-6.6.jar 
// and license.jar loaded into the classloader. the platform specific
// artefact will be retrieved manual).

val cl = URLClassLoader(arrayOf(URL("file://.../jxbrowser-mac-6.6.jar")), Browser::class.java.classLoader)


val backup = Thread.currentThread().contextClassLoader
try {
    Thread.currentThread().contextClassLoader = cl
    // can access like this
    Thread.currentThread().contextClassLoader.getResource("chromium-mac.zip")
    val ce = ChromiumExtractor.create()
    // cannot access as resource is retrieved "/chromium-mac.zip" ? 
    ce.extract(BrowserPreferences.getChromiumDir())

    browser = Browser()
} finally {
    Thread.currentThread().contextClassLoader = backup
}

Solution

  • The following does the trick, The resource jar had to be in the same class loader as the client jar (as well as the license). It would be nice if JxBrowser added a helper for this that is capable of performing the download and initialising chromium, perhaps taking just a path for a persistent storage directory.

    private fun initializeJxBrowser(): Browser {
        if(ChromiumExtractor.create().shouldExtract(BrowserPreferences.getChromiumDir())) {
           val cl = URLClassLoader(arrayOf(
                    URL("file:.../license.jar"),
                    URL("file:.../jxbrowser-mac-6.6.jar"),
                    URL("file:../jxbrowser-6.6.jar")
                    ))
    
            cl.loadClass("com.teamdev.jxbrowser.chromium.BrowserContext")
              .getMethod("defaultContext")
              .invoke(null)
       }
       return Browser()
    

    }