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 .jar files:

    File Description Download

    commons-lang3-3.18.0.jar

    String and utility support

    Link

    commons-io-2.20.0.jar

    Stream helpers (BOMInputStream lives here)

    Link

    commons-codec-1.15.jar

    Encoding/crypto utilities (Base64, hashing, etc.)

    Link

    jzlib-1.1.3.jar

    Compression support

    Link

Install P4 API for Java

  1. Extract the downloaded P4 API for Java ZIP file.

  2. Create a folder in a known location, such as C:\p4java

  3. Move the extracted P4 API for Java files into C:\p4java.

  4. 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

  5. Create an installation script file called P4Test.Java which:

    • 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:

    Copy
    import 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();
            }
        }
    }
  6. 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 .bat or .sh file:

    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.
    pause

    runp4.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.

  7. Run the runp4.bat or runp4.sh in the command line. You should see a file list output into the terminal.