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

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This question has a nice loader implementation you should look at: link

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.