Core Examples

This project contains multiple examples on the usage of the j-framework-core.

Prerequisites:

  1. You should have JDK 17+ Installed. (Click Here).

  2. You should create a Maven Project.

  3. You should enable snapshot versions. (Click Here).

Project Content:

This section contains what you should add to your code structure for this example.

  1. Maven Project with pom.xml that has the following contents:

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>com.jalalkiswani</groupId>
		<artifactId>j-app</artifactId>
		<version>7.0.0-SNAPSHOT</version>
	</parent>
	<artifactId>j-framework-core-examples</artifactId>
</project>
  1. Caching Example located at src/main/java/com/app/cache/JKCacheExample.java that has the following content:

package com.app.cache;

import com.jk.core.cache.JKCacheFactory;
import com.jk.core.cache.JKCacheManager;
import com.jk.core.util.JK;

public class JKCacheExample {
	public static void main(String[] args) {
		//Cache value
		JKCacheManager cm = JKCacheFactory.getCacheManager();
		cm.cache("username", "Jalal");

		//Retrieve value
		String value = cm.get("username", String.class);
		JK.print(value);

		//Remove value
		cm.remove("username", String.class);

		String value2 = cm.get("username", String.class);
		JK.print(value2);//null

	}
}
  1. Exception Example 1 located at src/main/java/com/app/exception/ExceptionExample1.java that has the following content:

package com.app.exception;

import com.jk.core.util.JK;

public class ExceptionExample1 {
	public static void main(String[] args) {
		//Throw null pointer exception and handle with default handler, which log the error then through runtime exception
		try {
			String name = null;
			name.length();
		} catch (Exception e) {
			JK.handle(e);
		}
	}
}
  1. Exception Example 2 located at src/main/java/com/app/exception/ExceptionExample2.java that has the following content:

package com.app.exception;

import com.jk.core.exceptions.handler.JKExceptionHandlerFactory;
import com.jk.core.util.JK;

public class ExceptionExample2 {
	public static void main(String[] args) {
		JKExceptionHandlerFactory.getInstance().registerHandlers("com.app");
		//Throw null pointer exception and handle with custom handler, which show the message in JOptionPane
		try {
			int arr[]=new int[0];
			int x=arr[1];
		} catch (Exception e) {
			JK.handle(e);
		}
	}
}
  1. Logger Example located at src/main/java/com/app/logger/JKLoggerExample.java that has the following content:

package com.app.logger;

import com.jk.core.logging.JKLogger;
import com.jk.core.logging.JKLoggerFactory;

public class JKLoggerExample {
	public static void main(String[] args) {
		JKLogger logger=JKLoggerFactory.getLogger(JKLoggerExample.class);
		String value="Dummy value";
		logger.trace("Print trace message with value {}", value);
		logger.debug("Print debug message with value {}", value);
		logger.info("Print info message with value {}", value);
		logger.error("Print error message with value {}", value);
	}
}

Alternatively, you can clone or download the tutorial repository then import the project into your IDE.

How to run Project:

  1. Set up the project with the content shown above.

  2. Inside your IDE, go to the src/main/java/com/app , and you’ll see three examples, you can try. (we’ll be showing the cache example, but feel free to try any of the others.)

Main Class

  1. Next, run it as a Java Application.

Run as Java Application

  1. Your program will start running and the output result will show in the console.

Output

Example Explanation

  1. This example demonstrates multiple examples on how to use J-Framework, check the different examples packages for more details on how each output was generated.