NLTK 找不到 Java 可执行文件

发布于 2024-12-04 12:12:59 字数 507 浏览 2 评论 0原文

我使用的是NLTK的nltk.tag.stanford,它需要调用java可执行文件。

我将 JAVAHOME 设置为安装了 jdk 的 C:\Program Files\Java\jdk1.6.0_25 ,但是当运行该程序时出现错误

"NLTK was unable to find the java executable! Use the config_java() or set the JAVAHOME variable"

然后我花了 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 "/". 

但是 nltk 仍然找不到它。

任何人都知道出了什么问题吗?谢谢你的战利品!

I am using NLTK's nltk.tag.stanford, which needs to call the java executable.

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

"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

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.

Anyone has idea about what's going wrong? Thanks a loooot!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

つ低調成傷 2024-12-11 12:13:00

我花了大约七个小时解决这个问题,终于找到了解决方案。您可以将您的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):

这解决了我的问题。 (我在32位Windows环境下工作)

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 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)

孤独患者 2024-12-11 12:13:00

protos1210 的技巧对我有用,但有一些细微的变化。完整的答案是:

import nltk
nltk.internals.config_java("C:/Program Files/Java/jdk1.6.0_30/bin/java.exe")

在我重新启动 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)

输出为:[('I', 'PRP'), ('希望', 'VBP'), ('this', 'DT'), ('works', 'VBZ'), ('!', ' .')]。

我永远无法让它识别我的 JAVAHOME 环境变量。

protos1210's tip worked for me, with a few minor changes. The full answer is:

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.

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 never could get it to recognize my JAVAHOME environment variables.

蘸点软妹酱 2024-12-11 12:13:00

我查看了此处,文档似乎表明论证应该看起来像

config_java("C:/Program Files/Java/jdk1.6.0_25/bin/java")

I looked here and the docs seem to suggest that the argument ought to look like

config_java("C:/Program Files/Java/jdk1.6.0_25/bin/java")
椵侞 2024-12-11 12:13:00

根据您的环境,您可能需要尝试重新安装 nltk 二进制文件。我从二进制文件安装,然后通过 easy_install 升级,它错误地安装了 osx 版本的 nltk,当 ntlk 找不到我的 java 二进制文件时,这导致了异常。

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.

默嘫て 2024-12-11 12:13:00

我已尝试了上述所有解决方案以及 Google 网上论坛,但没有一个起作用。因此,经过几轮试验和对上述答案的修改后,以下代码对我有用:-

>>>  import os

>>>  os.environ['JAVAHOME'] = "C:/Program Files/Java/jdk1.8.0_31/bin" #insert approriate version of jdk

然后我尝试了 NERTagger 代码:-

>>> from nltk.tag.stanford import NERTagger

>>> st = NERTagger('stanford-ner-2014-06-16/classifiers/english.all.3class.distsim.crf.ser.gz','stanford-ner-2014-06-16/stanford-ner.jar')

>>> st.tag('John has refused the offer from Facebook. He will work for Google'.split())

以下是我收到的输出

'John', u'PERSON'), (u'has', u'O'), (u'refused', u'O'), (u'the', u'O'), (u'offer', u'O'), (u'from', u'O'), (u'Facebook', u'ORGANIZATION'), (u'.', u'O')]

在 Windows 7 64 位上测试

I have tried all the above mentioned solutions and also the ones on Google Groups, but none worked. So after few more rounds of trial and modifications to above answers, the following piece of code worked for me :-

>>>  import os

>>>  os.environ['JAVAHOME'] = "C:/Program Files/Java/jdk1.8.0_31/bin" #insert approriate version of jdk

And then I tried NERTagger code :-

>>> from nltk.tag.stanford import NERTagger

>>> st = NERTagger('stanford-ner-2014-06-16/classifiers/english.all.3class.distsim.crf.ser.gz','stanford-ner-2014-06-16/stanford-ner.jar')

>>> st.tag('John has refused the offer from Facebook. He will work for Google'.split())

And the following was the output I received

'John', u'PERSON'), (u'has', u'O'), (u'refused', u'O'), (u'the', u'O'), (u'offer', u'O'), (u'from', u'O'), (u'Facebook', u'ORGANIZATION'), (u'.', u'O')]

Tested on Windows 7 64-bit

明明#如月 2024-12-11 12:13:00

我也遇到过这方面的问题。一直很头疼啊!

我让它在我的机器(Win7_x64)上工作,

将“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 too have been running into problems with this. It has been such a headache!

I got this to work on my machine (Win7_x64)

Replace 'jdk1.6.0_30' with your version of the jdk. Run this command:

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!

栩栩如生 2024-12-11 12:13:00

在 NLTK 中使用 stanford 包时遇到此错误消息的另一种可能性是,如果您使用 StanfordTagger 而不是 PosTaggerNERTagger。根据 Google 网上论坛,有一个设计鼓励用户远离一般的 StanfordTagger 类并转向两个特定标记器之一。

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.

