DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • AI Agents in Java: Architecting Intelligent Health Data Systems
  • Building an Image Classification Pipeline With Apache Camel and Deep Java Library (DJL)
  • Improving Java Application Reliability with Dynatrace AI Engine
  • How AI Is Rewriting Full-Stack Java Systems: Practical Patterns with Spring Boot, Kafka and WebSockets

Trending

  • The Agent Protocol Stack: MCP vs. A2A vs. AG-UI
  • From APIs to Event-Driven Systems: Modern Java Backend Design
  • Content Lakes: Harness Unstructured Data for Enterprise AI Readiness
  • Building a Production-Ready AI Agent in 2026: Beyond the Hello World Demo
  1. DZone
  2. Data Engineering
  3. AI/ML
  4. Building AI Applications With Java and Gradle

Building AI Applications With Java and Gradle

Explore AI development with Java and Gradle using popular libraries like Deeplearning4j, Weka, and Encog through practical code examples.

By 
Arun Pandey user avatar
Arun Pandey
DZone Core CORE ·
Sep. 27, 23 · Tutorial
Likes (6)
Comment
Save
Tweet
Share
12.9K Views

Join the DZone community and get the full member experience.

Join For Free

Artificial intelligence (AI) is transforming various industries and changing the way businesses operate. Although Python is often regarded as the go-to language for AI development, Java provides robust libraries and frameworks that make it an equally strong contender for creating AI-based applications. In this article, we explore using Java and Gradle for AI development by discussing popular libraries, providing code examples, and demonstrating end-to-end working examples.

Java Libraries for AI Development

Java offers several powerful libraries and frameworks for building AI applications, including:

  • Deeplearning4j (DL4J) - A deep learning library for Java that provides a platform for building, training, and deploying neural networks, DL4J supports various neural network architectures and offers GPU acceleration for faster computations.
  • Weka - A collection of machine learning algorithms for data mining tasks, Weka offers tools for data pre-processing, classification, regression, clustering, and visualization.
  • Encog - A machine learning framework supporting various advanced algorithms, including neural networks, support vector machines, genetic programming, and Bayesian networks

Setting up Dependencies With Gradle

To begin AI development in Java using Gradle, set up the required dependencies in your project by adding the following to your build.gradle file:

Groovy
 
dependencies {

    implementation 'org.deeplearning4j:deeplearning4j-core:1.0.0-M1.1'
    implementation 'nz.ac.waikato.cms.weka:weka-stable:3.8.5'
    implementation 'org.encog:encog-core:3.4'
}


Code Examples

Building a Simple Neural Network With DL4J

This example demonstrates creating a basic neural network using the Deeplearning4j (DL4J) library. The code sets up a two-layer neural network architecture consisting of a DenseLayer with 4 input neurons and 10 output neurons, using the ReLU activation function, and an OutputLayer with 10 input neurons and 3 output neurons, using the Softmax activation function and Negative Log Likelihood as the loss function. The model is then initialized and can be further trained on data and used for predictions.

Java
 
import org.deeplearning4j.nn.api.OptimizationAlgorithm;
import org.deeplearning4j.nn.conf.MultiLayerConfiguration;
import org.deeplearning4j.nn.conf.NeuralNetConfiguration;
import org.deeplearning4j.nn.conf.layers.DenseLayer;
import org.deeplearning4j.nn.conf.layers.OutputLayer;
import org.deeplearning4j.nn.multilayer.MultiLayerNetwork;
import org.deeplearning4j.nn.weights.WeightInit;
import org.nd4j.linalg.activations.Activation;
import org.nd4j.linalg.learning.config.Sgd;
import org.nd4j.linalg.lossfunctions.LossFunctions;

public class SimpleNeuralNetwork {

    public static void main(String[] args) {

        MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
                .seed(123)
                .optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT)
                .updater(new Sgd(0.01))
                .list()
                .layer(0, new DenseLayer.Builder().nIn(4).nOut(10)
                        .weightInit(WeightInit.XAVIER)
                        .activation(Activation.RELU)
                        .build())
                .layer(1, new OutputLayer.Builder(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD)
                        .nIn(10).nOut(3)
                        .weightInit(WeightInit.XAVIER)
                        .activation(Activation.SOFTMAX)
                        .build())
                .pretrain(false).backprop(true)
                .build();

        MultiLayerNetwork model = new MultiLayerNetwork(conf);

        model.init();
    }
}


Classification Using Weka

This example shows how to use the Weka library for classification on the Iris dataset. The code loads the dataset from an ARFF file, sets the class attribute (the attribute we want to predict) to be the last attribute in the dataset, builds a Naive Bayes classifier using the loaded data, and classifies a new instance.

Java
 
import weka.classifiers.bayes.NaiveBayes;
import weka.core.Instance;
import weka.core.Instances;
import weka.core.converters.ConverterUtils.DataSource;

public class WekaClassification {

    public static void main(String[] args) throws Exception {

        DataSource source = new DataSource("data/iris.arff");

        Instances data = source.getDataSet();
        data.setClassIndex(data.numAttributes() - 1);

        NaiveBayes nb = new NaiveBayes();
        nb.buildClassifier(data);

        Instance newInstance = data.instance(0);
        double result = nb.classifyInstance(newInstance);
        System.out.println("Predicted class: " + data.classAttribute().value((int) result));
    }
}


Conclusion

Java, with its rich ecosystem of libraries and frameworks for AI development, is a viable choice for building AI-based applications. By leveraging popular libraries like Deeplearning4j, Weka, and Encog, and using Gradle as the build tool, developers can create powerful AI solutions using the familiar Java programming language.

The provided code examples demonstrate the ease of setting up and configuring AI applications using Java and Gradle. The DL4J example shows how to create a basic deep learning model that can be applied to tasks such as image recognition or natural language processing. The Weka example demonstrates how to use Java and the Weka library for machine learning tasks, specifically classification, which can be valuable for implementing machine learning solutions in Java applications, such as predicting customer churn or classifying emails as spam or not spam.

Happy Learning!!

AI Activation function Gradle Java (programming language) neural network

Opinions expressed by DZone contributors are their own.

Related

  • AI Agents in Java: Architecting Intelligent Health Data Systems
  • Building an Image Classification Pipeline With Apache Camel and Deep Java Library (DJL)
  • Improving Java Application Reliability with Dynatrace AI Engine
  • How AI Is Rewriting Full-Stack Java Systems: Practical Patterns with Spring Boot, Kafka and WebSockets

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook