If you’re a Java programmer and you want to learn how to write apps powered by AI, you can do so without having to switch to a language like Python. Although most of the AI world has embraced Python as their language of choice, many AI libraries have been built to support Java. This is particularly important if you’re working on an enterprise-grade application with a large amount of Java legacy code, or if you’re starting a new app, already know Java, and don’t want to learn Python.
Let’s look at what tools and technologies are available to Java developers and where you can go from there.
Pro tip: Although we’re talking about Java here, Scala is a language similar to Java that targets the Java Runtime. Many Java developers have shifted to Scala, especially for Android development and Big Data systems. Some of the libraries below were built using Scala but can be used from within Java apps.
Libraries and Frameworks
Java has been a favorite language and platform for enterprise apps for decades. Because of this, the Java community has been quick to add in AI libraries and frameworks that are well-suited to enterprise applications, including machine learning and natural language processing. Here are some libraries and frameworks that you’ll want to learn; we’ll look at these in more detail in the sections below.
- Deeplearning4j: Part of the Eclipse Foundation.
- Weka
- Apache OpenNLP
- Javacv (Java Computer Vision Library): This is a Java wrapper around OpenCV
- Deep Java Library
- Smile (Statistical machine Intelligence and Learning Engine)
Adding Chats to a Java App
Many corporations that have a large infrastructure built on Java want to add chat features to their systems, particularly AI-powered chat. These chatbots can be local/internal for the people who work at the company or external (such as a customer-service module on the company’s website). There are many tools and libraries to implement chat features within existing Java apps. Some of these are products that you can install and deploy on your own servers, while others are third-party APIs.
Here are some frameworks you can explore and learn:
- Rasa: This is premium product that also has an open-source community version available. (But for enterprise grade apps you’ll probably want their premium products.) Note that although you install, deploy, and manage this system, it’s technically still API-based. You can work with it from any language including Java. (The product itself is mostly written in Python.)
- Apache OpenNLP: We’ll cover this in more detail shortly when we discuss natural language processing; however, this is a framework from Apache that you can add to your Java applications. While not offering out-of-the-box chatbot features, you can certainly build a chatbot with it. There are many guides online by searching “build chatbot with apache opennlp.”
There are also external AI chat systems that apps written in any language, including Java, can access through the use of remote APIs, along with the help of Java API libraries such as Unirest. Here are some third party API-based chat frameworks:
- DialogFlow: Built by Google (and not free), DialogFlow is intended to help you build conversational agents targeting a intuitive customer conversations.
- Amazon Lex: This uses the same technology behind Amazon Alexa, and as such can work with both text and voice.
- Azure AI Bot Service: From Microsoft, Azure AI Bot Services allows you to build your own bots similar to its Copilot products.
Machine Learning, Deep Learning, and Natural Language Processing
Machine learning is a technology where an app is fed massive amounts of data, and then the app looks for patterns in that data. Deep learning takes this to the next level, providing deep learning across neural networks. Natural Language Processing (NLP) is the technology that allows tools like ChatGPT to understand human language and create responses in human language.
This where the following libraries can help you out:
- Deeplearning4j: Written entirely in Java and released as an Apache project, DeepLearning4J allows you to run deep learning projects right inside the Java Virtual Machine. According to the documentation, Deeplearning4j works well alongside Python-based AI tools. It also integrates well with Apache Spark, which is an engine for managing large-scale data across multiple servers.
- Apache OpenNLP: This is a project within the Apache Foundation. It’s an open-source Java library designed for processing natural language text. It takes care of the hard work underneath (tokenization, sentence detection, and more–all terms you’ll want to google and explore if you want to become really knowledgeable about NLP). You can also use OpenNLP to train models. It can be installed as a library that runs locally.
Generative AI and LLMs
Generative AI and LLMs are what has effectively transformed AI into what we see today with chatbots such as ChatGPT. Generative AI allows apps to create new content, including text, images, and music, based on patterns discovered by processing a mountain of existing data (such as millions of web pages). In order to store the information, Generative AI uses models that can be trained. For text-based applications, these models are called Large Language Models.
By using generative AI and LLMs within your Java applications, you can build, for example, knowledge bases filled with the information from your company’s own documentation. You would start with a model that’s pre-trained to understand human language, and from there add your internal documentation to it so that it can be searched with human language.
One benefit to using LLMs is that the searches don’t need to be exact text searches. LLMs know, for example, that typically the word “cat” and “feline” have similar meanings, and can search for both when only one word was entered.
If you want to explore this area, take it slowly and first learn how to integrate existing, pre-trained LLMs into your application. Then learn how to use libraries such as these:
- LangChain4j: This is a Java implementation similar to a popular Python framework called LangChain. (They point out in the documentation that it’s not a direct port of LangChain, but includes influences from other similar libraries.) Using this library, you can load an existing LLM, and interact with it through your Java code. (Tip: Look at the different code samples here.)
- Llama2.java: This is a Java port of the well-known llama2.c library. While the developer intends it mostly for educational purposes, it’s a great place to learn how to load and interact with models.
- OpenAI Java Client: Although OpenAI only officially provides clients for its API for Python, TypeScript/JavaScript, and .NET languages (typically C#), the community has built a couple dozen clients for other languages. The Java one was created by Theo Kanning and includes nice documentation. If you’re interested in using the OpenAI API, you’ll want to learn how to do it with the help of this client.
AI Development Workflow in Java
In order to build AI applications in Java, or to integrate AI features into existing Java applications, you’ll need to be familiar with structured AI workflows. Depending on your needs, you may or may not need all of these:
- Model selection: If you’re including an AI model such as for generative AI, you’ll need to start with a pre-trained model. There are many available, and you can explore them at Hugging Face, which is probably the most popular repository for models.
- Model fine-tuning: Some models need to be fine-tuned. You may or may not need to add additional training to the model you choose.
- Testing: Like any software development, you need to do internal testing. For this you’ll use the standard Java testing tools. However, during testing you’ll have to also determine the minimum computing resources needed, particularly how many servers or nodes you’ll need, and how much memory each will need, and how big of a GPU they’ll need in terms of cores. This is important because it’s going to be somewhat expensive, and so you’ll want to find out the minimum and not use any more than required. You’ll also need to test out your AI to verify no ethical problems arise. Are the bots giving out dangerous advice? Are they using terms and sentences that are inappropriate? Problems like that are real and need to be caught during testing.
- Deployment: During deployment, you’ll need to continually monitor your AI tools to make sure they’re running with the appropriate hardware and not autoscaling to levels that will cost your company far more than planned.
Conclusion
This should get you started as you begin to explore AI in the context of Java development. Take it slowly—and recognize that although it can take several years to become a full expert in these technologies, you can be up and running using them in your apps in a very short time. For a start, try building a knowledge base where you load several documents into an LLM; then search it; then add a chatbot. It won’t take long to get it up and running, and you’ll be ready to further your studies from there.