Java – add jar classpath in Ubuntu
This may be a common problem, but I can't add Classpaths to jar files in Ubuntu I have given all the details I know below:
Java is located in: the O / P of the 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
The PS: downloads folder is located directly under 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've added Classpaths to bashrc and etc / environment... But I'm trying to run S3 sample. Java's awssdk Java still has errors
When I compile the java file, I receive the following error:
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 know that the jar file was not added to the classpath, so I didn't receive an error I've also tried javac with the classpath option – but it doesn't work:(
PS: set JAVA HOME correctly because other Java programs work normally
Solution
To set the classpath, in most cases, it is better to use the - CP or - classpath parameter when calling javac and Java It gives you more flexibility to use different Classpaths for different Java applications
Using the - CP and - classpath parameters, the classpath can contain multiple jars and multiple 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 entry in the example sets the classpath to contain the current working directory () And two jar files, a.jar and b.jar
You can do this if you want to use the classpath environment variable
export CLASSPATH=".:/somewhere/A.jar:/elsewhere/B.jar" javac MyClass.java java MyClass