Rabu, 18 Agustus 2010

How Java Recognizes Where Things Are Internally

I've always wondered how Java internally recognizes where to find its core classes or jars, where to find its extension classes or jars, and what we need to do to make Java do find these libraries. This is before you use CLASSPATH environment variable or -classpath argument option of executing 'java' command.

Surprisingly I cannot even find this information on Sun's website. Want to share your frustrations? Anyway here's the answer.

The Java launcher uses the system property called 'sun.boot.class.path' to determine where to find classes. This property is set in part when you installed Java, and in part when you run a Java program. You don't need to know the nitty gritty details. Just remember: After you install Java JDK or JRE, put Java's bin/ directory path in your system's environment variable called 'path' to make Java load its core classes and extension classes automatically.

For example, I am running Windows 7 on a 64-bit machine, and the default bin directory of Java is C:\Program Files (x86)\Java\jdk1.6.0_20\bin\. I add this path in my environment variables. Here's a screen shot of the 'Environment Variables' window on my Windows 7 desktop:

Java Bin Directory Included In Environment Variable Path

As you can see the highlighted row corresponds to variable 'path' and its first value is C:\Program Files (x86)\Java\jdk1.6.0_20\bin. I have other values and therefore I delimit them with semi-colons (;).

If you are using Linux or some other operating system simply use a search engine to find the way to change your environment variables! Questions?

Once you've done updating the environment variable, Java automatically looks at C:\Program Files (x86)\Java\jdk1.6.0_20\jre\lib\ext\ for extension jars, and Java automatically looks at C:\Program Files (x86)\Java\jdk1.6.0_20\lib\ and C:\Program Files (x86)\Java\jdk1.6.0_20\jre\lib\ for core jars such as rt.jar.

That means if you want to use any third party jars simply drop them in C:\Program Files (x86)\Java\jdk1.6.0_20\jre\lib\ext\ and everything should work as expected. Questions?


Incidentally if you see NoClassDefFoundError while running a Java program do “java -Verbose ” to see the classes that JVM has loaded so you get an idea if it picks up the jars you put in the ext\ folder. Any questions let me know!

0 komentar:

 
support by: infomediaku.com