# Configure TrueProxies with Java

> Configure TrueProxies endpoints, authentication, and targeting with Java HttpClient and HttpURLConnection.

Source: https://docs.trueproxies.com/integrations/java/

> **Use your own endpoints**
>
> The examples below use placeholders. Copy the connection host, ports, username, and proxy password from the **Username and password** tab of your service in the dashboard. See [How to connect](https://docs.trueproxies.com/proxy-instructions/how-to-connect/#connection-host-and-ports).

## HttpClient (Java 11+)

```java
import java.net.*;
import java.net.http.*;

public class ProxyExample {
    public static void main(String[] args) throws Exception {
        var proxy = ProxySelector.of(
            new InetSocketAddress("YOUR_HOST", 8080)
        );

        Authenticator.setDefault(new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(
                    "your_username", "your_password".toCharArray()
                );
            }
        });

        var client = HttpClient.newBuilder()
            .proxy(proxy)
            .authenticator(Authenticator.getDefault())
            .build();

        var request = HttpRequest.newBuilder()
            .uri(URI.create("https://httpbin.org/ip"))
            .build();

        var response = client.send(request, HttpResponse.BodyHandlers.ofString());
        System.out.println(response.body());
    }
}
```

## HttpURLConnection

```java
import java.io.*;
import java.net.*;

public class ProxyExample {
    public static void main(String[] args) throws Exception {
        Proxy proxy = new Proxy(Proxy.Type.HTTP,
            new InetSocketAddress("YOUR_HOST", 8080));

        Authenticator.setDefault(new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(
                    "your_username", "your_password".toCharArray()
                );
            }
        });

        URL url = new URL("https://httpbin.org/ip");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection(proxy);

        BufferedReader reader = new BufferedReader(
            new InputStreamReader(conn.getInputStream())
        );
        String line;
        while ((line = reader.readLine()) != null) {
            System.out.println(line);
        }
        reader.close();
    }
}
```

## Residential IPv4 country targeting

```java
return new PasswordAuthentication(
    "your_username-country-us", "your_password".toCharArray()
);
```

## Related guides

- [Connection and username format](https://docs.trueproxies.com/proxy-instructions/how-to-connect/)
- [HTTP CONNECT and SOCKS5](https://docs.trueproxies.com/proxy-instructions/proxy-protocols/)
- [Trusted IPs](https://docs.trueproxies.com/proxy-instructions/trusted-ips/)
- [Troubleshooting checklist](https://docs.trueproxies.com/support/)
