0

I am using Amazon Coretto, a JDK, to write and run Java code. I am trying to use a class from Apache Commons Lang with simple import statements like:

import org.apache.commons.lang3.*

However, everything I have looked up online will only describe how to do this with an IDE like Eclipse. How do I download the org.apache.commons.lang3 class and use its subclasses with only a JDK?

andrewJames
  • 19,570
  • 8
  • 19
  • 51

1 Answers1

3

The best way to do this is to install Maven, create a pom.xml file and add commons-lang as a dependency.

The minimal way is to download from https://repo1.maven.org/maven2/org/apache/commons/commons-lang3/3.11/commons-lang3-3.11.jar and add the jar to your class path when you compile: javac -cp commons-lang3-3.11.jar Foo.java and when you run: java -cp commons-lang3-3.11.jar:. Foo

The files you see in your working directory should be Foo.java and commons-lang3-3.11.jar. After you have runjavac you will also see Foo.class.

tgdavies
  • 10,307
  • 4
  • 35
  • 40
  • Thank you. I tried the second option, and it gave me this error: `error: package org.apache.commons.lang3 does not exist`. How do I fix this? – jaqdiddy Dec 20 '20 at 01:48
  • You probably don't have the jar where you are telling javac that it is. – tgdavies Dec 20 '20 at 02:49
  • Thank you. Just a head up. There is a typo in the statement running java. Colon to Semicolon. `java -cp commons-lang3-3.11.jar;. Foo` – Chris Moon Sep 16 '21 at 02:13
  • That depends on the platform `:` on Linux and OS X, `;` on Windows. – tgdavies Sep 16 '21 at 02:20