Adding JAR class path to UBUNTU

This may be a general question, but I cannot add the path to the JAR file in UBUNTU. I have given below all the details that I know:

java is here: command o / p which java - /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: the download folder is located 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 added the class path in bashrc, etc. / environment ... but still I get an error when I try to run S3Sample.java , which comes with awssdk for java.

when I compile a 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 therefore I am not getting an error. I also tried javac with the class path parameter, but it does not work :(

PS: JAVA home is configured correctly, as other java programs work correctly.

+6
source share
1 answer

To set the class path, in most cases it is better to use the -cp or -classpath when calling javac and java . This gives you great flexibility when using different class classes for different java applications.

With the arguments -cp and -classpath your class path may contain several jars and several locations separated by : (colon)

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

The classpath element in the example sets the class path containing the current working directory ( . ), And two jar 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 
+7
source

All Articles