奶气 2024-12-11 12:13:00

对于这种情况,另一个不同的答案是您正在使用 IDE,例如 Eclipse。即使您已经设置了 JAVA_HOME 环境变量,并且即使您显式调用 config_java 并且您得到了 [Found ... /bin/java.exe]< /code> 消息返回给您,您可能仍然需要为 IDE 设置运行时环境。原因是,当您调用标记器时,config_java 作为该过程的一部分再次被调用,因此您最初尝试设置 java 二进制可执行文件的路径可能会被覆盖。

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.

梦幻的心爱 2024-12-11 12:13:00

我意识到这是一个老问题,但这是对我有用的解决方案(在 Windows 7-64 位上运行)。希望它能为某人节省一些时间。

我实现了此处给出的解决方案:

 "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."

注释掉我以其他答案中提到的相同方式设置了Java可执行文件的路径:

 nltk.internals.config_java("path/to/javadk/bin/java.exe")

一个kludgey但可行的解决方案。之后一切工作正常。

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.

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:

 nltk.internals.config_java("path/to/javadk/bin/java.exe")

A kludgey but workable solution. Everything worked fine after that.

梦屿孤独相伴 2024-12-11 12:13:00

希望这可以节省其他人尝试解决此问题的时间。我对编程、Python 和 NLTK 还很陌生,当我尝试实现 @dduhaime 的解决方案时,我没有意识到有两个“internals.py”文件:一个位于 nltk 文件夹中(path=C:\nltk -2.0.4 在我的电脑上)和一个在我的 Python27 文件夹中(我的计算机上的路径=C:\Python27\Lib\site-packages\nltk-2.0.4-py2.7.egg\nltk)。您必须在第 69 行和第 69 行添加 java 目录的路径。 72 在后面的“internals.py”文件中,否则 NLTK 仍然无法找到它。

我的环境:Windows 7 64位,NLTK build 2.0.4

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.

My environment: Windows 7 64 bit, NLTK build 2.0.4

眼泪淡了忧伤 2024-12-11 12:13:00

我为此实现了一个解决方法,因为 NLTK 误解了 JAVA_HOME 变量的含义:

import os
if os.environ.get("JAVA_HOME") is not None and "/bin" not in os.environ["JAVA_HOME"]:
    os.environ["JAVAHOME"] = os.path.normpath(os.path.join(os.environ["JAVA_HOME"], "bin"))

这基本上采用 JAVA_HOME 中的正确值,创建 NLTK 友好的版本并将其存储在 JAVAHOME 中。 NLTK 将检查两者,以便找到二进制文件。显然,您需要在创建标记器之前执行此操作。

I implemented a workaround for this because NLTK is misunderstanding the meaning of the JAVA_HOME variable:

import os
if os.environ.get("JAVA_HOME") is not None and "/bin" not in os.environ["JAVA_HOME"]:
    os.environ["JAVAHOME"] = os.path.normpath(os.path.join(os.environ["JAVA_HOME"], "bin"))

This basically takes the correct value you have in JAVA_HOME, and creates the NLTK-friendly version and stores it in JAVAHOME. NLTK will check both so this will find the binary. You need to do this before the tagger is created, obviously.

匿名的好友 2024-12-11 12:13:00

我遇到了同样的问题,这对我有用,非常简单。当您设置 JavaHome 变量时,请设置计算机中 jdk 文件夹的路径,如下所示:

C:\Program Files\Java\jdk\ - 这确实有效

C:\Program Files\Java\jdk -这不起作用

I came across the same issue and this is what worked for me which is really simple. When you are setting up JavaHome variable set the path to jdk folder in your machine like below:

C:\Program Files\Java\jdk\ - This did work

C:\Program Files\Java\jdk - This did not work

幻梦 2024-12-11 12:13:00

这个答案适用于 ubuntu 14.04 。

注释掉 \nltk\tag\stanford.py 中的 batch_tag 函数中的两行

,即第 59 行和第 85 行。

config_java (options=self.java_options, verbose=False)
分别config_java(options=default_options, verbose=False)

注释掉这些行后,我以其他答案中提到的相同方式设置了 Java 可执行文件的路径:
nltk.internals.config_java("path/to/javadk/bin/java")

之后一切正常。

This answer is for ubuntu 14.04 .

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:
nltk.internals.config_java("path/to/javadk/bin/java")

Everything worked fine after that.

满栀 2024-12-11 12:12:59

如果设置 JAVA_HOME 环境对您没有帮助,请尝试以下操作:

config_java() 对我不起作用。我将以下几行添加到我的代码中并且它起作用了:

import os
java_path = "C:/Program Files/Java/jdk1.7.0_11/bin/java.exe"
os.environ['JAVAHOME'] = java_path

我正在运行 Windows 7 64 位

If setting the JAVA_HOME environment doesn't help you, try this:

config_java() did not work for me. I add the following lines to my code and it worked:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文