Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a library to Java CLASSPATH in Ubuntu

Tags:

java

ubuntu

I am not sure if my question is more related about Ubuntu or Java, so pardon me!

I am trying to compile a java program but I get the following error:

package javax.comm does not exist

I have downloaded the required package comm.jar but I do not know how/where should I install/copy this file.

I read somewhere that this should be in CLASSPATH folder but I dont have this folder.

This is what I get for java -version I guess this means I have already installed Java in my system:

java version "1.6.0_24"
OpenJDK Runtime Environment (IcedTea6 1.11.4) (6b24-1.11.4-1ubuntu0.12.04.1)
OpenJDK Server VM (build 20.0-b12, mixed mode)

I also have these folders in /usr/lib/jvm/ :

default-java             java-1.7.0-openjdk-i386  java-6-openjdk-i386
java-1.6.0-openjdk       java-6-openjdk           java-7-openjdk-common
java-1.6.0-openjdk-i386  java-6-openjdk-common    java-7-openjdk-i386
like image 521
Saeid Yazdani Avatar asked Sep 27 '12 13:09

Saeid Yazdani


3 Answers

The environment variable CLASSPATH contains a colon-separated list of locations Java should search for classes. Try

export CLASSPATH=$CLASSPATH:/path/to/comm.jar
like image 135
John Watts Avatar answered Nov 18 '22 17:11

John Watts


Typically you specify the classpath when you start your java program with the switch java -cp your.jar xxxx.java

But you can also permanently add it to your java installation by copying the jar to the default-java/jre/lib/ext folder.

Finally take a look at this question: Setting multiple jars in java classpath

like image 42
Hiro2k Avatar answered Nov 18 '22 18:11

Hiro2k


If you want to compile a class named foo.bar.Baz, you must put the Baz.java file in a directory foo/bar and launch javac from foo's parent directory, ie if you list the content of the current directory you can see foo listed. Alternatively, there's the -sourcepath command line switch:

javac -sourcepath .:/home/asdf/qwerty foo.bar.Baz.java

Assuming your class is declared as follows

import foo.bar.*;
public class Baz {}

you must put this code in a /home/raf/foo/bar/Baz.java file, and changing to the directory /home/raf before invoking the compiler.

javac will output the "package foo.bar doesn't exist" error if it cannot find a foo/bar directory tree in its sourcepath. So you either change to the right directory, or use the -sourcepath switch to point to the root of the project, ie the directory containing javax/comm. Put your sources in a directory like this:

+ /home/raf/project/src
|
+-/javax
  |
  +-/comm

and invoke javac from the src directory

cd /home/raf/project/src
javac $filenames

or with the aforementioned switch

javac -sourcepath /home/raf/project/src $filenames

You need to adjust your CLASSPATH to let javac compile against existing archives.

like image 1
Raffaele Avatar answered Nov 18 '22 18:11

Raffaele