Where does Homebrew install packages on Mac?

By default, Homebrew will install all packages in the directory /usr/local/Cellar/, and also creates symbolic links at /usr/local/opt/ and /usr/local/bin/ (for executable files). Terminal /usr/local/Cellar/ /usr/local/opt/ /usr/local/bin/ Terminal % brew -v Homebrew 2.7.2 Homebrew/homebrew-core (git revision 3c379; last commit 2021-01-11) Homebrew/homebrew-cask (git revision 40d45; last commit 2021-01-11) P.S Tested with Homebrew 2.7.2 and macOS Big …

Read more

Convert InputStream to BufferedReader in Java

Note The BufferedReader reads characters; while the InputStream is a stream of bytes. The BufferedReader can’t read the InputStream directly; So, we need to use an adapter like InputStreamReader to convert bytes to characters format. For example: // BufferedReader -> InputStreamReader -> InputStream BufferedReader br = new BufferedReader( new InputStreamReader(inputStream, StandardCharsets.UTF_8)); 1. Reads a file …

Read more

How to initialize a HashMap in Java

This article shows different ways to initialize HashMap in Java. Initialize a HashMap (Standard) Collections.singletonMap Java 9 Map.of Java 9 Map.ofEntries Create a function to return a Map Static Initializer Java 8, Stream of SimpleEntry Conclusion After initialized a HashMap, the result is either a mutable map or an immutable map: Mutable map – It …

Read more

How to get file extension in Java

This article shows how to get the file extension of a file in Java. Topics Get file extension normal Get file extension strict Get file extension hardcode Apache Common IO – FilenameUtils Path: /path/foo.txt -> File Extension: txt Path: . -> File Extension: Path: .. -> File Extension: Path: /path/run.exe -> File Extension: exe Path: …

Read more

Mocking Spring Data DateTimeProvider

In this article, we will demonstrate an integration test where we have to persist the entities with mocked auditing date fields when JPA Auditing is enabled. Technologies used: Spring Boot 2.4.0 Spring 5.3.1 JUnit Jupiter 5.7.0 Tomcat embed 9.0.39 Java 8 1. Project Dependencies pom.xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.h2database</groupId> …

Read more

Java regex check non-alphanumeric string

The Alphanumericals are a combination of alphabetical [a-zA-Z] and numerical [0-9] characters, a total of 62 characters, and we can use regex [a-zA-Z0-9]+ to matches alphanumeric characters. If we want regex matches non-alphanumeric characters, prefix it with a negate symbol ^, meaning we want any characters that are not alphanumeric. ^[^a-zA-Z0-9]+$ Regex explanation ^ # …

Read more

Java regex check alphanumeric string

The Alphanumericals are a combination of alphabetical [a-zA-Z] and numerical [0-9] characters, 62 characters. We can use below regex to match alphanumeric characters: ^[a-zA-Z0-9]+$ Regex explanation ^ # start string [a-z] # lowercase letters from a to z [A-Z] # uppercase letters from A to Z [0-9] # digits from 0 to 9 + # …

Read more

Spring Boot JobRunr examples

This article shows how to use Spring Boot to create REST endpoints to run the background jobs managed by the JobRunr. Tested with: Spring Boot 2.3.12.RELEASE JobRunr 3.1.2 Maven 3 1. Directory Structure. A standard Maven project structure. 2. Project Dependencies. We need a single jobrunr-spring-boot-starter to integrate Spring Boot web and JobRunr. pom.xml <!– …

Read more

What is new in Java 15

Java 15 reached General Availability on 15 September 2020, download Java 15 here. Java 15 features. 1. JEP 339: Edwards-Curve Digital Signature Algorithm (EdDSA) 2. JEP 360: Sealed Classes (Preview) 3. JEP 371: Hidden Classes 4. JEP 372: Remove the Nashorn JavaScript Engine 5. JEP 373: Reimplement the Legacy DatagramSocket API 6. JEP 374: Disable …

Read more

Java – Get the name or path of a running JAR file

In Java, we can use the following code snippets to get the path of a running JAR file. // static String jarPath = ClassName.class .getProtectionDomain() .getCodeSource() .getLocation() .toURI() .getPath(); // non-static String jarPath = getClass() .getProtectionDomain() .getCodeSource() .getLocation() .toURI() .getPath(); Sample output. Terminal /home/mkyong/projects/core-java/java-io/target/java-io.jar 1. Get the path of running JAR 1.1 Create an executable …

