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 to Properties 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 this junit.jar file is, as follows:
- Once done, click on Open and it will be added to your project:
- Now the JAR file has been added to the project, we can use the junit.jar file in an import statement. We can also import 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 select Clean and Build Project (HelloWorld) to build your project:
- Once building the HelloWorld project is complete, the Output window will say BUILD SUCCESSFUL, as shown in the following screenshot:
- To check if the JAR file been created or not, go to Windows Explorer and navigate to your project's location, which you received from the previous output:
- Open the project folder, in our case HelloWorld, and go into the dist folder, as follows:
- In the dist folder, you will find the JAR file (HelloWorld.jar), which you can use and where there will be a lib folder. This will contain the junit.jar file that is being used by the HelloWorld.jar file:
This means that whenever you use any JAR files in your project, they will be stored in the lib folder of your JAR file.