We will now download a JAR file from the internet and use it in our project to create a JAR file for our project.
Open a web browser and search for download a junit.jar
. This will take you to a link where you can download a JAR file. There are online repositories available where JAR files exist. One of the most reliable repositories can be found at http://www.java2s.com/Code/Jar/j/Downloadjunitjar.htm, where you can download any available JAR file. If you click on it, it should take you to the following page:
As seen in the preceding screenshot, you will find the junit.jar
file and the different classes that are available in the JAR file also listed. You can right-click on the save (floppy disc) symbol to save the file on your system.
Once the file is downloaded, extract it into a junit.jar
file. You can then add it to your project with the following steps:
- Create a new project on NetBeans, for example,
HelloWorld
. - Since the new project will not have the
junit.jar
file, go toProperties
by right-clicking on the project, as shown in the following screenshot:
- Go to the
Libraries
|Add JAR/Folder
option and provide the location of where thisjunit.jar
file is, as follows:
- Now the JAR file has been added to the project, we can use the
junit.jar
file in animport
statement.We can alsoimport
individual packages, as shown in the following screenshot:
- If you want to use all of the classes in
framework
, you just have to write the following code:
import junit.framework.*;
- Now, let's use the following code to print the output
Hello World
:
package helloworld; /** * * @author admin */ import junit.framework.*; public class HelloWorld { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here System.out.println("Hello World"); } }
- After running the preceding code, you should get an output similar to the following:
If you want to create a JAR file for this project, please perform the following steps:
- Go to
Run
and selectClean and Build Project (HelloWorld)
to build your project:
- Once building the
HelloWorld
project is complete, theOutput
window will sayBUILD SUCCESSFUL
, as shown in the following screenshot:
- Open the project folder, in our case
HelloWorld
, and go into thedist
folder, as follows:
This means that whenever you use any JAR files in your project, they will be stored in the lib
folder of your JAR file.