Reloading jars at Runtime

I haven’t blogged in a while but now i have some time :) . This week I had to dynamically reload some jars from a directory and i searched the net about the problem. I will show the implementation that I finally came up with. The problem with the dynamically loading external jars is that they should not be in the CLASSPATH because the system classloader is making some optimizations(cache) of the currently loaded classes and doesn’t search again in the CLASSPATH if it has already instantiated the class. So to make the jars dynamically loadable we have to make our own ClassLoader or we can use the URLClassLoader which is part of the JavaSE. The idea is that every time we load the class we should use a new object of the URLClassLoader so we don’t use the cache. And also we should have an interface so we can cast our instantiated object or the alternative is to use reflection and the Method class. But enough words here is the implementation:
1. The Interface :

package org.nikolavp;
public interface Reloadable{
    public void method();

2. The Class which will reload our class by a given name:

import org.nikolavp.Reloadable;
public class ClassReloader{ 
    public static Reloadable reload(String className) {
        Reloadable obj = null;
        URL jar = null;
        try {
            jar = new File("jars/somejar.jar").toURI().toURL();
        } catch (MalformedURLException e) {
        URLClassLoader loader = new URLClassLoader(new URL[] { jar },
        try {
            Class clazz = loader.loadClass(className);
            obj = (Reloadable) clazz.newInstance();
        } catch (ClassNotFoundException e) {
        } catch (InstantiationException e) {
        } catch (IllegalAccessException e) {
        }  return obj;

3. The Main class to test our work:

import org.nikolavp.Reloadable;
public class Main{
    public static void main(String[] args) {
        while (true) {
            Reloadable object = ClassReloader.reload("MyClassFromSomeJar");
            try {
                Thread.sleep(10 * 1000);
            } catch (InterruptedException e) {

Assume that MyClassFromSomeJar exist in the jars/somejar.jar
you can now make changes to the
code in the external jar and see the results :)

Conclusion: This is one of the areas where java really shines. Try doing that with static languages like C/C++ :)

Sparse matrix multiply dummy algorithm

Today almost the half an hour I was trying to write that algorithm which multiplies two sparse matrix. At the end this is what I’ve got. The matrix is represented as a linkedlist of triples, because this is how they want it in our university. There were some mistakes in the code like Sparce is not with “s”, but those are pretty much syntax mistakes and i didn’t want to make changes for our teacher :) .

public NumericMatrix multWith(SparseMatrix mat) throws CannotMult, 
   OutOfMatrixRange {
   if (!canMult(mat)){
       throw new CannotMult();
   SparseMatrix result = new SparseMatrix(this.rows, mat.columns);
   if (body.isEmpty() || mat.body.isEmpty()){
       return result;
   Iterator<Triple> itForThis = this.body.iterator();
   while (itForThis.hasNext()) {
       Triple thisElement =;
       Iterator<Triple> itForMat = mat.body.iterator();
       while (itForMat.hasNext()) {
           Triple thatElement =;
           if (thatElement.i > thisElement.j){
           if (thisElement.j == thatElement.i) {
               double value = result.elementAt(thisElement.i,
               result.setElement(thisElement.i, thatElement.j, value
                       + thisElement.element * thatElement.element);
   return result;

Sorry for the bad code formatting but java is kind of verbose :( . All in all the code is not so nice but it was the only thing that came up and worked so I am posting it here if someone need it, because there wasn’t good simple code for that algorithm. I didn’t have the time to look at performance libraries and how they implement that, but I read some interesting stuff on I didn’t know that there was an algorthm that can multiply two Ordinary matrices in O(n^2.376). Take a look at the page. If you find some free materials about the algorithm please let me know I will be glad to look into it, because I might need it for some graphic stuff I will be doing next year :)

UPDATE: Fixed some stuff and chan the name to SparseMatrix to be really normal :)

Eclipse for c++

Hmm lately I started using eclipse more and more. It seems they made it to not crash so much with the new release. I am pretty happy with it for C++ development it seems to own. I am not sure but i think that I am even more productive with it now than with vim. I have used the 3.2 version but it was full crap back then ;) . Besides it is written in java it is worth looking in it.

Don’t trust garbage collectors

I recently read This blog post and the author has a nice point about garbage collectors. When we program in languages with that feature we should not blindly trust the automatic memory management for everything. Most of the time it is ok, but sometimes you have to think about memory management in those languages too.
In the blog post Tahseen Ur Rehman gives an example with a thread which was holding some references to the objects so they could not be deleted, so the users were getting OutOfMemoryErrors. The author also warn us that we should not trust the lie

“In some programming languages, memory management is the programmer’s responsibility.
… …
An alternate approach to memory management that is now commonly utilized, especially by most modern object-oriented languages, is automatic management by a program called a garbage collector“

There is also a nice example in Effective Java with the same point. So next time when we want to do something smart with the language, we should think if it is ok for the garbage collector and it will be able to do it’s job. I find that most of the time those kind of errors are found in concurrent programs and hacky arrays.

A little benchmark with C++ and Java in the game.

Today i just wanted to make a benchmark and see which is faster java or C++. There are rumors over the internet that java is or will be faster than c++ some day. So today I decided to try compare the speed of those languages. We all know arrays and how bad they can be sometimes if you are not careful with them in C++, because

    • They don’t know it’s size


  • You cannot extend it if you reach the limit



  • The name of an array converts to a pointer to its first element at the slightest provocation(there are nice examples for this point here)



So with one word arrays are not nice. Java provides some fixes for those, because there an array know it’s size and there are no pointers :) . But what if we use a higher level abstractions from STL? Will std::vector beat the Java arrays? Here is the code:

#include <vector>

#include <iostream>

int main(int argc, char* argv[]){

std::vector<int> Vec(10000);

for(int x = 0;x < Vec.size();x++){

Vec[x] =x;

std::cout << Vec[x] << std::endl;



class Main{

public static void main(String []args){

int Vec[] = new int[10000];

for (int x = 0;x < Vec.length;x++){

Vec[x] = x;





So we compile with

g++ -O2 -fomit-frame-pointer main.cpp && javac

and let’s run them through time :)

nikolavp@Nikolavp:~$ time ./a.out > /dev/null

real    0m0.010suser    0m0.004ssys     0m0.004snikolavp@Nikolavp:~$ time java Main > /dev/null

real    0m0.196suser    0m0.140ssys     0m0.052s

So C++ with vector which is supposed to be a higher level abbstraction outperformed Java’s “safe” arrays. The next time you want to write something in Java think a little bit is it really worth the machines ;) . Don’t get me wrong Java have it’s place and it made the whole world a better place, but don’t convince me, that it will be faster(or is faster now) than C++… It just never will.

using namespace std;
int main(){

for(var i=0;i{
click("sign in")