How to import a jar in Eclipse?

You can add a jar in Eclipse by right-clicking on the Project → Build Path → Configure Build Path. Under Libraries tab, click Add Jars or Add External JARs and give the Jar. A quick demo here. The above solution is obviously a “Quick” one. However, if you are working on a project where you … Read more

How to load JAR files dynamically at Runtime?

The reason it’s hard is security. Classloaders are meant to be immutable; you shouldn’t be able to willy-nilly add classes to it at runtime. I’m actually very surprised that works with the system classloader. Here’s how you do it making your own child classloader: URLClassLoader child = new URLClassLoader( new URL[] {myJar.toURI().toURL()}, this.getClass().getClassLoader() ); Class … Read more

Reading a resource file from within jar

Rather than trying to address the resource as a File just ask the ClassLoader to return an InputStream for the resource instead via getResourceAsStream: try (InputStream in = getClass().getResourceAsStream(“/file.txt”); BufferedReader reader = new BufferedReader(new InputStreamReader(in))) { // Use resource } As long as the file.txt resource is available on the classpath then this approach will … Read more