While we are on topic, I am having some trouble understanding JPype classpath. How do I init the JVM with the folder in which the Python program is located included in the classpath?
I tried t = JPackage('.').test
That did not work.
My environment variable includes current folder in the classpath
I tried passing it as an argument to startJVM. Didn't help.
I think my main use is going to be using CPython with a few Java custom classes and if anyone has a snippet on this it would really help me. Thanks
That's easy.
First realise that "." denotes the current working directory, not the "directory where the python program is located".
Second, JPackage uses Java package names.
So, assuming you java classes are properly stord in a directory called "classes" at the same level as your python script (see how java looks up classes on the filesystem for what "properly" means), here is a snippet that will do what you wanted :
#============================================================== import os, os.path, jpype
root = os.path.abspath(os.path.dirname(__file__)) jpype.startJVM(jpype.getDefaultJVMPath(), "-Djava.class.path=%s%sclasses" % (root, os.sep))
#==============================================================
Or alternately, if the Java classes you want to use ar in JAR files, in a lib directory at the same level as your python script :
#============================================================== import os, os.path, jpype
root = os.path.abspath(os.path.dirname(__file__)) jpype.startJVM(jpype.getDefaultJVMPath(), "-Djava.ext.dirs=%s%slib" % (root, os.sep))
#==============================================================
The magic in the above script is the __file__ variable, which stores the absolute path to the currently executing Python script. If you have both situation (you classes in the classes directory and some JARS containing stuff that they uses) you can combine the above :
#============================================================== import os, os.path, jpype
root = os.path.abspath(os.path.dirname(__file__)) jpype.startJVM(jpype.getDefaultJVMPath(), "-Djava.class.path=%s%sclasses" % (root, os.sep), "-Djava.ext.dirs=%s%slib" % (root, os.sep))
#==============================================================
Hopefully this will help.
-- Steve Menard -------------------- Maintainer of http://jpype.sourceforge.net -- http://mail.python.org/mailman/listinfo/python-list