This guide explains how to set up and use Z3 Java bindings in popular Java IDEs (Eclipse, IntelliJ IDEA, and Visual Studio Code).
Before setting up Z3 in your IDE, you need to obtain the Z3 binaries:
-
Download the appropriate Z3 release for your platform from the Z3 Releases page
- For Windows:
z3-x.x.x-x64-win.zip - For Linux:
z3-x.x.x-x64-glibc-x.x.zip - For macOS:
z3-x.x.x-x64-osx-x.x.zip
- For Windows:
-
Extract the archive to a location on your system (e.g.,
C:\z3on Windows or/opt/z3on Linux/macOS) -
The extracted folder contains:
bin/com.microsoft.z3.jar- The Java API librarybin/libz3.dll(Windows) /bin/libz3.so(Linux) /bin/libz3.dylib(macOS) - The native Z3 librarybin/libz3java.dll(Windows) /bin/libz3java.so(Linux) /bin/libz3java.dylib(macOS) - The Java JNI bridge
If you need to build Z3 from source with Java bindings enabled:
python scripts/mk_make.py --java
cd build
makeThe resulting files will be in the build directory.
- Right-click on your Java project in the Package Explorer
- Select Build Path → Configure Build Path...
- In the Libraries tab, click Add External JARs...
- Navigate to the Z3
binfolder and selectcom.microsoft.z3.jar - Click Apply and Close
Eclipse needs to know where to find the native Z3 libraries (.dll, .so, or .dylib files).
Method 1: Using Eclipse Native Library Location (Recommended)
- In the Package Explorer, expand the Referenced Libraries section
- Find and expand
com.microsoft.z3.jar - Right-click on Native Library Location and select Edit...
- Click External Folder... and select the Z3
binfolder (where the native libraries are located) - Click OK
Method 2: Using VM Arguments
- Right-click on your project → Run As → Run Configurations...
- Select your Java application configuration (or create a new one)
- Go to the Arguments tab
- In the VM arguments field, add:
(Replace with your actual Z3 bin directory path)
-Djava.library.path=C:\path\to\z3\bin - Click Apply and Run
Method 3: Using System PATH (Windows)
- Add the Z3
bindirectory to your system PATH environment variable:- Open System Properties → Advanced → Environment Variables
- Under System Variables, find and edit the
Pathvariable - Add the full path to your Z3
bindirectory (e.g.,C:\z3\bin) - Click OK and restart Eclipse
Create a test Java file with the following code:
import com.microsoft.z3.*;
public class Z3Test {
public static void main(String[] args) {
System.out.println("Creating Z3 context...");
Context ctx = new Context();
System.out.println("Z3 version: " + Version.getFullVersion());
// Simple example: x > 0
IntExpr x = ctx.mkIntConst("x");
Solver solver = ctx.mkSolver();
solver.add(ctx.mkGt(x, ctx.mkInt(0)));
if (solver.check() == Status.SATISFIABLE) {
System.out.println("SAT");
System.out.println("Model: " + solver.getModel());
}
ctx.close();
System.out.println("Success!");
}
}Run the program. If you see the Z3 version and "Success!" printed, your setup is working correctly.
- Open your project in IntelliJ IDEA
- Go to File → Project Structure (or press
Ctrl+Alt+Shift+S) - Select Modules → Dependencies
- Click the + button and select JARs or directories...
- Navigate to the Z3
binfolder and selectcom.microsoft.z3.jar - Click OK and Apply
Method 1: Using Run Configuration (Recommended)
- Go to Run → Edit Configurations...
- Select your application configuration (or create a new one)
- In the VM options field, add:
(Replace with your actual Z3 bin directory path)
-Djava.library.path=/path/to/z3/bin - Click OK
Method 2: Using Environment Variable (Windows)
Add the Z3 bin directory to your system PATH as described in the Eclipse section above, then restart IntelliJ IDEA.
Use the same test code from the Eclipse section to verify your setup.
- Open Visual Studio Code
- Install the Extension Pack for Java from the Extensions marketplace
Create or edit .vscode/settings.json in your project root:
{
"java.project.referencedLibraries": [
"path/to/z3/bin/com.microsoft.z3.jar"
]
}Create or edit .vscode/launch.json in your project root:
{
"version": "0.2.0",
"configurations": [
{
"type": "java",
"name": "Launch with Z3",
"request": "launch",
"mainClass": "YourMainClass",
"vmArgs": "-Djava.library.path=/path/to/z3/bin"
}
]
}Replace YourMainClass with your actual main class name and adjust the path to your Z3 bin directory.
Use the same test code to verify your setup.
If you prefer to build and run from the command line:
# Windows
javac -cp "C:\path\to\z3\bin\com.microsoft.z3.jar;." YourProgram.java
# Linux/macOS
javac -cp "/path/to/z3/bin/com.microsoft.z3.jar:." YourProgram.java# Windows
java -cp "C:\path\to\z3\bin\com.microsoft.z3.jar;." -Djava.library.path=C:\path\to\z3\bin YourProgram
# Linux
LD_LIBRARY_PATH=/path/to/z3/bin java -cp "/path/to/z3/bin/com.microsoft.z3.jar:." YourProgram
# macOS
DYLD_LIBRARY_PATH=/path/to/z3/bin java -cp "/path/to/z3/bin/com.microsoft.z3.jar:." YourProgramProblem: Java cannot find the Z3 classes.
Solution:
- Verify that
com.microsoft.z3.jaris in your project's classpath - In Eclipse: Check Project Properties → Java Build Path → Libraries
- In IntelliJ: Check File → Project Structure → Modules → Dependencies
- Ensure you're not just copying the JAR to your project's bin folder - it must be explicitly added to the classpath
Problem: Java can find the Z3 classes but cannot load the native libraries.
Solution:
- Verify that
libz3.dll/libz3.so/libz3.dylibandlibz3java.dll/libz3java.so/libz3java.dylibare in a directory accessible to Java - Add the Z3
bindirectory to:- The
java.library.pathVM argument, or - The system PATH environment variable (Windows), or
- The LD_LIBRARY_PATH (Linux) / DYLD_LIBRARY_PATH (macOS) environment variable
- The
Problem: Z3 fails to initialize properly.
Solution:
- Ensure all Z3 files (JAR and native libraries) are from the same version
- Check that you're using a compatible Java version (Java 8 or later)
- Verify that the native libraries match your system architecture (32-bit vs 64-bit)
Windows:
- Use semicolons (
;) as classpath separators - Native libraries:
libz3.dllandlibz3java.dll - Set PATH or use
-Djava.library.path
Linux:
- Use colons (
:) as classpath separators - Native libraries:
libz3.soandlibz3java.so - Set LD_LIBRARY_PATH or use
-Djava.library.path
macOS:
- Use colons (
:) as classpath separators - Native libraries:
libz3.dylibandlibz3java.dylib - Set DYLD_LIBRARY_PATH or use
-Djava.library.path
For Maven or Gradle projects, you can use system-scoped dependencies:
<dependency>
<groupId>com.microsoft</groupId>
<artifactId>z3</artifactId>
<version>x.x.x</version> <!-- Replace with your Z3 version -->
<scope>system</scope>
<systemPath>${project.basedir}/lib/com.microsoft.z3.jar</systemPath>
</dependency>Place the Z3 JAR in your project's lib directory and configure the native library path as described above.
dependencies {
implementation files('lib/com.microsoft.z3.jar')
}If you need to build Z3 from source with Java bindings:
# Clone the repository
git clone https://github.com/Z3Prover/z3.git
cd z3
# Configure with Java support
python scripts/mk_make.py --java
# Build
cd build
make
# The Java bindings will be in the build directory
# - com.microsoft.z3.jar
# - libz3java.so / libz3java.dll / libz3java.dylib
# - libz3.so / libz3.dll / libz3.dylibFor more details on building Z3, see the main README.md.