We've been hosted on Digital Ocean since 2013 and highly recommend it for your hosting too. Try it now!

JavaScript Minifier

Online JavaScript Minifier/Compressor. Free! Provides an API. Simple Quick and Fast.

Written and hosted by Andrew Chilton (@andychilton) since 2012.

Java

Thanks to Cleber Muramoto for this contribution.

final URL url = new URL("https://javascript-minifier.com/raw");

// JS File you want to compress
byte[] bytes = Files.readAllBytes(Paths.get(...));

final StringBuilder data = new StringBuilder();
data.append(URLEncoder.encode("input", "UTF-8"));
data.append('=');
data.append(URLEncoder.encode(new String(bytes), "UTF-8"));

bytes = data.toString().getBytes("UTF-8");

final HttpURLConnection conn = (HttpURLConnection) url.openConnection();

conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("charset", "utf-8");
conn.setRequestProperty("Content-Length", Integer.toString(bytes.length));

try (DataOutputStream wr = new DataOutputStream(conn.getOutputStream())) {
    wr.write(bytes);
}

final int code = conn.getResponseCode();

System.out.println("Status: " + code);

if (code == 200) {
    System.out.println("----");
    final BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String inputLine;

    while ((inputLine = in.readLine()) != null) {
        System.out.print(inputLine);
    }
    in.close();

    System.out.println("\n----");
} else {
    System.out.println("Oops");
}

Many people have sent language examples over the years, but we want more! We'd love to add more examples in your favourite programming language, so please send them our way. Email andychilton at that gmail place in the sky, tweet me @andychilton, or open an issue or PR on webdev-sh/javascript-minfier.com.

Click on the language of your choice to see an example: