A jar file contains Java external dependencies such as classes, resources, and metadata. It is often used to package libraries and dependencies that your Java application requires to simplify the distribution and organization of Java code.
In this article, we will see the process of including jar files and compiling Java files using the command prompt.
Prepare the jar file:
As a first step, download the jar files that your Java program depends on. If you want to create your own jar file, use the following command to do that:
Commnad:
jar cf jar-file input-file(s)
Example:
jar cvf TicTacToe.jar TicTacToe.class
After executing the above command, a jar file will be created.
Prepare the java file:
Create a Java source code file with the necessary code to utilize this jar file and Save it with a .java
extension. For example, MyProgram.java.
Compile the java file with jar file
To compile, open the command prompt and navigate to the directory containing your Java file. Use the javac
command to compile your code along with the jar files.
Following is the command syntax:
Command:
javac -cp path/to/jar/file1.jar;path/to/jar/file2.jar MyProgram.java
Example:
javac -cp myLibrary.jar MyProgram.java
Run the program
After successfully compiling your code, you can run the program using the java
command shown below.
Command:
java -cp .;path/to/jar/file1.jar;path/to/jar/file2.jar MyProgram
Example:
java -cp .;myLibrary.jar MyProgram
References:
https://docs.oracle.com/javase/tutorial/deployment/jar/basicsindex.html