Jersey and JSON examples (EclipseLink MOXy)

This article shows how to return a JSON response in the Jersey application, using EclipseLink MOXy. Tested with Jersey 3.0.2 EclipseLink MOXy 3 Jetty 11, HTTP Server Java 11 Maven 3 SLF4J, Logback, redirect Jersey J.U.L logs to logback JUnit 5 and JSONassert 1.5 (Unit Test) org.json 20210307, JSONObject Table of contents 1. EclipseLink MOXy …

Read more

VirtualBox running slow and lag on macOS, MacBook Pro

This article shows how to fix VirtualBox 6.1.* running slow on macOS, MacBook Pro with retina display. Tested with VirtualBox 6.1.22 macOS Big Sur 11.1 MacBook Pro (16-inch, 2019) Kali Linux, Ubuntu, Debian 1. VirtualBox with more memory, CPU, etc In VirtualBox, clicks on the Settings icon. 1.1 Increase based memory. 1.2 Increase CPU processors. …

Read more

Git – Unable to resolve reference ‘refs/remotes/origin/master’: reference broken

A git pull and hits the below "reference broken" error message? Terminal $ git pull error: cannot lock ref ‘refs/remotes/origin/master’: unable to resolve reference ‘refs/remotes/origin/master’: reference broken From bitbucket.org:xx/mkyong-tutorials ! [new branch] master -> origin/master (unable to update local ref) Solution Unsure the root cause of the "reference broken". To fix it, delete this file …

Read more

Java Logging APIs Tutorial

This tutorial shows logging using the Java built-in logging APIs in the java.util.logging package. Table of contents 1. Java Logging APIs Hello World 2. Logging Levels 3. logging.properties 4. java.util.logging.config.file 5. Why choose java.util.logging 6. Download Source Code 7. References P.S The java.util.logging is bundled with the Java since JDK 1.4. 1. Java Logging APIs …

Read more

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

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.NoClassDefFoundError: javax/xml/bind/JAXBException

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

logging.properties example

The Java logging APIs (java.util.logging) default loads logging.properties in the $JAVA_HOME/jre/lib/ (Java 8 and before); for Java 9 and above, the logging.properties file moved to $JAVA_HOME/conf. Note Java Logging APIs Tutorial logging.properties Here is the logging.properties file from Java 11. C:\opt\jdk-11.0.1\conf ############################################################ # Default Logging Configuration File # # You can use a different file …

Read more

Jersey and HK2 dependency injection (auto scanning)

This article shows how to use HK2 dependency injection framework in Jersey and enable auto-scanning auto-discovery of the declared @Contract and @Service components. Table of contents 1. Jersey and HK2 dependency injection 2. @Contract, @Service, and @Inject 3. HK2 manual register @Contract and @Service 4. HK2 auto scanning @Contract and @Service 4.1 HK2 inhabitant files …

Read more

jakarta.activation.DataSource was not found

Using Jersey 3x + Jetty to develop endpoints, but hits the jakarta.activation.DataSource warning during application startup? Terminal SLF4J: No SLF4J providers were found. SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See http://www.slf4j.org/codes.html#noProviders for further details. May 27, 2021 10:09:48 AM org.glassfish.jersey.message.internal.MessagingBinders$EnabledProvidersBinder bindToBinder WARNING: A class jakarta.activation.DataSource for a default provider MessageBodyWriter<jakarta.activation.DataSource> was not found. …

Read more

NoClassDefFoundError: jakarta/servlet/ServletInputStream

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

Jersey and Jetty HTTP Server examples

This article shows how to start a Jetty HTTP Sever to run a JAX-RS or Eclipse Jersey application. Tested with Jersey 3.0.2 Jetty 11 Jackson 2.12.2 JUnit 5.4.0 (unit test) JSONassert 1.5.0 (unit test) Maven 3.8.3 Java 11 Tables of contents 1. Using Jersey with Jetty HTTP Server 2. Project Directory 3. Project dependencies 4. …

Read more

How to read a file in Python

