How to make an empty PUT request in JAVA

public static HttpURLConnection put(String endpoint, String token) {
HttpURLConnection con = null;
try {
URL url = new URL(endpoint);
con = (HttpURLConnection) url.openConnection();
con.setRequestProperty("Authorization", "Bearer " + token);
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("User-Agent", <em>USER_AGENT</em>);
con.setRequestProperty("Content-Length" , "0");
DataOutputStream out = new DataOutputStream(con.getOutputStream());
} catch (Exception e) {
return con;

If the Content-Length is not specified the server will return a 411 status code error.

HTTP Status Code 411 (Length Required) is sent by the server as a response when it refuses to accept a message without a content-length header, for whatever reason.

The DataOutputStream is still required even if you send an empty payload.

Java find project location

public static String findProjectLocation() {
String path = "";
try {
path = new File(".").getCanonicalPath();
} catch (IOException e) {
System.<em>setProperty</em>("app.home", path);
return path;

How to build a jar with dependencies included using Maven


Then run this maven command  : package

How to write logs in Java using log4j

# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=DEBUG, A1, FILE

# A1 is set to be a ConsoleAppender.

# A1 uses PatternLayout.
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

# Define the file appender

# Set the name of the file

# Set the immediate flush to true (default)

# Set the threshold to debug mode

# Set the append to false, should not overwrite

# Set the maximum file size before rollover

# Set the the backup index

# Define the layout for file appender
log4j.appender.FILE.layout.conversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p - %m%n

How to encrypt/decrypt with 256 bit in JAVA

Java Cryptography Extension (JCE) Unlimited Strength is needed. Without it the maximum key size are limited to 128-bit. You may get this error : Illegal key size

To avoid this, in Java 8 you must download : and add the files in C:/Program Files/Java/jre<version>/lib/security/

Java 9 enables unlimited encryption by default.

How to read from Windows Registry using Java

public static String readRegistry(String location, String key) {
String result = "";
try {
Process process = Runtime.<em>getRuntime</em>().exec("reg query " + '"' + location + "\" /v " + key);
StreamReader reader = new StreamReader(process.getInputStream());
String[] parsed = reader.getResult().split("\\s+");
if (parsed.length > 1) {
result =  parsed[parsed.length - 1];
} catch (InterruptedException | IOException e) {
return result;

Key Promoter X – Learn every shortcut for IntelliJ

Or MouseFeed for Eclipse

Shows the user the keyboard short-cuts when a button is pressed with the mouse. This provides an easy way to learn how to replace tedious mouse work with keyboard keys and helps to transition to a faster, mouse free development. Currently, it supports toolbar buttons, menu buttons, and tool windows and the actions therein.


Get computer name in Java

public static String getComputerName() {
String computerName = System.<em>getenv</em>("COMPUTERNAME");
if (computerName == null || computerName.isEmpty()) {
try {
computerName = InetAddress.<em>getLocalHost</em>().getHostName();
Constants.<em>computerName </em>= computerName;
} catch (UnknownHostException e) {
Logger.<em>message</em>("Cannot get the computer name");
if (computerName != null && !computerName.isEmpty()) {
computerName = computerName.split("\\.")[0];
return computerName;

<strong>Jackson deserialize array of objects in Java</strong>

public static List<Car> getCars(String response) {
List<Car> carList = new ArrayList<>();
try {
carList = Arrays.<em>asList</em>(ObjectBuilders.<em>getObjectMapper</em>().readValue(response, Car[].class));
} catch (Exception e) {
return carList;

If you have a jsonArray response, then you must provide a Car[].class type

Some json utils :

public static boolean isJSONValid(String test) {
try {
new JSONObject(test);
} catch (JSONException ex) {
try {
new JSONArray(test);
} catch (JSONException ex1) {
return false;
return true;

public static ObjectMapper getObjectMapper() {
if (<em>objectMapper </em>== null) {
<em>objectMapper </em>= new ObjectMapper();
return <em>objectMapper</em>;

Logger in Java - slf4j.Logger

public class Logger {

private Logger(){}
private static org.slf4j.Logger <em>logger </em>= LoggerFactory.<em>getLogger</em>(Logger.class);

public static void log(Exception e) {
<em>logger</em>.error(e.getLocalizedMessage() + e.getMessage());
StackTraceElement[] elements = e.getStackTrace();
for (int i = 1; i < elements.length; i++) {
StackTraceElement s = elements[i];
<em>logger</em>.error("\tat " + s.getClassName() + "." + s.getMethodName()
+ "(" + s.getFileName() + ":" + s.getLineNumber() + ")");