Book Image

Hands-On Artificial Intelligence with Java for Beginners

By : Nisheeth Joshi
Book Image

Hands-On Artificial Intelligence with Java for Beginners

By: Nisheeth Joshi

Overview of this book

Artificial intelligence (AI) is increasingly in demand as well as relevant in the modern world, where everything is driven by technology and data. AI can be used for automating systems or processes to carry out complex tasks and functions in order to achieve optimal performance and productivity. Hands-On Artificial Intelligence with Java for Beginners begins by introducing you to AI concepts and algorithms. You will learn about various Java-based libraries and frameworks that can be used in implementing AI to build smart applications. In addition to this, the book teaches you how to implement easy to complex AI tasks, such as genetic programming, heuristic searches, reinforcement learning, neural networks, and segmentation, all with a practical approach. By the end of this book, you will not only have a solid grasp of AI concepts, but you'll also be able to build your own smart applications for multiple domains.
Table of Contents (14 chapters)
Title Page
Copyright and Credits
Packt Upsell
Contributors
Preface
Index

Importing Java libraries and exporting code in projects as a JAR file


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:

  1. Create a new project on NetBeans, for example, HelloWorld.
  2. 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:

 

  1. Go to the Libraries | Add JAR/Folder option and provide the location of where this junit.jar file is, as follows:
  1. Once done, click on Open and it will be added to your project:
  1. Now the JAR file has been added to the project, we can use the junit.jarfile in animportstatement.We can alsoimportindividual packages, as shown in the following screenshot:
  1. If you want to use all of the classes in framework, you just have to write the following code:
import junit.framework.*;
  1. 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"); 
 } 
}
  1. 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:

  1. Go to Run and select Clean and Build Project (HelloWorld) to build your project:
  1. Once building the HelloWorld project is complete, the Output window will say BUILD SUCCESSFUL, as shown in the following screenshot:
  1. 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:
  1. Open the project folder, in our case HelloWorld, and go into the dist folder, as follows:
  1. 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.