Mac 用户 - 如何在 Mac 中设置 CLASSPATHS(我正在开发 Lucene 演示)

发布于 2024-10-15 12:46:29 字数 1035 浏览 4 评论 0原文

我正在尝试让我的 Apache Lucene 演示正常工作,并且我正在本教程中设置类路径 http://lucene.apache.org/java/2_3_2/demo.html

我在网上搜索了一下,这是我发现的设置 CLASSPATH 的 2 个解决方案:

CLASSPATH=${CLASSPATH}:/Users/philhunter/Desktop/COM562\ Project/lucene-3.0.3/lucene-core-3.0.3.jar

第二

setenv CLASSPATH ${CLASSPATH}:/Users/philhunter/Desktop/COM562\ Project/lucene-3.0.3/lucene-core-3.0.3.jar 

个提出了一个错误 -bash:setenv:找不到命令

第一个似乎可以接受,但是当我尝试教程中的下一步时,我收到了错误。下一步是运行以下命令:

Phil-hunters-MacBook:webapps philhunter$ java org.apache.lucene.demo.IndexFiles /Users/philhunter/Desktop/COM562\ Project/lucene-3.0.3/src

这给了我错误:

线程“main”中出现异常 java.lang.NoClassDefFoundError: org/apache/lucene/demo/IndexFiles

这让我相信我的 CLASSPATHS 没有正确设置。我的假设正确吗?我尝试过其他教程和演示,并发现很多时候都会出现同样的错误。我对 Lucene 很陌生,对 mac 和 Unix shell 脚本也比较陌生。有人知道我是否正确设置了 CLASSPATH,这是否是错误的原因?

I'm trying to get my Apache Lucene demo to work and I'm down to setting the classpath in this tutorial http://lucene.apache.org/java/2_3_2/demo.html

I've hunted the web and these wer the 2 solutions I found to set CLASSPATH:

CLASSPATH=${CLASSPATH}:/Users/philhunter/Desktop/COM562\ Project/lucene-3.0.3/lucene-core-3.0.3.jar

and

setenv CLASSPATH ${CLASSPATH}:/Users/philhunter/Desktop/COM562\ Project/lucene-3.0.3/lucene-core-3.0.3.jar 

The second one brings up a error
-bash: setenv: command not found

The first one seemed to accept ok but wen i tried the next step in the tutorial i got an error. The next step was to run the following:

Phil-hunters-MacBook:webapps philhunter$ java org.apache.lucene.demo.IndexFiles /Users/philhunter/Desktop/COM562\ Project/lucene-3.0.3/src

which gave me the error:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/lucene/demo/IndexFiles

This leads me to believe my CLASSPATHS didnt set correctly. Would I be right in assuming this? I have tried other tutorials and demos and see to get this same error quite a bit. Im new to Lucene and relatively new to mac and Unix shell scripting. Anyone know if I am setting the CLASSPATH correctly and if thats the cause of the errors?

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

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

发布评论

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

评论(3

时光与爱终年不遇 2024-10-22 12:46:29

在终端中键入

$ vim ~/.bash_profile

编辑文件并添加一行:

export CLASSPATH=${CLASSPATH}:/usr/local/lucene-3.6.2/lucene-core-3.6.2.jar:/usr/local/lucene-3.6.2/contrib/demo/lucene-demo-3.6.2.jar;

确保更改您的路径。

以您的方式,您无法在类路径中添加 lucene-demo-3.0.3.jar 。

in the terminal type

$ vim ~/.bash_profile

edit the file and add one line:

export CLASSPATH=${CLASSPATH}:/usr/local/lucene-3.6.2/lucene-core-3.6.2.jar:/usr/local/lucene-3.6.2/contrib/demo/lucene-demo-3.6.2.jar;

make sure to change the path of yours.

In your way you lose to add lucene-demo-3.0.3.jar in your classpath.

你的他你的她 2024-10-22 12:46:29

当您设置像 CLASSPATH 这样的环境变量时,默认情况下它仅适用于当前进程(即 shell 进程本身) - 它不适用于您在下一行启动的 java 进程。为了使其可供其他进程使用,您需要“导出”该变量。在这种情况下,您可以使用类似的内容:

export CLASSPATH=${CLASSPATH}:/Users/philhunter/Desktop/COM562\ Project/lucene-3.0.3/lucene-core-3.0.3.jar

这基本上表示“将 CLASSPATH 变量设置为其当前值加上 lucene jar 的位置,并使新变量可用于从此 shell 启动的任何进程”。

但是,对于 java,设置类路径的常用方法是使用 -classpath-cp 将其作为 java 命令本身的一部分来执行> 选项。在您的情况下,它看起来像这样:

Phil-hunters-MacBook:webapps philhunter$ java -cp /Users/philhunter/Desktop/COM562\ Project/lucene-3.0.3/lucene-core-3.0.3.jar org.apache.lucene.demo.IndexFiles /Users/philhunter/Desktop/COM562\ Project/lucene-3.0.3/src

顺便说一句,您在使用 setenv 行时看到的错误是因为 setenv 是 C shell 中用于设置环境的命令变量,但默认的 Mac shell(以及您正在使用的 shell)是 bash,它无法识别 setenv 并让您知道它无法通过错误消息:-bash:setenv:找不到命令

When you set an environment variable like CLASSPATH then by default it only applies to the current process (i.e. the shell process itself) - it isn't available to the java process you launch in the next line. In order to make it available to other processes you need to "export" the variable. In this case you can use something like:

export CLASSPATH=${CLASSPATH}:/Users/philhunter/Desktop/COM562\ Project/lucene-3.0.3/lucene-core-3.0.3.jar

This basically says "set the CLASSPATH variable to its current value plus the location of the lucene jar, and make the new variable available to any processes launched from this shell".

However, with java the usual way of setting the classpath is to do it as part of the java command itself, using the -classpath or -cp options. In your case it would look something like:

Phil-hunters-MacBook:webapps philhunter$ java -cp /Users/philhunter/Desktop/COM562\ Project/lucene-3.0.3/lucene-core-3.0.3.jar org.apache.lucene.demo.IndexFiles /Users/philhunter/Desktop/COM562\ Project/lucene-3.0.3/src

As an aside, the error you see when using the setenv line is because setenv is the command used in the C shell to set environment variables, but the default Mac shell (and the shell you're using) is bash which doesn't recognise setenv and lets you know it doesn't recognise it with the error message: -bash: setenv: command not found.

不知在何时 2024-10-22 12:46:29

我在我的主目录中创建一个 .bash_profile 文件,并执行类似的

export GRAILS_HOME=/usr/share/grails
...
export PATH=${GRAILS_HOME}/bin:${GROOVY_HOME}/bin:/usr/local/mysql-5.1.45-osx10.6-x86_64/bin:${PATH}

操作来设置类路径 - 这些示例展示了如何声明环境变量以及如何在其他变量中使用该变量。

i create a .bash_profile file in my home directory and do things like

export GRAILS_HOME=/usr/share/grails
...
export PATH=${GRAILS_HOME}/bin:${GROOVY_HOME}/bin:/usr/local/mysql-5.1.45-osx10.6-x86_64/bin:${PATH}

you can work of that to set the classpath -- these examples show how to declare an environment variable and how to use the variable in other variables.

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