尝试使用类路径运行Java程序; Java无法找到我原来的编译类 [英] Trying to run Java program with classpath; java is not able to find my original compiled class

查看:65
本文介绍了尝试使用类路径运行Java程序; Java无法找到我原来的编译类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


Java的菜鸟在这里.我正在尝试编译一个使用stanford-corenlp-3.9.1.jar的Java程序.我正在尝试使用MacOS终端进行编译.以下是各种命令的输出


java noob here. I am trying to compile a run a java program which uses stanford-corenlp-3.9.1.jar. I am trying to compile this using MacOS terminal. Following are the outputs of various commands

javac QuestionsToAnswer.java
这会导致大量错误
QuestionsToAnswer.java:5: error: package edu.stanford.nlp.trees does not exist import edu.stanford.nlp.trees.Tree; ^ QuestionsToAnswer.java:6: error: package edu.stanford.nlp.trees.tregex does not exist import edu.stanford.nlp.trees.tregex.TregexMatcher; ^ ...

javac QuestionsToAnswer.java
This leads to huge list of errors
QuestionsToAnswer.java:5: error: package edu.stanford.nlp.trees does not exist import edu.stanford.nlp.trees.Tree; ^ QuestionsToAnswer.java:6: error: package edu.stanford.nlp.trees.tregex does not exist import edu.stanford.nlp.trees.tregex.TregexMatcher; ^ ...

所以我改为运行:
javac -cp stanford-corenlp-3.9.1.jar QuestionsToAnswer.java
这将按预期工作,并创建一个QuestionToAnswer.class文件.

So instead I run this:
javac -cp stanford-corenlp-3.9.1.jar QuestionsToAnswer.java
This works as expected and creates a QuestionToAnswer.class file.

然后当我尝试运行程序时
java QuestionsToAnswer
它给了我以下错误:
Error: Unable to initialize main class QuestionsToAnswer Caused by: java.lang.NoClassDefFoundError: edu/stanford/nlp/trees/Tree

Then when I try to run the program
java QuestionsToAnswer
It gives me the following error:
Error: Unable to initialize main class QuestionsToAnswer Caused by: java.lang.NoClassDefFoundError: edu/stanford/nlp/trees/Tree

要解决此问题,我将类路径添加到java命令,以便它能够从斯坦福大学的图书馆中找到Tree类.
java -cp stanford-corenlp-3.9.1.jar QuestionsToAnswer
然后它给了我以下错误:
Error: Could not find or load main class QuestionsToAnswer Caused by: java.lang.ClassNotFoundException: QuestionsToAnswer

To fix this I add the classpath to the java command so that it is able to find the Tree Class from stanford's library
java -cp stanford-corenlp-3.9.1.jar QuestionsToAnswer
Then it gives me the following error:
Error: Could not find or load main class QuestionsToAnswer Caused by: java.lang.ClassNotFoundException: QuestionsToAnswer

添加类路径后,java无法找到我的原始类文件,该文件已被编译并存在于目录中.我在这里做什么错了?

After adding the classpath, java is not able to find my original class file which is already compiled and present in the directory. What am I doing wrong here?

推荐答案

jar文件必须位于类路径中,因为您的代码使用了此jar中的类.因此Java必须能够找到它们.

The jar file must be in the classpath because your code uses classes from this jar. So Java must be able to find them.

包含您的类的目录也必须在类路径中,否则Java可能找不到它.

The directory containing your class must also be in the classpath otherwise Java can't possibly find it.

java -cp .:stanford-corenlp-3.9.1.jar QuestionsToAnswer

在Unix/MacOS上,或

on Unix/MacOS, or

java -cp .;stanford-corenlp-3.9.1.jar QuestionsToAnswer

在Windows上.

这篇关于尝试使用类路径运行Java程序; Java无法找到我原来的编译类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