Is it possible to add dependency class binaries to "javac" as a argument.(compiling a java file directly from terminal)
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
It is not possible to explicitly add binaries (i.e. ".class" files) as dependencies by passing them as javac
command line arguments.
The javac
command (and other Java toolchain commands) use a classpath to find dependency classes. This is a sequence of classpath entries which are either directories or JAR or ZIP files. So, to add a single dependency class, you would either need to put it into a directory on the classpath, or add it to a JAR or ZIP file on the classpath, or add a new entry (as above) to the classpath.
In your case, you need to add the JAR file for the Apache Commons Lang library to the classpath. If you are compiling like this:
$ javac myjava.java
change it to this:
$ javac -classpath .:<path/to/dependency.jar> myjava.java
For more information, you should read about how the classpath works and how it is specified. See
-
Setting the Class Path (Windows version).
-
Setting the Class Path (UNIX version). This applies to MacOS too.
Finally, note that explicitly downloading dependencies and adding them your build and runtime classpath is clunky and old-fashioned. Build tools like Maven and Gradle take care of it for you. If you plan to implement anything non-trivial in Java, you should learn to use at least one of these build tools.