0

The workings of my program:

  1. Connect to server
  2. Get String
  3. Decrypt String
  4. Send it back

I'm decrypting with a class i downloaded from the server. The class changes everytime and should be downloaded everytime i start my program

It HAS! to be in package named etc/etc/client/file.class

It works flawless when i'm testing it INSIDE eclipse cause the package folder is then accesible

But what do i do when i want to export is as runnable .jar ? Then i can't write in the package folder?

The line that's loading the class: (The class extends Base64 which is already in the folder)

etc.sec.client.Base64 decode = (etc.sec.client.Base64)Class.forName("etc.sec.client." + handlerClass).newInstance();
// Handler class is the name of the class

The folder i'm downloading the class to before loading newInstance():

bin/etc/sec/client/"+filename+".class

Works perfect in eclipse but i do not know how to make it work when exporting to .jar

home
  • 12,468
  • 5
  • 46
  • 54
Stackie Overflower
  • 201
  • 1
  • 6
  • 14

1 Answers1

0

You will have to load the class using a new class loader.

public void go() throws Exception {
    ClassLoader cl = new URLClassLoader(new URL[] { new URL("file:///home/ben/") }, this.getClass().getClassLoader());
    Class decoderclass = cl.loadClass("etc.sec.client." + handlerClass);
    etc.sec.client.Base64 decode = (etc.sec.client.Base64)decoderclass.newInstance();
    System.out.println(decode.toString());
}

If download your class into:

/home/ben/etc/sec/client/

That should instantiate the class fine. Naturally you will have to use the interface available at compile time, etc.sec.client.Base64 must be an interface or your handler class must inherit from it.

tribeca
  • 462
  • 3
  • 8
  • This question has a nice loader implementation you should look at: [link](http://stackoverflow.com/questions/3580752/java-dynamically-loading-a-class) – tribeca Mar 25 '12 at 17:59