qed
qed

Reputation: 23134

How to use the apache commons java library on Ubuntu?

I am a Java beginner and trying to figure out how to use the apache commons lib.

Here is a source file Randstr.java:

import org.apache.commons.lang3.RandomStringUtils;

class Randstr {
    public static void main(String[] args) {
        String s = RandomStringUtils.random(12);
        System.out.println(s);
    }
}

I have the commons-lang3-3.1.jar file in /usr/share/java/ and have created a symlink in the current dir. Then I compiled it like this: javac -cp commons-lang3-3.1.jar Randstr.java, the complilation was fine, but when I execute java Randstr, I got the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang3/RandomStringUtils
        at Randstr.main(Randstr.java:5)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang3.RandomStringUtils
        at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        ... 1 more

And if I don't specify the jar file in the classpath, it will not even compile:

javac -cp . Randstr.java

# Randstr.java:1: error: package org.apache.commons.lang3 does not exist
# import org.apache.commons.lang3.RandomStringUtils;
#                                ^
# Randstr.java:5: error: cannot find symbol
#         String s = RandomStringUtils.random(12);
#                    ^
#   symbol:   variable RandomStringUtils
#   location: class Randstr
# 2 errors

javac -cp /usr/share/java/  Randstr.java

# Randstr.java:1: error: package org.apache.commons.lang3 does not exist
# import org.apache.commons.lang3.RandomStringUtils;
#                                ^
# Randstr.java:5: error: cannot find symbol
#         String s = RandomStringUtils.random(12);
#                    ^
#   symbol:   variable RandomStringUtils
#   location: class Randstr
# 2 errors

From reading other questions on stackoverflow, I see this can be solved by using an IDE, but I prefer a simple editor at the moment.

Upvotes: 4

Views: 7418

Answers (3)

Chase
Chase

Reputation: 3183

Edit your profile file. vim ~/.bashrc

In your profile file add the following line:

export CLASSPATH=/usr/share/java/commons-lang3-3.1.jar:.

Log out and back in. Or source your profile file in the windows you have open. You can always add your classpath to every java and javac command you invoke but that becomes a pain. With the CLASSPATH environmental variable you don't have to add it on the command line any more. Note that if you are using an IDE such as NetBeans or Eclipse you still might have to add the library to your project's libraries within the IDE.

Upvotes: 1

vanje
vanje

Reputation: 10383

If you can compile it with

javac -cp commons-lang3-3.1.jar Randstr.java

then you can run it with

java -cp commons-lang3-3.1.jar:. Randstr

The JAR file has to be in the classpath.

Upvotes: 3

millimoose
millimoose

Reputation: 39990

Clearly the contents of /usr/share/java/ don't automatically get added to the classpath - it's just a common location where APT packages put Java libraries. It's up the developer to reference them correctly.

JARs in the ext/ subdirectory of a Java installation do get added to the classpath automatically. However, do not put your own JARs in there. It's a terrible practice because it doesn't match how Java apps are deployed "in the real world".

The correct way is using the -cp parameter explicitly when compiling AND running your app. Java doesn't compile library code into your .class files, a .class file only refers to names of other classes which are then loaded as-needed from the class path when your app runs. The -cp parameter takes only .jar files, or directories with .class files in them. You can also use wildcards in the value of that parameter. For more information on wrangling the class path, check the tool documentation on setting the class path.

You using a build tool that sets it for you automatically, like an IDE or Maven or another build system with dependency management. (Gradle or Ant+Ivy.) If you're writing a Java app that uses third party libraries, I very strongly suggest you learn and use one of those. (Also, most IDEs can work with Maven's configuration files letting you use the same build settings in a team with people using mixed or no IDEs.) Generally if you're invoking a compiler directly you're not doing it right.

Upvotes: 0

Related Questions