This article shows how to read a text file in Python. Table of contents 1. Read a file using open() and close() 2. Read a file using with statement 3. Read a file using try-finally 4. Read a file line by line 5. Read a file and skip the first line 6. Default open mode …

Read more

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

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

How to load logging.properties for java.util.logging

In Java Logging APIs or java.util.logging, we use system property java.util.logging.config.file to define the location of the logging.properties file. Table of contents 1. Loads logging.properties at runtime 2. Loads logging.properties from the classpath 2.1 LogManager 2.2 System.setProperty("java.util.logging.config.file") 3. Download Source Code 4. References Note Java Logging APIs Tutorial 1. Loads logging.properties at runtime In the …

Read more

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

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

Java – How to add and remove BOM from UTF-8 file

This article shows you how to add, check and remove the byte order mark (BOM) from a UTF-8 file. The UTF-8 representation of the BOM is the byte sequence 0xEF, 0xBB, 0xBF (hexadecimal), at the beginning of the file. 1. Add BOM to a UTF-8 file 2. Check if a file contains UTF-8 BOM 3. …

Read more

Java Swing Hello World example

The below program launch a Java Swing application; It configures a JFrame and attach a JLabel to display a hello world and center the JLabel component. Read the comment for self-explanatory. SwingHelloWorld.java package com.mkyong.swing import javax.swing.*; import java.awt.*; public class SwingHelloWorld { public static void main(String[] args) { JFrame frame = new JFrame("Hello World Java …

Read more

Gradle – How to run a single unit test class

In Gradle, we can pass an –tests option to run a single unit test class. Read this Gradle Test Filtering. Terminal gradle test –test TestClass P.S Tested with Gradle 6.7.1 1. Run a single test class Review a simple unit test. DummyTest.java package com.mkyong.security.db; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertTrue; public class DummyTest { @Test void …

Read more

Gradle + Quarkus Hello World example

This article shows how to create or scaffold a Gradle + Quarkus JAX-RS hello world project, which creates a /hello endpoint and returns a string. Technologies used: Quarkus 1.11.0 Gradle 6.7 Maven 3.6.3 Java 11 Topics Creating a new Gradle + Quartus project Gradle + Quarkus Project Structure Quarkus and build.gradle Quarkus Dependencies (runtimeClasspath) Quarkus …

Read more

Maven + Quarkus Hello World example

This article shows how to use the quarkus-maven-plugin to create or scaffold a Maven + Quarkus JAX-RS hello world project. Technologies used: Quarkus 1.11.0 Maven 3.6.3 Java 11 Topics Creating a new Maven + Quartus project Maven + Quarkus Project Structure Quarkus and pom.xml Quarkus Dependencies Quarkus JAX-RX endpoint Quarkus and Dockerfile Run Quarkus in …

Read more

Go to the parent folder on macOS

This article shows how to use a keyboard shortcut to access the parent folder or any item or folder on macOS. 1. Go to the parent folder. (Command + Up Arrow) In Finder, we can press Command + ↑ to access the parent folder of any item or folder. 1.1 For example, in Finder, this …

Read more

Go to a specified folder on macOS

This article shows how to use a keyboard shortcut to go to a specified folder on macOS. 1. Go to a specified folder. (Shift + Command + G) In Finder, there is no explorer to type the path you want to go. To fix it, we can press Shift + Command + G together to …

Read more

Install VirtualBox on macOS, the installation failed?

Try to install VirtualBox on macOS, and hits the error "The Installation failed."? P.S Tested with VirtualBox 6.1 and macOS Big Sur 11.1 Solution 1.1 Desktop, top menu, clicks Apple icon -> System Preferences… 1.2 Clicks on the Security & Privacy icon. 1.3 Clicks on the left-bottom lock icon, type password to unlock it. 1.4 …

Read more

Java DOM Parser XML and XSLT examples

With XSLT(Extensible Stylesheet Language Transformations), we can transform XML documents into other formats such as HTML. Table of contents 1. DOM Parser and Transformer 2. DOM example: XML + XSLT = HTML format 3. Download Source Code 4. References P.S Tested with Java 11 1. DOM Parser and Transformer We can use the TransformerFactory to …

Read more