Unpack and repack a JAR file with all of its dependencies

Repacking an unpacked JAR is a little frustrating because of the folder structure

When unpacking with:

jar xvf JAR_NAME.jar

you get a JAR_NAME/ folder

To repack the JAR:

remove old jar

rm JAR_NAME.jar

get inside the folder

cd JAR_NAME

pack the jar referencing the parent folder

jar cf ../JAR_NAME.jar *

and you will end up with the JAR_NAME.jar in the parent folder, where the original was unpacked from, without the first folder level you would get if you had packed the folder itself.

Develop a modular application – Loading classes using ClassLoader

In Java, all the classes are loaded using several ClassLoader. In this article, we’ll develop a loader for our modules and watch the problems that arrive when working with custom ClassLoaders.

Normally, Java use the system ClassLoader to load all the classes of our application. So it contains all the classes of our application and all the classes our application needs to work. But the problem is that we cannot add our modules jar files into classpath because the application doesn’t know the modules jar files names.

Continue reading

Installing third-party JAR libs in local maven repository

Installing 3rd party JAR libs in local maven repository:

mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> \
    -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging>

Usage:

<dependency>
  <groupId>group-id</groupId>
  <artifactId>artifact-id</artifactId>
  <version>version</version>
</dependency>

Installing 3rd party JAR libs in local maven repository in a different directory:

mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> \
    -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging> \
    -DlocalRepositoryPath=<full-path>

Usage:

<repositories>
  <repository>
    <id>LocalRepo</id>
    <name>Local library folder</name>
    <url>file://full-path</url>
    <layout>default</layout>
    <snapshots>
      <updatePolicy>always</updatePolicy>
    </snapshots>
  </repository>
</repositories>
<dependency>
  <groupId>group-id</groupId>
  <artifactId>artifact-id</artifactId>
  <version>version</version>
</dependency>

 

Log4j Tutorial : How to send log messages to different log files?

One of the common requirement in Java projects, that are using Log4j logging, is to have different log files for each module (or layer) in the project. For example, if you have a web application, you may want to log the debug/info messages from the service layer to a service.log file and the log messages from the presentation layer to the web-app.log file and so on. This is very simple to achieve in Log4j.

Continue reading

Using Log MDC in multi-thread – Hello World example

The slf4j has MDC which will  delegate to underlying logging system’s MDC implementation.  The most popular logging systems are log4j and logback. They both support MDC. So the most common way to use MDC (or logging system) is through slf4j API. In this demo, logback is used as underline logging system.

What can MDC (Mapped Diagnostic Context) do for me?

MDC is a map like structure, it can save information you want to output to the log, so you don’t need add that information to every logger.info(…) as parameter. For example, in an web application, you want every log output contain http request source IP. The IP string don’t need to be added to every logger call , or passed back and forth between controller and service layers. You should save that IP in the MDC, and use %X{IP} in log configuration to add that value to every line of your log output.

Continue reading

Java Dynamic Property Configuration

Recently, I had the need of changing config.properties (predefined application configuration) in Java without restarting the application. I found out a very nice library from Netflix which constantly looks for the changes in your config file and applies these variables runtime.

Continue reading

Make REST calls in PHP

Example of calling GET request
//next example will recieve all messages for specific conversation
$service_url = 'http://example.com/api/conversations/[CONV_CODE]/messages&apikey=[API_KEY]';
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$curl_response = curl_exec($curl);
if ($curl_response === false) {
    $info = curl_getinfo($curl);
    curl_close($curl);
    die('error occured during curl exec. Additioanl info: ' . var_export($info));
}
curl_close($curl);
$decoded = json_decode($curl_response);
if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') {
    die('error occured: ' . $decoded->response->errormessage);
}
echo 'response ok!';
var_export($decoded->response);

Continue reading