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.

2
  • What is the result of echo $CLASSPATH? And have you tried running with the -classpath option? Commented Nov 12, 2012 at 21:32
  • 1
    @Perception i tried this command - javac -cp '.:abc.jar' test.java... this works fine but I want to specify multiple JAR files instead of one. Can you help me with that? Commented Nov 15, 2012 at 4:28

1 Answer 1

10

To set the classpath, it is in most cases better to use the the -cp or -classpath argument when calling javac and java. It gives you more flexibility to use different classpaths for different java applications.

With the -cp and -classpath arguments your classpath can contain multiple jars and multiple locations separated with a : (colon)

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

The classpath entry in the example sets the classpath to contain the current working directory (.), and the two jar files A.jar and B.jar.

If you want to use the CLASSPATH environment variable you can do

export CLASSPATH=".:/somewhere/A.jar:/elsewhere/B.jar"
javac MyClass.java
java MyClass
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.