尽管类路径中存在类,但ClassNotFoundException [英] ClassNotFoundException despite class in the classpath

查看:354
本文介绍了尽管类路径中存在类,但ClassNotFoundException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ClassNotFoundException问题是在SO上找到的,但即使如此,我也没有在过去的答案中找到问题的答案:(
基本上我在尝试从命令行,使用项目文档中提供的命令提示符。提示符如下:

The ClassNotFoundException questions are found by the dozens on SO but even so I did not find the answer to my problem in past answers :( Basically I get a ClassNotFoundException while trying to run a large open source program from the command line using a command prompt that is provided in the project's doc. The prompt is as follow:

java -cp "target/classes/*;../../Algotrader/code/target/classes/*;../../lib/*;../../target/*" -Dsimulation=true -DdataSource.dataSet=1year com.algoTrader.starter.SimulationStarter simulateWithCurrentParams

(注意:原始命令实际上说的是java.exe,但我将其更改为java,因为在我的Mac上无法将java.exe识别为命令)

(note: the original command actually says java.exe but I changed it to java as java.exe is not recognised as a command on my Mac)

SimulationStarter 类抛出了异常通过堆栈跟踪:

The exception is thrown for the SimulationStarter class as shown by the stack trace:

Exception in thread "main" java.lang.NoClassDefFoundError: com/algoTrader/starter/SimulationStarter
Caused by: java.lang.ClassNotFoundException: com.algoTrader.starter.SimulationStarter
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)

在Eclipse中,我可以看到SimulationStarter.class位于 AlgoTrader / code / target / classes / com / algoTrader / starter 中,该路径与中提供的路径一致

From Eclipse I can see that SimulationStarter.class is in AlgoTrader/code/target/classes/com/algoTrader/starter, which looks in line with the path provided in the command prompt.

所以我的问题是:除了将类错误地放置在类路径中之外,导致此异常的原因是什么?

So my question is: what could be the cause(s) for this exception other than the class being incorrectly placed in the classpath?

也不知道这有什么区别,但是该项目保存在svn和Maven下,并且我在一个Mac。

Also not sure this makes any difference but the project is kept under svn and Maven and I am running it on a Mac.

正确的类路径

最后,命令中给出的类路径提示错误。正确的路径(至少可以解决问题的路径)是:

In the end the classpath given in the command prompt was at fault. The correct paths (at least ones that solve the problem) are:

java -cp target / classes /:../../ Algotrader / code / target / classes /:target / * -Dsimulation = true -DdataSource.dataSet = 1year com.algoTrader.starter.SimulationStarter SimulationWithCurrentParams

java -cp "target/classes/:../../Algotrader/code/target/classes/:target/*" -Dsimulation=true -DdataSource.dataSet=1year com.algoTrader.starter.SimulationStarter simulateWithCurrentParams

与原始提示的主要区别是删除除了包含jar文件的文件夹以及缩短的基本类路径路径以仅捕获基本目录之外,其他所有星号都没有。另外,应使用:代替;在Mac和Linux上,按照
@reprogrammer答案

The main differences with the original prompt is the removal of the stars except for the folder that contains the jar files and the shortening of the base classpath paths to capture only the base directories. Also ":" should be used instead of ";" on Macs and Linux as per @reprogrammer answer

推荐答案

确定要全部 * 在那里?

通常,您需要提供类路径的基本目录,而不是子文件夹。

Usually, you will want to give the base directories of the classpath, not subfolders.

例如将应用程序构建为 bin时,应使用 java -cp bin mainclass ,而不是 java -cp bin / * !对 * 的支持通常有点不稳定,因为它是一个外壳元字符,您需要正确引用。如果您的类路径不正确,它可能会真的。我看到人们遇到了问题,因为他们在他们的类路径中添加了 README.TXT

E.g. when building your application into "bin", you would use java -cp bin mainclass, not java -cp bin/*! The support for * is generally a bit flaky, as it is a shell metacharacter, and you need to get quoting right. It can really screw you if you have an incorrect classpath. I've seen people have issues because they added README.TXT to their classpath.

这篇关于尽管类路径中存在类,但ClassNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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