| // |
| // 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. |
| // |
| |
| = I got a `ClassNotFoundException` or `NoClassDefFoundError`. How can I fix it? |
| :page-layout: wikidev |
| :page-tags: wiki, devfaq, needsreview |
| :jbake-status: published |
| :keywords: Apache NetBeans wiki DevFaqTroubleshootClassNotFound |
| :description: Apache NetBeans wiki DevFaqTroubleshootClassNotFound |
| :toc: left |
| :toc-title: |
| :page-syntax: true |
| :page-wikidevsection: _when_things_go_wrong_troubleshooting |
| :page-position: 1 |
| |
| The most likely explanation is that you have a problem in your dependencies. |
| In order for a class in one module to reference a class/interface defined in another module, |
| the following must be true: |
| |
| 1. The class/interface being referenced must be visible to the code using it, according to the normal Java visibility rules. This typically means that the class must be public, since package-private access across modules is impossible. |
| 2. The package containing the class/interface must be _exported_ (marked as providing an API visible to other modules). To "export" package, right click project, select Properties -> API Versioning and choose either public or friend export type. |
| 3. The module containing the code which uses this class/interface must declare a dependency on the module which provides it. |
| |
| These rules are pretty straightforward and it is easy in most cases to verify that dependencies are set up correctly. |
| If you receive a `ClassNotFoundException` or `NoClassDefFoundError` at runtime, the stack trace will generally lead you to the problem. |
| |
| However, there are some cases where you will receive a `ClassNotFoundException` or `NoClassDefFoundError` at runtime, |
| but finding which modules need to declare dependencies on one another is more difficult because the stacktrace does not directly identify the code involved. |
| This occurs most frequently when you have library modules |
| (composed of JAR files which were compiled outside of the platform). |
| Although the dependencies were satisfied (by setting the classpath as needed) when the libraries were compiled, |
| the developer may not have correctly set these dependencies in the platform application which uses them. |
| |
| In this case, you can often locate the problem by rebuilding the suite |
| and paying close attention to the output generated by the `verify-class-linkage` task. |
| For example: |
| |
| [source,java] |
| ---- |
| |
| verify-class-linkage: Warning: a.SomeImplementation cannot access b.publicapi.SomeInterface |
| ---- |
| |
| This tells us that the module which provides `SomeImplementation` needs to declare a dependency on the module which provides `SomeInterface`. |
| |
| For more background, see link:. |