简体   繁体   中英

How to import .jar file

I've been attempting to import a class contained with in a .jar file. My my_package.jar file resides in /Users/user/Library/Java/Extensions and my ~/.bashrc contains the following line export CLASSPATH = "$CLASSPATH:/Users/user/Library/Java/Extensions . At the top of my program is a line that reads import my_package.MyClass; . Whenever I try to compile the program using javac MyProgram.java I get the package my_package does not exist . I've done a bit of searching around online and found several possible fixes. One of which involved moving the .jar file to the current directory and invoking javac found here as follows.

javac -cp '.:my_package.jar' MyClass.java

This yields the same error.

I have also tried changing the CLASSPATH to /Users/David_Johnson/Library/Java/Extensions/ and /Users/David_Johnson/Library/Java/Extensions/* . They both yield the same error.

It appears as though I am missing something quite trivial. Any help in catching my mistake would be greatly appreciated.

I'm on Max OSX 10.8.5 using javac 1.7.0_07

CLASSPATH must explicitly list each and every JAR file. Wildcards or directories are not supported.

You can use bash to generate the list though.

for jar in /Users/where/ever/they/are/*.jar
do
    CLASSPATH="$CLASSPATH:$jar"
done
export CLASSPATH

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