0

Is there a way to import other classes in Java without adding them to the classpath? Something like "import C:/dir/file.jar"?

Matteo
  • 14,696
  • 9
  • 68
  • 106
Andrew
  • 6,254
  • 16
  • 59
  • 93
  • 2
    [How to load a jar file at runtime](http://stackoverflow.com/questions/194698/how-to-load-a-jar-file-at-runtime) – stivlo Oct 09 '11 at 17:44

1 Answers1

2

You can't import a jar file just by changing the "import". But you can using the class loader. See How to load a jar file at runtime

File file  = new File("C:\\dir\\file.jar");
URL url = file.toURL();  
ClassLoader classLoader = new URLClassLoader( new URL[]{ file.toURL() } );
Class cls = classLoader.loadClass("mypackage.myclass");
Community
  • 1
  • 1
Matteo
  • 14,696
  • 9
  • 68
  • 106