简体   繁体   中英

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.

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:

#!/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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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