JNIUS& TIKA - 尝试parseToString

时间:2017-05-14 09:55:45

标签: java python apache-tika pyjnius tika-server

试图用jnius运行tike-app但遇到了问题(macOS Sierra,Java 1.8 JDK,Python 2.7和Python 3.6) 一切正常(tika.detect输出正常),直到parseToString命令。如果运行此命令,似乎会弹出一个弹出窗口 (也用java程序测试,它的工作原理)。但是使用jnius运行它会停止工作,没有输出也没有错误。

import os

os.environ['CLASSPATH'] = "tika-app-1.14.jar"
from jnius import autoclass
from jnius import JavaException

# Import the Java classes
Tika = autoclass('org.apache.tika.Tika')
Metadata = autoclass('org.apache.tika.metadata.Metadata')
File = autoclass('java.io.File')

# Raise an exception and continue if parsing fails
try:
    file = File('./source/test.doc')
    tika = Tika()
    meta = Metadata()
    detectText = tika.detect(file)
    print(detectText) # Working the output is: application/msword
    contentText = tika.parseToString(file) #here it stops no further steps are executed
    print(contentText)
except (JavaException,UnicodeDecodeError) as e:
    print("ERROR: %s" % (e))

1 个答案:

答案 0 :(得分:1)

最后我找到了解决方案。 JVM缺少一个选项 告诉tiki.jar使用无头模式。

#Config have to be before import minus
import jnius_config
jnius_config.add_options('-Djava.awt.headless=true')

import os
os.environ['CLASSPATH'] = "tika-app-1.14.jar"

from jnius import autoclass

## Import the Java classes we are going to need
Tika = autoclass('org.apache.tika.Tika')
Metadata = autoclass('org.apache.tika.metadata.Metadata')
FileInputStream = autoclass('java.io.FileInputStream')

tika = Tika()
meta = Metadata()
text = tika.parseToString(FileInputStream("./source/test.doc"), meta)
print(text)