Password Generator for Java

Free online password generator with Java code examples

Working with password generator in Java? Our free online password generator helps Java developers format, validate, and process data instantly. Below you will find Java code examples using java.security.SecureRandom (built-in) so you can achieve the same result programmatically in your own projects.

Try the Password Generator Online

Use our free Password Generator directly in your browser — no setup required.

Open Password Generator

Java Code Example

import java.security.SecureRandom;

public class PasswordGenerator {
    private static final String CHARS =
        "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*";
    private static final SecureRandom random = new SecureRandom();

    public static String generate(int length) {
        StringBuilder sb = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            sb.append(CHARS.charAt(random.nextInt(CHARS.length())));
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        System.out.println(generate(20));
    }
}

Quick Setup

Library: java.security.SecureRandom (built-in)
// Built-in — no external dependency needed

Java Tips & Best Practices

  • Always use SecureRandom instead of Random for passwords
  • SecureRandom is cryptographically secure by default
  • Use Passay library for password policy enforcement

Frequently Asked Questions

Password Generator in Other Languages

More Java Tools