Read more

Java – Convert File to Path

This article shows how to convert a File to a Path in Java. java.io.File => java.nio.file.Path // Convert File to Path File file = new File("/home/mkyong/test/file.txt"); Path path = file.toPath(); java.nio.file.Path => java.io.File // Convert Path to File Path path = Paths.get("/home/mkyong/test/file.txt"); File file = path.toFile(); 1. Convert File to Path In Java, we can …

Read more

Java – Run shell script on a remote server

This article shows how to use JSch library to run or execute a shell script in a remote server via SSH. pom.xml <dependency> <groupId>com.jcraft</groupId> <artifactId>jsch</artifactId> <version>0.1.55</version> </dependency> 1. Run Remote Shell Script This Java example uses JSch to SSH login a remote server (using password), and runs a shell script hello.sh. 1.1 Here is a …

Read more

File Transfer using SFTP in Java (JSch)

This article shows how to do file transfer from a remote server to the local system and vice versa, using SSH File Transfer Protocol (SFTP) in Java. P.S Tested with JSch 0.1.55 1. JSch Dependency pom.xml <dependency> <groupId>com.jcraft</groupId> <artifactId>jsch</artifactId> <version>0.1.55</version> </dependency> 2. File Transfer – JSch Examples 2.1 In JSch, we can use put and …

Read more

Jsch – UnknownHostKey exception

