6

This is might be a common question but I am not able to add class path for a JAR file in UBUNTU. I have given below all the details I know:

java is located here: the o/p of which java command is - /usr/bin/java

sudo vim /etc/bash.bashrc  
export CLASSPATH=$CLASSPATH:/downloads/aws-java-sdk-1.3.24/lib/aws-java-sdk-1.3.24.jar

ps: downloads folder is directly under the root

sudo vim /etc/environment
CLASSPATH="/usr/lib/jvm/jdk1.7.0/lib: /downloads/aws-java-sdk-1.3.24/lib/aws-java-sdk-1.3.24.jar:"

As you can see, I have added the class path in bashrc and etc/environment... but still I am getting an error while trying to run the S3Sample.java which comes with awssdk for java.

when I compile the java file, I get the following errors:

ubuntu@domU-12-31-39-03-31-91:/downloads/aws-java-sdk-1.3.24/samples/AmazonS3$ javac S3Sample.java

S3Sample.java:25: error: package com.amazonaws does not exist
import com.amazonaws.AmazonClientException;

Now, I clearly understand that the JAR file is not added to the class path and so I am not getting the error. I've also tried javac with the class path option - but it does not work :(

PS: JAVA home is set correctly as other java programs work properly.

4

1 回答 1

9

要设置类路径,在大多数情况下,最好在调用and时使用-cpor参数。它使您可以更灵活地为不同的 Java 应用程序使用不同的类路径。-classpathjavacjava

使用-cpand-classpath参数,您的类路径可以包含多个 jar 和多个位置,并用:(冒号)分隔

javac -cp ".:/somewhere/A.jar:/elsewhere/B.jar" MyClass.java
java -cp ".:/somewhere/A.jar:/elsewhere/B.jar" MyClass

示例中的类路径条目将类路径设置为包含当前工作目录 ( .) 以及两个 jar 文件A.jarB.jar.

如果你想使用CLASSPATH环境变量,你可以这样做

export CLASSPATH=".:/somewhere/A.jar:/elsewhere/B.jar"
javac MyClass.java
java MyClass
于 2013-08-05T22:11:25.163 回答