java编译系统找不到指定路径,尝试使用类路径运行Java程序;Java无法找到我原来的编译类...

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

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;

^

...

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.

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

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

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?

解决方案

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

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

on Unix/MacOS, or

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

on Windows.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值