whenever I want to see a behavior of java library/functions, I always create a file with .java extension and add a demo code to it. I will try to compile and run the file from the terminal using javac(to compile) and java(to run) it.
lately, I wanted to understand the behavior of method "random" in "org.apache.commons.lang.RandomStringUtils". but when i try to compile it, i am getting "package org.apache.commons.lang does not exist" error.
I understand I didn't add the apache libraries anywhere to the system path etc, and the compiler is unable to find it. I have found some info related to maven/ or other ide's and java packages and using classpath arguments but i am not sure how to run it in my scenerio.
I have downloaded the apache commons binaries from here.
I was wondering if there is a way to provide this path info to "javac" command as an argument while compiling. or if there is any other way to compile it by making some temporary changes only.
sorry if I am using any keywords/terminology wrong. let me know if you need any further info.
myjava.java
import java.util.*;
import java.io.*;
import java.nio.charset.StandardCharsets;
import org.apache.commons.lang.RandomStringUtils;
class myjava
{
public static void main(String args[])
{
ArrayList<String> mlist = new ArrayList<String>();
int x =10;
while(x>0){
x--;
mlist.add(RandomStringUtils.random(6));
}
System.out.println("printing list in Java");
System.out.println(mlist);
}
}
and when i try to compile it from terminal, i am getting following error.
sabodda@sabodda-mac java-python % javac myjava.java
myjava.java:5: error: package org.apache.commons.lang does not exist
import org.apache.commons.lang.RandomStringUtils;
^
myjava.java:37: error: cannot find symbol
mlist.add(RandomStringUtils.random(6));
^
symbol: variable RandomStringUtils
location: class myjava
2 errors
let me know , if you need further info. Thanks.
i am using mac with java jdk installed and path to jdk is set.
EDIT:Answering my own question. i have downloaded the jar(commons-lang3-3.12.0.jar) (It is extracted from binary downloaded) and copied it to directory of source file(myjava.java).
To compile:
javac -cp .:commons-lang3-3.12.0.jar myjava.java
To execute:
java -cp .:commons-lang3-3.12.0.jar myjava