Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding JAR class path in UBUNTU

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.

like image 712
user1736333 Avatar asked Nov 12 '12 17:11

user1736333


1 Answers

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
like image 121
cyon Avatar answered Oct 19 '22 22:10

cyon