This Java example tries to use Jsch to download a file from a remote server to the local system. pom.xml <dependency> <groupId>com.jcraft</groupId> <artifactId>jsch</artifactId> <version>0.1.55</version> </dependency> SFTPFileTransfer.java package com.mkyong.io.howto; import com.jcraft.jsch.*; public class SFTPFileTransfer { private static final String REMOTE_HOST = "1.1.1.1"; private static final String USERNAME = ""; private static final String PASSWORD = ""; …

Read more

JSch – invalid privatekey exception

This Java example tries to use JSch to transfer a file from the local folder to a remote server using public and private keys, instead of a password. pom.xml <dependency> <groupId>com.jcraft</groupId> <artifactId>jsch</artifactId> <version>0.1.55</version> </dependency> SFTPFileTransfer.java package com.mkyong.io.howto; import com.jcraft.jsch.*; public class SFTPFileTransfer { private static final String REMOTE_HOST = "1.1.1.1"; private static final String USERNAME …

Read more

How to check OpenSSH version

On Linux, macOS, or Windows, we can use ssh -V (uppercase V) to check the OpenSSH version currently installed. Example 1 Terminal $ ssh -V OpenSSH_9.0p1, LibreSSL 3.3.6 Example 2 Terminal $ ssh -V OpenSSH_8.4p1 Debian-5+deb11u1, OpenSSL 1.1.1n 15 Mar 2022 References Wikipedia – OpenSSH OpenSSH Release Notes

How to format a double in Java

In Java, we can use String.format or DecimalFormat to format a double, both support Locale based formatting. 1. String.format .2%f For String.format, we can use %f to format a double, review the following Java example to format a double. FormatDouble1.java package com.mkyong.io.utils; import java.util.Locale; public class FormatDouble1 { public static void main(String[] args) { String …

Read more

How to format FileTime in Java

In Java, we can use DateTimeFormatter to convert the FileTime to other custom date formats. public static String formatDateTime(FileTime fileTime) { LocalDateTime localDateTime = fileTime .toInstant() .atZone(ZoneId.systemDefault()) .toLocalDateTime(); return localDateTime.format( DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm:ss")); } 1. File Last Modified Time This example displays the last modified time of a file in a custom date format. GetLastModifiedTime.java package …

Read more

Java – Unable to assign group write permission to a file

In Java, we can use the NIO createFile() to assign file permission during file creation. Files.java package java.nio.file; public static Path createFile(Path path, FileAttribute<?>… attrs) throws IOException But, the createFile() fails to assign the group writes file permission to a file on the Unix system? Path path = Paths.get("/home/mkyong/test/runme.sh"); Set<PosixFilePermission> perms = PosixFilePermissions.fromString("rwxrwxrwx"); Files.createFile(path, PosixFilePermissions.asFileAttribute(perms)); …

Read more

How to get file path separator in Java

For file path or directory separator, the Unix system introduced the slash character / as directory separator, and the Microsoft Windows introduced backslash character \ as the directory separator. In a nutshell, this is / on UNIX and \ on Windows. In Java, we can use the following three methods to get the platform-independent file …

Read more

Where is the java.security file?

In Java, we can find the java.security file at the following location: $JAVA_HOME/jre/lib/security/java.security $JAVA_HOME/conf/security For Java 8, and early version, we can find the java.security file at $JAVA_HOME/jre/lib/security/java.security. Terminal $ /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/security$ ls -lsah total 12K 4.0K drwxr-xr-x 3 root root 4.0K Mei 12 11:53 . 4.0K drwxr-xr-x 8 root root 4.0K Mei 12 11:53 .. …

Read more

Nginx + ModSecurity and OWASP CRS

This tutorial shows how to install ModSecurity (open source web application Firewall) in Nginx, and also enable the OWASP ModSecurity Core Rule Set (CRS). Tested: Nginx Open Source 1.17.7 ModSecurity 3.0 OWASP ModSecurity CRS 3.2.2 Debian The official guide of installing ModSecurity for NGINX is very detail and well documented, and you should refer it. …

Read more

Java Password Hashing with Argon2

Argon2 was the winner of the Password Hashing Competition in July 2015, a one-way hashing function that is intentionally resource (CPU, memory, etc) intensive. In Argon2, we can configure the length of the salt, the length of the generated hash, iterations, memory cost, and CPU cost to control the resources that are needed to hash …

Read more

Java – Convert String to Binary

This article shows you five examples to convert a string into a binary string representative or vice verse. Convert String to Binary – Integer.toBinaryString Convert String to Binary – Bit Masking Convert Binary to String – Integer.parseInt Convert Unicode String to Binary. Convert Binary to Unicode String. 1. Convert String to Binary – Integer.toBinaryString The …

Read more

Java – Convert Integer to Binary

In Java, we can use Integer.toBinaryString(int) to convert an Integer to a binary string representative. Integer.java public final class Integer extends Number implements Comparable<Integer>, Constable, ConstantDesc { public static String toBinaryString(int i) { return toUnsignedString0(i, 1); } //… } This article will show you two methods to convert an Integer to a binary string representative. …

Read more

How to reverse a string in Java

In this article, we will show you a few ways to reverse a String in Java. StringBuilder(str).reverse() char[] looping and value swapping. byte looping and value swapping. Apache commons-lang3 For development, always picks the standard StringBuilder(str).reverse() API. For educational purposes, we can study the char[] and byte methods, it involved value swapping and bitwise shifting …

Read more

Java – How to convert a byte to a binary string

In Java, we can use Integer.toBinaryString(int) to convert a byte to a binary string representative. Review the Integer.toBinaryString(int) method signature, it takes an integer as argument and returns a String. Integer.java public final class Integer extends Number implements Comparable<Integer>, Constable, ConstantDesc { public static String toBinaryString(int i) { return toUnsignedString0(i, 1); } //… } If …

Read more

Java – Convert negative binary to Integer

Review the following Java example to convert a negative integer in binary string back to an integer type. String binary = Integer.toBinaryString(-1); // convert -1 to binary // 11111111 11111111 11111111 11111111 (two’s complement) int number = Integer.parseInt(binary, 2); // convert negative binary back to integer System.out.println(number); // output ?? The result is NumberFormatException! Exception …

Read more

How to reverse a string in Python

In Python, the fastest and easiest way to reverse a string is the extended slice, [::-1]. print("hello world"[::-1]) # dlrow olleh This article will show you a few ways to reverse a string in Python. [::-1] reverse order slice. (Good) [::-1] slice implement as function. (Good) Reversed and Join (Reverse String and Words) (Ok) Python …

Read more

Java >> and >>> bitwise shift operators

In programming, bitwise shift operators, >> means arithmetic right shift, >>> means logical right shift, the differences: >>, it preserves the sign (positive or negative numbers) after right shift by n bit, sign extension. >>>, it ignores the sign after right shift by n bit, zero extension. To work with bitwise shift operators >> and …

Read more