简体   繁体   English

即使jar在classpath上,也无法找到类异常/调试classpath问题

[英]Class not found exception even if the jar is on the classpath / debugging classpath problems

I'm stumped, I try to run my simple program like this: 我很困惑,我尝试像这样运行我的简单程序:

java -cp ~/FirmwareUpdate/bin:~/PureJavaHidApi/bin/purejavahidapi.jar:~/PureJavaHidApi/lib/jna-4.0.0.jar Test

and I get 我得到

Exception in thread "main" java.lang.NoClassDefFoundError: purejavahidapi/InputReportListener

in the point in code where it first accesses that class. 在代码中首次访问该类的位置。

I've read dozens of answers here in SO and elsewhere but have not found the answer. 我已经在SO和其他地方阅读了数十个答案,但没有找到答案。

The penny finally dropped. 一分钱终于掉了。

The problem is that home directory '~' references do not work except for the first entry in the classpath, which really threw me off the track. 问题在于,主目录“〜”引用除了在类路径中的第一个条目外不起作用,这实际上使我偏离了轨道。 Because it worked on for the first entry I thought it would work for the others. 因为它适用于第一个条目,所以我认为它将适用于其他条目。

The problem comes from (and is easy to see with hindsight) that the home directory path expansion is done by the shell and to the shell the whole classpath entry looks like a single argument and hence the expansion happens only once at the beginning of the argument. 问题来自(并且很容易想到),主目录路径扩展是由外壳程序完成的,对于外壳程序,整个类路径条目看起来像一个参数,因此扩展仅在该参数的开头发生一次。

I confirmed this by writing this simple bash script: 我通过编写以下简单的bash脚本确认了这一点:

#!/bin/bash 
 for i; do 
    echo $i 
 done

saved it as 'test' and tried my command line like this: 将其保存为“测试”并尝试这样的命令行:

./test -cp ~/FirmwareUpdate/bin:~/PureJavaHidApi/bin/purejavahidapi.jar:~/PureJavaHidApi/lib/jna-4.0.0.jar Test

which outputs: 输出:

-cp
/Users/nyholku/FirmwareUpdate/bin:~/PureJavaHidApi/bin/purejavahidapi.jar:~/PureJavaHidApi/lib/jna-4.0.0.jar
Test

confirming the diagnose. 确认诊断。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM