Installation
Before installing P4 API for Java, ensure you have met all the criteria of the prerequisites
Prerequisites
-
Check your versions of Java and OS meet the requirements for P4 API for Java.
-
P4 API for Java requires Java 11 and above. Find which version of Java you are using by entering the following into the command line:
java -version
-
-
You will also need the compiler Javac, this is usually included in your Java installation package. Enter the following into the command line to check if you have Javac:
javac -version -
Download P4 API for Java from the Perforce website.
-
Download the following
.jarfiles:File Description Download commons-lang3-3.18.0.jarString and utility support
commons-io-2.20.0.jarStream helpers (
BOMInputStreamlives here)commons-codec-1.15.jarEncoding/crypto utilities (Base64, hashing, etc.)
jzlib-1.1.3.jarCompression support
Install P4 API for Java
-
Extract the downloaded P4 API for Java ZIP file.
-
Create a folder in a known location, such as
C:\p4java -
Move the extracted P4 API for Java files into
C:\p4java. -
Move the following .jar files into
C:\p4java:-
commons-lang3-3.18.0.jar -
commons-io-2.20.0.jar -
commons-codec-1.15.jar -
jzlib-1.1.3.jar
-
-
Create an installation script file called
P4Test.Javawhich:-
Connects to a P4 Server
-
Authenticates the connection.
-
Selects a workspace
-
List the depot files
-
Syncs files to a local workspace
Below is an example of this code. Update the code with your credentials:
Copyimport com.perforce.p4java.client.IClient;
import com.perforce.p4java.core.file.IFileSpec;
import com.perforce.p4java.core.file.FileSpecBuilder;
import com.perforce.p4java.exception.P4JavaException;
import com.perforce.p4java.server.IOptionsServer;
import com.perforce.p4java.server.ServerFactory;
import java.util.List;
public class P4Test {
public static void main(String[] args) {
String serverUri = "p4java://perforce:1666"; // adjust if needed
String user = "user.name"; // your P4 user
String password = "p@ssw0rd3000"; // set your password if needed
String workspace = "workspace_main"; // your client workspace
try {
// Connect to the server
IOptionsServer server = ServerFactory.getOptionsServer(serverUri, null);
server.connect();
System.out.println("Connected to: " + serverUri);
// Login if needed
server.setUserName(user);
if (password != null && !password.isEmpty()) {
server.login(password);
System.out.println("Logged in as: " + user);
}
// Set workspace (client)
IClient client = server.getClient(workspace);
if (client == null) {
System.out.println("Workspace not found: " + workspace);
return;
}
server.setCurrentClient(client);
System.out.println("Using workspace: " + workspace);
// ---- Example 1: List depot files ----
System.out.println("\nListing files in //depot/... :");
List<IFileSpec> fileList = server.getDepotFiles(
FileSpecBuilder.makeFileSpecList("//depot/..."),
false
);
for (IFileSpec file : fileList) {
if (file != null && file.getDepotPathString() != null) {
System.out.println(" " + file.getDepotPathString());
}
}
// ---- Example 2: Sync workspace ----
System.out.println("\nSyncing workspace...");
List<IFileSpec> syncFiles = client.sync(
FileSpecBuilder.makeFileSpecList("//depot/..."),
null
);
for (IFileSpec file : syncFiles) {
if (file != null && file.getDepotPathString() != null) {
System.out.println(" Synced: " + file.getDepotPathString());
}
}
System.out.println("\nDone!");
server.disconnect();
} catch (P4JavaException e) {
System.err.println("Perforce error: " + e.getMessage());
e.printStackTrace();
} catch (Exception e) {
System.err.println("Unexpected error: " + e.getMessage());
e.printStackTrace();
}
}
} -
-
Create a
.bat(Windows) or.sh(macOS) file which uses a custom classpath to include required libraries, compiles a Java program (P4Test.java) and runs the compiled program (P4Test.class).See the following examples for a
.bator.shfile:runp4.bat
Copy@echo off
echo Compiling P4Test.java...
set CP=.;C:\p4java\p4java-2025.1.2785811.jar;C:\p4java\commons-lang3-3.18.0.jar;C:\p4java\jzlib-1.1.3.jar;C:\p4java\commons-io-2.20.0.jar
javac -cp "%CP%" P4Test.java
echo Running P4Test...
java -cp "%CP%" P4Test
echo.
pauserunp4.sh
Copy#!/bin/bash
echo "Compiling P4Test.java..."
# Define classpath (use : instead of ; on Unix systems)
CP=".:/Users/<USERNAME>/p4java/p4java-2025.1.2785811.jar:/Users/<USERNAME>/p4java/commons-lang3-3.18.0.jar:/Users/<USERNAME>/p4java/jzlib-1.1.3.jar:/Users/<USERNAME>/p4java/commons-io-2.20.0.jar"
# Compile the Java file
javac -cp "$CP" P4Test.java
# Check if compilation succeeded
if [ $? -ne 0 ]; then
echo "Compilation failed!"
exit 1
fi
echo "Running P4Test..."
java -cp "$CP" P4Test
echo
read -p "Press Enter to continue..."Replace
<USERNAME>with your macOS username. -
Run the
runp4.batorrunp4.shin the command line. You should see a file list output into the terminal.