Including python code into jar file?

227 views
Skip to first unread message

Ilya Platonov

unread,
Feb 23, 2021, 6:27:09 PM2/23/21
to Jep Project
Is there a way to bundle python files into jar file somehow or they need to be on file system?

I tried to use JepConfig().setIncludePath("classpath:") but it does not work.

I also tried to use "jar:file:/path/to/file.jar/search_core.jar!"

Ben Steffensmeier

unread,
Feb 24, 2021, 10:19:40 AM2/24/21
to Jep Project
Since a jar file is just a zip file you might be able to hook something up using zipimport. I haven't tried it but something like this might work:

import zipimport
import sys
sys.meta_path.append(zipimport.zipimporter("/path/to/file.jar")

Ilya Platonov

unread,
Feb 24, 2021, 1:32:06 PM2/24/21
to Ben Steffensmeier, Jep Project
Thank you for the pointer.
Apparently you can just add .jar file path to sys.path and it picks up from there. I ended up writing following code(Kotlin)
        this.javaClass.classLoader.getResources("mypackage/__init__.py").asSequence().forEach { url ->
            if (url.protocol == "file") {
                jepConfig.addIncludePaths(Paths.get(url.path).parent.parent.toString())
            } else if (url.protocol == "jar") {
                val path = Paths.get(url.path).parent.parent.toString().trimEnd('!')
                jepConfig.addIncludePaths(path)
            }
        }


Works like a charm.

--
You received this message because you are subscribed to a topic in the Google Groups "Jep Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jep-project/Ia_94lR2rMg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jep-project...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jep-project/26064604-b1df-4799-b18b-ed1455cdca3bn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages