Quick revision
The short answer
ONNX Runtime's Java API runs models exported to the ONNX format from PyTorch or scikit-learn, fast on CPU and GPU. Deep Java Library (DJL) gives one Java API over several engines with ready model zoos, Tribuo and Smile train and run classic machine learning models in Java, and LangChain4j and Spring AI connect to LLM providers and can run small embedding models in-process through ONNX. Java's foreign function and vector APIs help these libraries reach native speed, but we should check each library's maturity before adopting it.
What to remember
In this answer, we saw that Java services can run models without Python. ONNX Runtime runs exported models with low latency, DJL wraps several engines with model zoos, Tribuo and Smile cover classic training and prediction, and LangChain4j and Spring AI handle LLM providers and local ONNX embeddings. The Foreign Function and Vector APIs help reach native speed, and we check each library's maturity before adopting it.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
ONNX Runtime's Java API runs models exported to the ONNX format from PyTorch or scikit-learn, fast on CPU and GPU. Deep Java Library (DJL) gives one Java API over several engines with ready model zoos, Tribuo and Smile train and run classic machine learning models in Java, and LangChain4j and Spring AI connect to LLM providers and can run small embedding models in-process through ONNX. Java's foreign function and vector APIs help these libraries reach native speed, but we should check each library's maturity before adopting it.
Why this question is asked
Most models are trained in Python, but many of the services that need predictions are written in Java. Adding a Python service just to score a model brings another runtime to deploy, monitor and secure. Interviewers ask this question to see whether we know the realistic options for running models inside the JVM and can match each one to a job, instead of naming libraries at random. In this answer, we will recap what inference means, then go through ONNX Runtime with a code example, DJL, the classic ML libraries, the LLM frameworks, and the Java platform features underneath them, ending with a comparison table.
1A Quick Recap: Training Versus Inference
Training is the process of fitting a model to data, which is slow, experimental and mostly done by data scientists in Python with PyTorch, TensorFlow or scikit-learn. Inference is using the trained model to make a prediction for new input, such as scoring one payment for fraud. Inference is what a Java service usually needs, and it has different priorities: low latency, steady memory use and easy deployment.
The bridge between the two worlds is a model file format. If the data scientists can save the model in a format Java can load, the Java team does not need Python at runtime at all.
2ONNX Runtime: Running Exported Models
ONNX, the Open Neural Network Exchange format, describes a model as a graph of standard operations. PyTorch can export to it, and scikit-learn models can be converted with a tool called skl2onnx. ONNX Runtime is Microsoft's engine for executing those graphs, and it has an official Java API. It is fast on CPU and can use GPUs through execution providers such as CUDA when we use the GPU build.
Notice that the session is created once and reused; creating it loads and optimises the model, which takes far longer than one prediction. The input name, here "input", and the output shape come from the exported model, so we check them with session.getInputInfo() rather than guessing. This option is the usual first choice when data scientists can export their model.
3Deep Java Library: One API Over Several Engines
Deep Java Library, DJL, is an open source project started at Amazon. It gives Java developers one API for loading models and running predictions, while the actual work is done by an engine underneath: PyTorch, TensorFlow or ONNX Runtime among others. DJL downloads the native engine libraries for us and offers model zoos, collections of ready pretrained models for tasks such as image classification, object detection and text embeddings.
DJL's strength is convenience. Its Translator concept converts our Java objects into tensors and back, so the prediction code reads like normal Java. It suits teams that want to try several model types or use pretrained models without writing low-level tensor code. The cost is one more layer to learn and keep up to date.
4Tribuo and Smile: Classic Machine Learning in Java
Not every problem needs a neural network. Classic machine learning, such as classification with decision trees or logistic regression, regression, and clustering, covers many business tasks, and two Java libraries do it well. Tribuo, from Oracle Labs, can train and evaluate models in Java and records the provenance of each model, meaning which data and settings produced it, which helps with audits. It can also load ONNX models. Smile is a broad library of statistical and machine learning algorithms with good performance.
These libraries are the right choice when the Java team itself wants to train models on data it already has, for example clustering customers inside a batch job, without involving a Python stack. Before adopting Smile, check its licence for the version you use, because it changed between major versions.
5LangChain4j and Spring AI: LLMs and Local Embeddings
For large language models the work is usually a call to a provider such as OpenAI, Anthropic or a self-hosted model, not local inference. LangChain4j and Spring AI are the two main Java frameworks for this. They wrap chat models, embeddings, retrieval, tool calling and structured output in Java APIs that fit Spring Boot or plain Java.
Both can also run small embedding models in-process. An embedding model turns text into a vector of numbers so we can search by meaning. LangChain4j ships modules such as all-MiniLM-L6-v2 packaged with ONNX Runtime, and Spring AI has a transformers embedding model that runs ONNX models locally. That lets us embed documents without sending them to an external service.
6What the Java Platform Adds, and a Comparison
Underneath these libraries, Java's Foreign Function and Memory API, final since Java 22, lets Java call native libraries such as ONNX Runtime's C code and manage off-heap memory safely, without the older JNI boilerplate. The Vector API, still an incubator module in Java 25, lets code use the CPU's SIMD instructions to process many numbers at once. Together they help Java libraries approach native speed. Because some of these pieces are young, we check each library's release history, community size and support before betting a production service on it.
| Option | Good for | Watch out for |
|---|---|---|
| ONNX Runtime Java API | exported PyTorch or scikit-learn models, low latency | model must export cleanly to ONNX |
| Deep Java Library | several engines, pretrained model zoos | extra abstraction layer |
| Tribuo | training classic models in Java, provenance | smaller community |
| Smile | broad classic algorithms, fast | check the licence of your version |
| LangChain4j, Spring AI | LLM providers, RAG, local ONNX embeddings | APIs still change between releases |
7Follow-up questions
If the data scientists use XGBoost, can we still run the model in Java?
Yes. XGBoost models can be converted to ONNX with converter tools, and XGBoost also has its own Java package, XGBoost4J. We should compare predictions from Python and Java on a test set of real rows to be sure the conversion is exact.
Does running a model in the JVM affect garbage collection?
The model's weights and tensors mostly live in native memory managed by the engine, not on the Java heap, so they do not add much to garbage collection, but they still count towards the container's memory limit. We must close tensors and results, as the try-with-resources in the example does, or native memory leaks.
When would you still call a Python model server?
When the model cannot be exported, depends on custom Python code, needs a GPU cluster shared by many services, or changes so often that the data science team must own deployment. Then a separate model server with a clear API is the simpler choice.
8Conclusion
In this answer, we saw that Java services can run models without Python. ONNX Runtime runs exported models with low latency, DJL wraps several engines with model zoos, Tribuo and Smile cover classic training and prediction, and LangChain4j and Spring AI handle LLM providers and local ONNX embeddings. The Foreign Function and Vector APIs help reach native speed, and we check each library's maturity before adopting it.
Open with
The main option is ONNX Runtime's Java API. Data scientists export their PyTorch or scikit-learn model to ONNX, and we load it once into an OrtSession and score requests in-process, which is fast on CPU and can use a GPU.
Then, if they want more
If we want one API over several engines, Deep Java Library from Amazon wraps PyTorch, TensorFlow and ONNX Runtime and offers model zoos with pretrained models, which is convenient for experiments and standard tasks.
For classic machine learning, like classification, regression or clustering that the Java team trains itself, there are Tribuo from Oracle Labs, which also tracks model provenance, and Smile, which has a broad set of algorithms, though I would check its licence.
For LLMs, LangChain4j and Spring AI connect to the providers and handle retrieval, tools and structured output, and both can run small embedding models locally through ONNX. Underneath, the Foreign Function and Memory API and the incubating Vector API help Java libraries reach native speed.
Whatever we pick, I would check how active the library is and compare its predictions with Python on a real test set before going to production.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 22, Java 25
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring AI reference
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A Java team wants to run ML inference without Python. Name Java options for running...": https://scrivio-interview-library.netlify.app/spring-boot/llm-fundamentals/d4f3542386/