How to Read a File from CLASSPATH in Java

This can be done in two simple ways   Using ClassLoader.getResourceAsStream This method can be used to load any file from CLASSPATH InputStream in =this.getClass().getClassLoader().getResourceAsStream(“MyFile.txt”);   Using Class.getResourceAsStream This method can be used to load files using relative path to the package of the class InputStream in =this.getClass().getResourceAsStream(“SomeTextFile.txt”);   This method can also be used to load any files … Read more

How can we Include Jar within a Jar in Java Classpath

There is no easy way to do this in current java versions. There are 2 alternatives to deal with this problem using third party libraries. Use Jar class loader library The “http://www.jdotsoft.com/JarClassLoader.php”JarClassLoader library provides you the feature of loading resources from a top JAR and from JARs inside the top JAR.   Explode and combine into one … Read more

What is the Difference Between NoClassDefFoundError and ClassNotFoundException?

What is the Difference Between NoClassDefFoundError and ClassNotFoundException? When NoClassDefFoundError and ClassNotFoundException are Thrown NoClassDefFoundError and ClassNotFoundException are very closely related and often confused with each other by many developers. Below is the description of each from the Java API Specifications ClassNotFoundException Thrown when an application tries to load in a class through its string … Read more

How do I Check the CLASSPATH Variable is set in My Machine?

Checking CLASSPATH on Windows To check CLASSPATH variable is set on Microsoft Windows, run following command on command prompt C:> echo %CLASSPATH% If CLASSPATH variable is not set you will see %CLASSPATH% on windows system. Checking CLASSPATH on Unix, Linux or Mac To check CLASSPATH variable is set on Unix/Linux/Mac run following command on shell $ … Read more

How to set Java Classpath on Windows, Unix, Linux and Mac

Setting CLASSPATH on Windows XP Right-click My Computer, and then click Properties. Click the Advanced tab. Click Environment variables. Click one the following options, for either a user or a system variable Click New to add a new variable name and value. Enter Variable name as CLASSPATH enter all directories and jar files separated by … Read more