На фоне
современной веб стартапной истерии
может показаться, что у языков
программирования C/C++, Java и прочих
Fortran-ов нет будущего. Что это умирающая
культура. Но это не так. Вот, к примеру,
Java — масса проектов, невероятное
количество полезных (и не очень) библиотек
Just look on
Sourceforge, the are 3267 Python-related projects, and 12126
Java-related projects. And that not counting commercial interests
А раз так, очень
вероятно, что одна (или много) из
существующих Java библиотек может
понадобиться при разработке чего-либо
на Python. Нет проблем, JPype нас спасет.
JPype is an effort
to allow python programs full access to java class libraries. This is
achieved not through re-implementing Python, as Jython/JPython has
done, but rather through interfacing at the native level in both
Virtual Machines.
Once JPype installed (you'll have to hack a bit
some files to integrate seamlessly with your system) you can access
java classes by doing something like that:
import jpype
jpype.startJVM(jpype.getDefaultJVMPath())
# you can then access to the basic java functions
jpype.java.lang.System.out.println("hello world")
# and you have to shutdown the VM at the end
jpype.shutdownJVM()
Here is a simple example of how to use
boilerpipe... from python
import jpype
# start the JVM with the good classpaths
classpath = "dist/boilerpipe-1.1-dev.jar:lib/nekohtml-1.9.13.jar:lib/xerces-2.9.1.jar"
jpype.startJVM(jpype.getDefaultJVMPath(), "-Djava.class.path=%s" % classpath)
# get the Java classes we want to use
DefaultExtractor = jpype.JPackage("de").l3s.boilerpipe.extractors.DefaultExtractor
# call them !
print DefaultExtractor.INSTANCE.getText(jpype.java.net.URL("http://blog.notmyidea.org"))
And you get what you want.
Я не разбирался,
каков механизм связи Python с Java VM, а надо
бы. Очень интересно. Накладные расходы,
побочные эффекты, ограничения и все
такое.
Комментариев нет:
Отправить комментарий