Reading and writing jar files using java

Hi Friends ,

I’m back with another interesting tutorial , today i’ll telling how to read a jar file and write the jar file to destination folder.

You can also say copying jar file from one location to another /destination location.

So first find the code below :

So now what i’m doing is reading the “sourceLocation” checking whether its a file if yes calling my method to copy that file to “destinationLocation” else if its folder i’m going inside the folder scanning for jar file and calling the method passing that file as argument.

So in the method “copyJarFile” takes two argument the source jar file and other the destination folder where the source file must be copied here we are using the

JarOutputStream jos = new JarOutputStream(new FileOutputStream(destFile));

JarOutputStream for taking jar file as inputstream

then

Enumeration<JarEntry> entries = jarFile.entries();

inorder get the entries inside it and writing jos.

So using while loop all entries are written and then we are closing stream and flushing out .

 

Feel free to comment for any doubts , also if you like it please share as sharing is caring 🙂

Advertisements

Simple java program to read files inside jar files and run those files .

Hi Everyone ,

Today we will be learning a very interesting topic where in your most of questions related to

How to get a path to a resource in a Java JAR file?
Running the files inside the jar file once packaged?

So the main thing is firstly you need to add your resource folder to your build.

In order to that

1)Right click on the project

2)Project properties

3)Search build and add your resource folder in source

4)Apply and close

bloggJava

Now once clean  and build the project.

Now here is the tricky part , when you export your project as jar and run the program you will still get the java.io null pointer / file not found exception i.e you’ve use the

class.getClassLoader().getResource("index.html");

Here “index.html” is a file inside my resource folder.

What really happens at run time is we do not the get path as its inside the jar and not on the user’s file system.

So what i’ve implemented is

class.getClassLoader().getResourceAsStream("index.html");

 

I’ve got it as stream then read the file line  by line and created a folder(i.e resourcess) on the user system and copied the file.

So now we have a real relative path available so we can do stuff , here i wanted the file (i.e index.html) to be opened on the default browser of operating system.

And now the source code.

Please feel free to ask any doubts and questions.

If it solve your problem please share 🙂 as sharing is caring.

Building a docker image using simple jar File.

Hi Guys Today is a very Special tutorial of how to create a docker image from scratch.

 

A Simple way to create docker image.

The workflow of Dockerfile and building is very simple. It goes like this….

  1. Docker first run’s the container that you mentioned us FROM (in our case its Java 8). So docker first pulls down that image ,and run’s it.
  2. It then looks for the first instruction after that, and after executing that, it does something similar to #docker commit, so that it saves a layer with what is done till that instruction.
  3. Docker run’s a new container with the last committed image (ie. The image created from the previous instruction step in the Dockerfile. The Running in   type of messages during the build command showed in the above example indicates this.)
  4. It then executes the next instruction in the Dockerfile, and again does a commit and creates another image. So that the very next instruction is executed inside this new image committed. And this process continues till the very last instruction in the Dockerfile.

Feel free to comment for any doubts.

If you like please share and subscribe 🙂

Using Font Awesome Stack Property and Printing Images in Console

Hi Guys,

I’ll be showing how to using the stack property of font awesome inorder to show numbers inside the icons.

It can be handy in many situations like showing the notification icon with no of notifications.

The second thing will be printing images in the console.

I’d a rare scenario where i needed the images received is right or wrong so instead of displaying i choose to print on console.

Blog

Also find the github code here :

Feel free to comment and share 🙂

Add Multiple Markers to Map from user input

Hello Everyone ,

Recently i’ve been in a situation where in i needed to drop markers to location on map wherein the location is given by the user.

It was a unique situation as in the markers should be updated in real time to map with retaining the previous markers.

After digging to various stack overflow posts and googling found that there no easier way to it.So wrote a simple java script code which adds the markers to map in real time also retaining the previous location

screenshot19thMarcha

 

 

And bang done.

Feel free share and ask Questions.