{"id":379,"date":"2007-10-21T00:00:00","date_gmt":"2007-10-21T00:00:00","guid":{"rendered":"http:\/\/www.strongd.net\/?p=379"},"modified":"2007-10-21T00:00:00","modified_gmt":"2007-10-21T00:00:00","slug":"Installed Extensions","status":"publish","type":"post","link":"https:\/\/www.strongd.net\/?p=379","title":{"rendered":"Installed Extensions"},"content":{"rendered":"<p><DIV id=PageTitle>Installed Extensions<\/DIV><br \/>\n<BLOCKQUOTE>Installed extensions are JAR files in the <TT>lib\/ext<\/TT> directory of the Java Runtime Environment (JRE<FONT size=-2><SUP>TM<\/SUP><\/FONT>) software. As its name implies, the JRE is the runtime portion of the Java Development Kit containing the platform&#8217;s core API but without development tools such as compilers and debuggers. The JRE is available either by itself or as part of the Java Development Kit.<br \/>\n<P>As of the Java 1.2 platform, the JRE is a strict subset of the JDK<FONT size=-2><SUP>TM<\/SUP><\/FONT> software. A subset of the JDK software directory tree looks like this: <\/P><br \/>\n<P><\/P><br \/>\n<CENTER><IMG height=234 alt=\"\" src=\"http:\/\/java.sun.com\/docs\/books\/tutorial\/figures\/ext\/extb1.gif\" width=392 align=bottom><\/CENTER><br \/>\n<P><\/P><br \/>\n<P>The JRE consists of those directories within the highlighted box in the diagram. Whether your JRE is stand-alone or part of the JDK software, any JAR file in the <TT>lib\/ext<\/TT> of the JRE directory is automatically treated by the runtime environment as an extension. <\/P><br \/>\n<P>Since installed extensions extend the platform&#8217;s core API, use them judiciously. They are rarely appropriate for interfaces used by a single, or small set of applications. <\/P><br \/>\n<P>Furthermore, since the symbols defined by installed extensions will be visible in all Java processes, care should be taken to ensure that all visible symbols follow the appropriate &#8220;reverse domain name&#8221; and &#8220;class hierarchy&#8221; conventions. For example, <TT>com.mycompany.MyClass<\/TT>. <\/P><br \/>\n<P>As of Java 6, extension JAR files may also be placed in a location that is independent of any particular JRE, so that extensions can be shared by all JREs that are installed on a system. Prior to Java 6, the value of <TT>java.ext.dirs<\/TT> referred to a single directory, but as of Java 6 it is a list of directories (like <TT>CLASSPATH<\/TT>) that specifies the locations in which extensions are searched for. The first element of the path is always the <TT>lib\/ext<\/TT> directory of the JRE. The second element is a directory outside of the JRE. This other location allows extension JAR files to be installed once and used by several JREs installed on that system. The location varies depending on the operating system: <\/P><br \/>\n<UL><br \/>\n<LI>Solaris<FONT size=-2><SUP>TM<\/SUP><\/FONT> Operating System: <TT>\/usr\/jdk\/packages\/lib\/ext<\/TT><\/LI><br \/>\n<LI>Linux: <TT>\/usr\/java\/packages\/lib\/ext<\/TT><\/LI><br \/>\n<LI>Microsoft Windows: <TT>%SystemRoot%\\Sun\\Java\\lib\\ext<\/TT><\/LI><\/UL><br \/>\n<P>Note that an installed extension placed in one of the above directories extends the platform of <EM>each<\/EM> of the JREs (Java 6 or later) on that system. <\/P><br \/>\n<P><\/P><\/BLOCKQUOTE><br \/>\n<H3>A Simple Example<\/H3><br \/>\n<BLOCKQUOTE>Let&#8217;s create a simple installed extension. Our extension consists of one class, <TT>RectangleArea<\/TT>, that computes the areas of rectangles:<br \/>\n<BLOCKQUOTE><PRE>public final class RectangleArea {<br \/>\n    public static int area(java.awt.Rectangle r) {<br \/>\n        return r.width * r.height;<br \/>\n    }<br \/>\n}<br \/>\n<\/PRE><\/BLOCKQUOTE>This class has a single method, <TT>area<\/TT>, that takes an instance of <TT>java.awt.Rectangle<\/TT> and returns the rectangle&#8217;s area.<br \/>\n<P>Suppose that you want to test <TT>RectangleArea<\/TT> with an application called <CODE>AreaApp<\/CODE>: <\/P><br \/>\n<BLOCKQUOTE><PRE>import java.awt.*;<\/p>\n<p>public class AreaApp {<br \/>\n    public static void main(String[] args) {<br \/>\n        int width = 10;<br \/>\n        int height = 5;<\/p>\n<p>        Rectangle r = new Rectangle(width, height);<br \/>\n        System.out.println(&#8220;The rectangle&#8217;s area is &#8221;<br \/>\n                           + RectangleArea.area(r));<br \/>\n    }<br \/>\n}<br \/>\n<\/PRE><\/BLOCKQUOTE>This application instantiates a 10 <VAR>x<\/VAR> 5 rectangle, and then prints out the rectangle&#8217;s area using the <TT>RectangleArea.area<\/TT> method.<br \/>\n<P><\/P><br \/>\n<H3>Running AreaApp Without the Extension Mechanism<\/H3>Let&#8217;s first review how you would run the <CODE>AreaApp<\/CODE> application without using the extension mechanism. We&#8217;ll assume that the <TT>RectangleArea<\/TT> class is bundled in a JAR file named <TT>area.jar<\/TT>.<br \/>\n<P>The <TT>RectangleArea<\/TT> class is not part of the Java platform, of course, so you would need to place the <TT>area.jar<\/TT> file on the class path in order to run <CODE>AreaApp<\/CODE> without getting a runtime exception. If <TT>area.jar<\/TT> was in the directory <TT>\/home\/user<\/TT>, for example, you could use this command: <\/P><br \/>\n<BLOCKQUOTE><PRE>java -classpath .:\/home\/user\/area.jar AreaApp<br \/>\n<\/PRE><\/BLOCKQUOTE>The class path specified in this command contains both the current directory, containing <TT>AreaApp.class<\/TT>, and the path to the JAR file containing the <TT>RectangleArea<\/TT> package. You would get the desired output by running this command:<br \/>\n<BLOCKQUOTE><PRE>The rectangle&#8217;s area is 50<br \/>\n<\/PRE><\/BLOCKQUOTE><br \/>\n<P><\/P><br \/>\n<H3>Running AreaApp by Using the Extension Mechanism<\/H3>Now let&#8217;s look at how you would run <CODE>AreaApp<\/CODE> by using the <TT>RectangleArea<\/TT> class as an extension.<br \/>\n<P>To make the <TT>RectangleArea<\/TT> class into an extension, you place the file <TT>area.jar<\/TT> in the <TT>lib\/ext<\/TT> directory of the JRE. Doing so automatically gives the <TT>RectangleArea<\/TT> the status of being an installed extension. <\/P><br \/>\n<P>With <TT>area.jar<\/TT> installed as an extension, you can run <CODE>AreaApp<\/CODE> without needing to specify the class path: <\/P><br \/>\n<BLOCKQUOTE><PRE>java AreaApp<br \/>\n<\/PRE><\/BLOCKQUOTE><br \/>\n<P>Because you&#8217;re using <TT>area.jar<\/TT> as an installed extension, the runtime environment will be able to find and to load the <CODE>RectangleArea<\/CODE> class even though you haven&#8217;t specified it on the class path. Similarly, any applet or application being run by any user on your system would be able to find and use the <TT>RectangleArea<\/TT> class. <\/P><br \/>\n<P>If there are multiple JREs (Java 6 or later) installed on a system and want the <TT>RectangleArea<\/TT> class to be available as an extension to all of them, instead of installing it in the <TT>lib\/ext<\/TT> directory of a particular JRE, install it in the system-wide location. For example, on system running Linux, install <TT>area.jar<\/TT> in the directory <TT>\/usr\/java\/packages\/lib\/ext<\/TT>. Then <TT>AreaApp<\/TT> can run using different JREs that are installed on that system, for example if different browsers are configured to use different JREs. <\/P><\/BLOCKQUOTE><br \/>\n<DIV><\/DIV><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Installed Extensions Installed extensions are JAR files in the lib\/ext directory of the Java Runtime Environment (JRETM) software. As its name implies, the JRE is the runtime portion of the Java Development Kit containing the platform&#8217;s core API but without development tools such as compilers and debuggers. The JRE is available either by itself or &hellip; <a href=\"https:\/\/www.strongd.net\/?p=379\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Installed Extensions<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-379","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/379","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=379"}],"version-history":[{"count":0,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/379\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=379"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=379"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=379"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}