blob: e1448634fdda926b13687c50240c851cef202139 [file] [log] [blame]
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//
= DevFaqClassLoaders
:jbake-type: wiki
:jbake-tags: wiki, devfaq, needsreview
:jbake-status: published
:keywords: Apache NetBeans wiki DevFaqClassLoaders
:description: Apache NetBeans wiki DevFaqClassLoaders
:toc: left
:toc-title:
:syntax: true
=== What class loaders are created by the module system?
==== Overview
This FAQ item should be a companion to the main link:http://bits.netbeans.org/dev/javadoc/org-openide-modules/org/openide/modules/doc-files/classpath.html[classpath documentation]. Please refer to the original document for additional details.
==== Class loaders in the NetBeans platform
There are basically three main class loader types used in the platform.
Most code is loaded by module class loaders.
In special cases the "system" class loader can be used,
when you need access to resources from unknown modules.
Resources directly on the classpath from the launch script (mainly `platform*/lib/*.jar`)
are loaded by the application loader.
(There are also bootstrap and extension loaders in the JRE,
and the platform has a special loader for a couple of JARs in `platform*/core/*.jar`.)
Most of the class loaders in the NetBeans platform are multi-parented class loaders.
This means that the class loader can have zero or more parents.
`org.netbeans.ProxyClassLoader` implements the search across multiple parents.
==== Module class loader
Every module loaded by the module system has its own class loader.
This loader loads resources primarily from the module's JAR.
The application loader is an implicit parent of each module loader.
The module loader is able to load from additional JARs (besides delegating to various parents):
* extensions - anything listed in the manifest attribute `Class-Path` of the module JAR
* locale extensions - JARs placed in a subdirectory `locale` relative to the original module JAR position, named by appending a locale suffix to the original name
* patches - JARs placed in a subdirectory `patches/code-name-base` relative to the original JAR position (can override module classes)
The implementation class is `link:http://hg.netbeans.org/main/file/tip/o.n.bootstrap/src/org/netbeans/StandardModule.java[org.netbeans.StandardModule$OneModuleClassLoader]`.
==== System class loader
The "system" loader loads no resources on its own,
but has as its parents all enabled module's class loaders.
It is accessible via `Lookup.getDefault().lookup(ClassLoader.class)`
or by using the fact that it is the context loader on all threads by default: `Thread.currentThread().getContextClassLoader()`
==== Application class loader
This class loader is set up by the launch script (or by `javaws` if running in JNLP mode).
It can load classes from `lib/*.jar` in specified clusters.
It is generally discouraged to use this loader for your own classes,
but it is sometimes needed e.g. for Look & Feel classes
(which must be loaded very early during the startup sequence).
==== Example
Take a very simple module `a`:
[source,java]
----
Manifest-Version: 1.0
OpenIDE-Module: org.netbeans.modules.a
----
and module `b` depending on `a`:
[source,java]
----
Manifest-Version: 1.0
OpenIDE-Module: org.netbeans.modules.b
OpenIDE-Module-Module-Dependencies: org.netbeans.modules.a
Class-Path: ext/library-b-1.1.jar
----
Classes in `org-netbeans-modules-a.jar` will be loaded in `a`'s module class loader.
Classes in both `org-netbeans-modules-b.jar` and `library-b-1.1.jar`
will be loaded in `b`'s module loader,
and can refer to classes in `org-netbeans-modules-a.jar`.
<hr/>
Applies to: NetBeans 6.8 and above
=== Apache Migration Information
The content in this page was kindly donated by Oracle Corp. to the
Apache Software Foundation.
This page was exported from link:http://wiki.netbeans.org/DevFaqClassLoaders[http://wiki.netbeans.org/DevFaqClassLoaders] ,
that was last modified by NetBeans user Rmichalsky
on 2009-12-02T13:43:15Z.
*NOTE:* This document was automatically converted to the AsciiDoc format on 2018-02-07, and needs to be reviewed.