Adding a library to Java CLASSPATH in Ubuntu

I'm not sure my question is more related to Ubuntu or Java, so forgive me!

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

package javax.comm does not exist 

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

I read somewhere that this should be in the CLASSPATH folder, but I don't have this folder.

This is what I get for java -version I assume it means that I have already installed Java on 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 
+8
java ubuntu
source share
4 answers

You usually specify the class path when starting your Java program using the java -cp your.jar xxxx.java

But you can also add it to your java installation for a long time by copying the jar to the default folder java / jre / lib / ext.

Finally, take a look at this question: Installing multiple jars in the java classpath path

+12
source share

The CLASSPATH environment variable contains a list of locations separated by colons. Java should look for classes. Try

 export CLASSPATH=$CLASSPATH:/path/to/comm.jar 
+7
source share

You can try to do this as shown below:

  • javac -cp comm.jar XXXXX.java or
  • export CLASSPATH=comm.jar:$CLASSPATH
+1
source share

If you want to compile a class called foo.bar.Baz , you must put the Baz.java file in the foo/bar directory and run javac from the foo parent directory, that is, if you list the contents of the current one, you can see foo . In addition, there is a command line -sourcepath :

 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 the /home/raf/foo/bar/Baz.java file and change to the /home/raf before calling the compiler.

javac will throw a “package foo.bar does not exist” error if it cannot find the foo/bar directory tree in its path to the source file. Thus, you either go to the right directory or use the -sourcepath switch to point to the root of the project, i.e. the directory containing javax/comm . Put your sources in the directory as follows:

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

and call javac from src directory

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

or with the switch above

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

You need to configure CLASSPATH so that javac compiles with existing archives.

+1
source share

All Articles