Jump to content

Why wouldn't a copy of the jar file work?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
sourlemon

sourlemon

    Programmer

  • Members
  • PipPipPip
  • 99 posts
Hi guys, I'm trying to dynamically load a class. But I'm having trouble with the jar file. This is a snippet of my my code:

    File jarFile = null;
    URL url = null;
    try {
            String path = "C:\\Users\\Orange\\Documents\\Lab5\\RpcServer\\MaxMin.jar";
            jarFile = new File(path);
            url = new URL("file", null, -1, jarFile.getAbsolutePath());
            URLClassLoader classLoader = new URLClassLoader(new URL[] {url});
        return classLoader.loadClass("nhu.lab5.maxmin.MaxMinImpl").newInstance();

That doesn't work. But if I replace the path to where the original file is at, it works fine.

    File jarFile = null;
    URL url = null;
    try {
            String path =  "C:\\Users\\Orange\\Documents\\Lab5\\MaxMin\\dist\\MaxMin.jar";
            jarFile = new File(path);
            url = new URL("file", null, -1, jarFile.getAbsolutePath());
            URLClassLoader classLoader = new URLClassLoader(new URL[]  {url});
        return  classLoader.loadClass("nhu.lab5.maxmin.MaxMinImpl").newInstance();

I'm sure I copy the exact same file, but I don't understand why the first code would work but the second one doesn't. Help?

Thank you in advance.

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
It looks like your jar files is part of a package, which means the .class files MUST be in the correct folder.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
sourlemon

sourlemon

    Programmer

  • Members
  • PipPipPip
  • 99 posts
You're awesome. Thank you! :thumbup: