NLTK 无法找到 Java 可执行文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7404720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NLTK fails to find the Java executable
提问by Thomas Chu
I am using NLTK's nltk.tag.stanford, which needs to call the java executable.
我正在使用 NLTK 的 nltk.tag.stanford,它需要调用 java 可执行文件。
I set JAVAHOME to C:\Program Files\Java\jdk1.6.0_25 where my jdk is installed, but when run the program I get the error
我将 JAVAHOME 设置为 C:\Program Files\Java\jdk1.6.0_25 安装了 jdk 的位置,但是在运行程序时出现错误
"NLTK was unable to find the java executable! Use the config_java() or set the JAVAHOME variable"
Then I spent 3 hours on debugging it and tried
然后我花了 3 个小时调试它并尝试
config_java("C:/Program Files/Java/jdk1.6.0_25/")
config_java("C:/Program Files/Java/jdk1.6.0_25/bin/")
and those without the ending "/".
However the nltk still cannot find it.
但是 nltk 仍然找不到它。
Anyone has idea about what's going wrong? Thanks a loooot!
任何人都知道出了什么问题?感谢 loooot!
回答by Alan
If setting the JAVA_HOME environment doesn't help you, try this:
如果设置 JAVA_HOME 环境对您没有帮助,请尝试以下操作:
config_java()
did not work for me. I add the following lines to my code and it worked:
config_java()
对我不起作用。我将以下行添加到我的代码中并且它起作用了:
import os
java_path = "C:/Program Files/Java/jdk1.7.0_11/bin/java.exe"
os.environ['JAVAHOME'] = java_path
I am running Windows 7 64-bit
我正在运行 Windows 7 64 位
回答by duhaime
I spent about seven hours working through this problem, and finally found a solution. You can write your java directory right into lines 69 and 72 of the internals.py file (build 2.0.4) as follows:
我花了大约七个小时解决这个问题,终于找到了解决方案。您可以将 java 目录直接写入 internals.py 文件(构建 2.0.4)的第 69 和 72 行,如下所示:
##########################################################################
# Java Via Command-Line
##########################################################################
_java_bin = 'C:\Program Files\Java\jdk1.7.0_25\bin\java.exe'
_java_options = []
# [xx] add classpath option to config_java?
def config_java(bin='C:\Program Files\Java\jdk1.7.0_25\bin\java.exe', options=None, verbose=True):
This resolves the problem for me. (I'm working in a 32 bit Windows environment)
这为我解决了问题。(我在 32 位 Windows 环境中工作)
回答by Alexander Measure
protos1210's tip worked for me, with a few minor changes. The full answer is:
protos1210 的提示对我有用,有一些小的改动。完整的答案是:
import nltk
nltk.internals.config_java("C:/Program Files/Java/jdk1.6.0_30/bin/java.exe")
After I restarted IDLE, the following code worked.
重新启动 IDLE 后,以下代码有效。
import nltk
path_to_model = "C:/Program Files/stanford-postagger-2012-05-22/models/english-bidirectional-distsim.tagger"
path_to_jar = "C:/Program Files/stanford-postagger-2012-05-22/stanford-postagger.jar"
tagger = nltk.tag.stanford.POSTagger(path_to_model, path_to_jar)
tokens = nltk.tokenize.word_tokenize("I hope this works!")
print tagger.tag(tokens)
Output is: [('I', 'PRP'), ('hope', 'VBP'), ('this', 'DT'), ('works', 'VBZ'), ('!', '.')].
输出是: [('I', 'PRP'), ('hope', 'VBP'), ('this', 'DT'), ('works', 'VBZ'), ('!', ' .')]。
I never could get it to recognize my JAVAHOME environment variables.
我永远无法让它识别我的 JAVAHOME 环境变量。
回答by Ernest Friedman-Hill
回答by Tyler
depending on your environment you might want to try reinstalling the nltk binary. I installed from binary and then later upgraded via easy_install and it incorrectly installed the osx version of nltk which caused exceptions when ntlk couldn't find my java binary.
根据您的环境,您可能想尝试重新安装 nltk 二进制文件。我从二进制安装,然后通过 easy_install 升级,它错误地安装了 nltk 的 osx 版本,当 ntlk 找不到我的 java 二进制时导致异常。
回答by protoss1210
I too have been running into problems with this. It has been such a headache!
我也遇到了这个问题。一直很头疼!
I got this to work on my machine (Win7_x64)
我让它在我的机器上工作(Win7_x64)
Replace 'jdk1.6.0_30' with your version of the jdk. Run this command:
将“jdk1.6.0_30”替换为您的 jdk 版本。运行此命令:
config_java("C:/Program Files/Java/jdk1.6.0_30/bin/java.exe")
[Found C:/Program Files/Java/jdk1.6.0_30/bin/java.exe: C:/Program Files/Java/jdk1.6.0_30/bin/java.exe]
I do not know why it has been this difficult to get working. Hope this helps!
我不知道为什么上班这么难。希望这可以帮助!
回答by demongolem
Another possibility when facing this error message while using the stanford package in NLTK is if you use StanfordTagger
instead of PosTagger
or NERTagger
. According to Google Groups, there was a design to encourage users away from the general StanfordTagger
class and towards one of the two specific taggers.
在 NLTK 中使用 stanford 包时遇到此错误消息的另一种可能性是,如果您使用StanfordTagger
代替PosTagger
或NERTagger
。根据Google Groups 的说法,有一种设计可以鼓励用户远离一般StanfordTagger
类,而转向两个特定标记器中的一个。
回答by demongolem
Another distinct answer for this situation is you are using an IDE such as Eclipse. Even if you have set your JAVA_HOME
environment variable and even if you explicitly call config_java
and you get the [Found ... /bin/java.exe]
message returned to you, you could still have to set the runtime environment for your IDE. The reason is that when you invoke the tagger, config_java
is called again as part of the process and your original attempts at settings the path to the java binary executable can therefore be overwritten.
这种情况的另一个明显答案是您使用的是 IDE,例如 Eclipse。即使您已经设置了JAVA_HOME
环境变量,并且即使您显式调用config_java
并收到[Found ... /bin/java.exe]
返回给您的消息,您仍然可能需要为您的 IDE 设置运行时环境。原因是当您调用标记器时,config_java
作为过程的一部分再次调用,因此您最初尝试设置 java 二进制可执行文件的路径可能会被覆盖。
回答by Renklauf
I realize that this is an old question but here is the solution that worked for me (running on Windows 7-64 bit). Hopefully it will save someone some time.
我意识到这是一个老问题,但这是对我有用的解决方案(在 Windows 7-64 位上运行)。希望它会节省一些时间。
I implemented the solution given here:
我实现了这里给出的解决方案:
"I have been able to get it working by commenting out two lines in the batch_tag function in
\nltk\tag\stanford.py
The lines are line 59 and 85.
config_java(options=self.java_options, verbose=False)
and
config_java(options=default_options, verbose=False)
respectively."
After commenting out the lines I set the path to the Java executable in the same manner mentioned in other answers:
在注释掉这些行之后,我以其他答案中提到的相同方式设置了 Java 可执行文件的路径:
nltk.internals.config_java("path/to/javadk/bin/java.exe")
A kludgey but workable solution. Everything worked fine after that.
一个笨拙但可行的解决方案。之后一切正常。
回答by mariera
Hopefully this saves someone else some time when trying to fix this problem. I'm pretty new to programming, Python and the NLTK, and didn't realize when I was trying to implement @dduhaime's solution that there are two 'internals.py' files: one in the nltk folder (path=C:\nltk-2.0.4 on my computer) and one in my Python27 folder (path=C:\Python27\Lib\site-packages\nltk-2.0.4-py2.7.egg\nltk on my computer). You have to add the path to the java directory on lines 69 & 72 in the latter 'internals.py' file, or the NLTK will still not be able to find it.
希望这可以在尝试解决此问题时为其他人节省一些时间。我对编程、Python 和 NLTK 还很陌生,当我尝试实现 @dduhaime 的解决方案时没有意识到有两个“internals.py”文件:一个在 nltk 文件夹中(路径 = C:\nltk -2.0.4 在我的电脑上)和一个在我的 Python27 文件夹中(路径=C:\Python27\Lib\site-packages\nltk-2.0.4-py2.7.egg\nltk 在我的电脑上)。您必须在后一个“internals.py”文件的第 69 和 72 行添加 java 目录的路径,否则 NLTK 仍然无法找到它。
My environment: Windows 7 64 bit, NLTK build 2.0.4
我的环境:Windows 7 64 位,NLTK build 2.0.4