In Java, we can use Integer.valueOf(String) to convert a String to an Integer object; For unparsable String, it throws NumberFormatException. Integer.valueOf("1"); // ok Integer.valueOf("+1"); // ok, result = 1 Integer.valueOf("-1"); // ok, result = -1 Integer.valueOf("100"); // ok Integer.valueOf(" 1"); // NumberFormatException (contains space) Integer.valueOf("1 "); // NumberFormatException (contains space) Integer.valueOf("2147483648"); // NumberFormatException (Integer max […]

Read more How to convert String to Integer in Java

Java 17 is a long-term support (LTS) release, reached General Availability on 14 September 2021, download Java 17 here. Java 17 has 14 JEP items. 1. JEP 306: Restore Always-Strict Floating-Point Semantics 2. JEP 356: Enhanced Pseudo-Random Number Generators 3. JEP 382: New macOS Rendering Pipeline 4. JEP 391: macOS/AArch64 Port 5. JEP 398: Deprecate […]

Read more What is new in Java 17

Starts a Java project and hits the below "class not found for com.sun.xml.bind.v2.ContextFactory"? Terminal Caused by: java.lang.ClassNotFoundException: com.sun.xml.bind.v2.ContextFactory at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:606) at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:168) at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522) at jakarta.xml.bind.ServiceLoaderUtil.nullSafeLoadClass(ServiceLoaderUtil.java:92) at jakarta.xml.bind.ServiceLoaderUtil.safeLoadClass(ServiceLoaderUtil.java:125) at jakarta.xml.bind.ContextFinder.newInstance(ContextFinder.java:230) … 43 more Solution The com.sun.xml.bind.v2.ContextFactory is under the JAXB APIs; Java 9 deprecated the JAXB, and Java 11 deleted the JAXB completely. To fix […]

Read more java.lang.ClassNotFoundException: com.sun.xml.bind.v2.ContextFactory

Migrate a Java project to Java 11 and hits the below "class not found error for JAXBException" java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException A short history of JAXB on Java The Jakarta XML Binding (JAXB; formerly Java Architecture for XML Binding) is an XML binding framework to convert Java classes to and from XML. The JAXB is part of […]

Read more java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException

Using Jersey 3x + Jetty to develop endpoints, but hits the following error during application startup. Terminal Exception in thread "main" java.lang.NoClassDefFoundError: jakarta/servlet/ServletInputStream at org.glassfish.jersey.jetty.JettyHttpContainerProvider.createContainer(JettyHttpContainerProvider.java:43) at org.glassfish.jersey.server.ContainerFactory.createContainer(ContainerFactory.java:58) at org.glassfish.jersey.jetty.JettyHttpContainerFactory.createServer(JettyHttpContainerFactory.java:110) at com.mkyong.MainApp.startServer(MainApp.java:22) at com.mkyong.MainApp.main(MainApp.java:32) Caused by: java.lang.ClassNotFoundException: jakarta.servlet.ServletInputStream at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583) at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178) at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521) … 5 more 1. jakarta/servlet/* and Servlet API 5.0 Since Servlet API […]

Read more NoClassDefFoundError: jakarta/servlet/ServletInputStream

In Java, if we remove items from a List while iterating it, it will throw java.util.ConcurrentModificationException. This article shows a few ways to solve it. Table of contents 1. java.util.ConcurrentModificationException 2. Java 8 Collection#removeIf 2.1 removeIf examples 2.2 removeIf uses Iterator 3. ListIterator example 4. Filter and Collect example 5. References P.S Tested with Java […]

Read more Java – How to remove items from a List while iterating?

This article shows how to solve the popular JAXB exception Implementation of JAXB-API has not been found on module path or classpath. Table of contents 1. JAXBException: Implementation of JAXB-API has not been found 2. We need a JAXB Implementation 3. JAXB Implementation – Jakarta XML Binding 4. JAXB Implementation – EclipseLink MOXy 4.1 Implementation […]

Read more JAXBException: Implementation of JAXB-API has not been found on module path or classpath

Java 16 reached General Availability on 16 March 2021, download Java 16 here. Java 16 has 17 JEP items. 1. JEP 338: Vector API (Incubator) 2. JEP 347: Enable C++14 Language Features 3. JEP 357: Migrate from Mercurial to Git 4. JEP 369: Migrate to GitHub 5. JEP 376: ZGC: Concurrent Thread-Stack Processing 6. JEP […]

Read more What is new in Java 16

In this article, we’ll learn how to read and write Microsoft Word with the help of Apache POI library. Topics Apache POI – HWPF and XWPF Apache POI dependencies Create a word document Working with Paragraphs, Fonts, and Page breaks Document Header and Footer Add Image Create a table Read and parse document, paragraph, and […]

Read more Java – Read and Write Microsoft Word with Apache POI

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 Convert InputStream to BufferedReader in Java

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 What is new in Java 15

Run a simple java command on Ubuntu and hits the below errors: Terminal $ java -version bash: /usr/bin/java: cannot execute binary file: Exec format error P.S Tested with Java 15 and Ubuntu 20 1. Problem – Download the wrong JDK build The error Exec format error means we download the wrong JDK build for a […]

Read more bash: /usr/bin/java: cannot execute binary file: Exec format error

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 – Get the name or path of a running JAR file

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 – Convert File to Path

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 Java – Run shell script on a remote server

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 File Transfer using SFTP in Java (JSch)

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 – UnknownHostKey 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 JSch – invalid privatekey exception

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 a double 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 How to format FileTime in Java

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 Java – Unable to assign group write permission to a 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 Where is the java.security file?