简体   繁体   English

尽管类路径中存在类,但仍发生ClassNotFoundException

[英]ClassNotFoundException despite class in the classpath

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: SO上有数十个类找到了ClassNotFoundException问题,但即使如此,我也无法在过去的答案中找到问题的答案:(基本上,当我尝试使用命令行提示符从命令行运行大型开源程序时,我得到了ClassNotFoundException。在项目的文档中提供,提示如下:

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

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

The exception is thrown for the SimulationStarter class as shown by the stack trace: 如堆栈跟踪所示,为SimulationStarter类引发了异常:

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)

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. 从Eclipse中,我可以看到SimulationStarter.class位于AlgoTrader / code / target / classes / com / algoTrader / starter中 ,其外观与命令提示符中提供的路径一致。

So my question is: what could be the cause(s) for this exception other than the class being incorrectly placed in the classpath? 所以我的问题是:除了将类错误地放置在类路径中之外,导致此异常的原因是什么?

Also not sure this makes any difference but the project is kept under svn and Maven and I am running it on a Mac. 同样不确定这有什么区别,但是项目保存在svn和Maven下,并且我在Mac上运行它。

CORRECT CLASSPATH 正确的类路径

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 simulateWithCurrentParams java -cp“ target / classes /:../../ Algotrader / code / target / classes /:target / *” -Dsimulation = true -DdataSource.dataSet = 1year com.algoTrader.starter.SimulationStarter SimulationWithCurrentParams

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. 与原始提示的主要区别在于,除了包含jar文件的文件夹外,还删除了星号,并缩短了基本类路径路径以仅捕获基本目录。 Also ":" should be used instead of ";" 另外,应使用“:”代替“;” on Macs and Linux as per @reprogrammer answer 在Mac和Linux上,按照@reprogrammer回答

The classpath syntax is OS-dependent. 类路径语法与操作系统有关。 The classpath separator in Linux and Mac OS X is : not ; 在Linux和Mac OS X中,类路径分隔符是: not ; .

Are you sure you need all the * there? 您确定在那里需要所有*吗?

Usually, you will want to give the base directories of the classpath, not subfolders. 通常,您将要提供类路径的基本目录 ,而不是子文件夹。

Eg when building your application into "bin", you would use java -cp bin mainclass , not java -cp bin/* ! 例如,将应用程序构建为“ bin”时,应使用java -cp bin mainclass ,而不是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. 我看到人们遇到了问题,因为他们将README.TXT添加到了他们的类路径中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尽管清单类路径包括包含该类的.jar,但仍发生ClassNotFoundException - ClassNotFoundException despite the manifest classpath including a .jar which contains the class 类路径上的类文件的ClassNotFoundException - ClassNotFoundException for class file on classpath 尽管在MANIFEST中没有找到类路径,但是找不到类 - Class not found despite of classpath in MANIFEST 主要在 static 内部 class 产生 ClassnotFoundException (尽管 $) - Main in static inner class produces ClassnotFoundException (despite $) 即使将类放在classpath目录中,也会重复ClassNotFoundException - Repeats ClassNotFoundException even after class has been put in classpath directory ClassNotFoundException,即使包含该类的jar正确存在于类路径中 - ClassNotFoundException even though the jar containing the class is properly present in the classpath 尽管指定了正确的主类,类路径等,Jar仍未运行 - Jar does not run despite specifying correct main class, classpath, etc 尽管jar在类路径中,但获取ClassNotFoundException - Getting ClassNotFoundException though jar is in classpath 替换classpath中的jar后出现ClassNotFoundException - ClassNotFoundException after replacing a jar in classpath 类路径不起作用-Java lang ClassNotFoundException - classpath not working - java lang ClassNotFoundException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM