Search code examples
javajarbuildpath

Java Build Path


I have a quick question about altering the build path as the code is running.

For example, I have a class which downloads a .jar file from the internet and then into the same directory as the code is running from. How, if possible, could I load the jar into the build path to access the classes within the .jar file?


Solution

  • Some suggested amendments / comments:

    • Remove the jar: prefix and the !/ suffix - these are note required and are probably confusing the matter
    • Can you verify the jar file exists:

      System.out.println(new File(new URL("file://test.jar")).exists());

    • Amend your class declaration to the following (get the File object to generate the URL for you, to avoid problems):

      URL[] classes = new URL[] { new File("test.jar").toURI().toURL() };

    This worked for my test example, using commons-codec as the jar, and loading the Base64 class