OSGI expose An “ClassNotFoundException: org.w3c.dom.***” Error when release

流过昼夜 提交于 2019-12-19 03:42:17

问题


I only wrote the following codes in Activator.start() function

    public void start(BundleContext bundleContext) throws Exception {
    Activator.context = bundleContext;
    Node node = new Node() {

        @Override
        public Object setUserData(String arg0, Object arg1, UserDataHandler arg2) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public void setTextContent(String arg0) throws DOMException {
            // TODO Auto-generated method stub

        }

        @Override
        public void setPrefix(String arg0) throws DOMException {
            // TODO Auto-generated method stub

        }

        @Override
        public void setNodeValue(String arg0) throws DOMException {
            // TODO Auto-generated method stub

        }

        @Override
        public Node replaceChild(Node arg0, Node arg1) throws DOMException {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public Node removeChild(Node arg0) throws DOMException {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public void normalize() {
            // TODO Auto-generated method stub
            System.out.println("normalize 方法调用");

        }

        @Override
        public String lookupPrefix(String arg0) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public String lookupNamespaceURI(String arg0) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public boolean isSupported(String arg0, String arg1) {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public boolean isSameNode(Node arg0) {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public boolean isEqualNode(Node arg0) {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public boolean isDefaultNamespace(String arg0) {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public Node insertBefore(Node arg0, Node arg1) throws DOMException {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public boolean hasChildNodes() {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public boolean hasAttributes() {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public Object getUserData(String arg0) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public String getTextContent() throws DOMException {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public Node getPreviousSibling() {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public String getPrefix() {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public Node getParentNode() {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public Document getOwnerDocument() {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public String getNodeValue() throws DOMException {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public short getNodeType() {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public String getNodeName() {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public Node getNextSibling() {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public String getNamespaceURI() {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public String getLocalName() {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public Node getLastChild() {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public Node getFirstChild() {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public Object getFeature(String arg0, String arg1) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public NodeList getChildNodes() {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public String getBaseURI() {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public NamedNodeMap getAttributes() {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public short compareDocumentPosition(Node arg0) throws DOMException {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public Node cloneNode(boolean arg0) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public Node appendChild(Node arg0) throws DOMException {
            // TODO Auto-generated method stub
            return null;
        }
    };
    node.normalize();
}

Everything goes well when run in eclipse environment, but, when release the product, ERRORS in log when runs:

Root exception: java.lang.NoClassDefFoundError: org/w3c/dom/Node

Caused by: java.lang.ClassNotFoundException: org.w3c.dom.Node

Anyone can give some help?


回答1:


OSGi gives access to system packages but only java.* packages by default, this does not include other packages like: javax.net , javax.xml , com.sun

Thus it is necessary to specify any of such packages for OSGi framework to export them through the system bundle making them accessible to other bundles that import them.

To do that you need to set a configuration property with the additional packages required by your bundles, try setting it as a system property before starting the OSGi framework such that it picks up this property when it first starts.

Assuming you are on OSGi 4.2, that property would be configured like:

org.osgi.framework.system.packages.extra=org.w3c.dom

You may want to check the Apache Felix Framework Configuration Properties for more details, though this property is part of the OSGi spec and thus should be available in other implementations as well




回答2:


Please update your question to include the bundle's MANIFEST.MF

It looks like org.w3c.dom is not implicitly provided in your production. Check the Import-Package header, may be you don't have Import-Package: org.w3c.dom




回答3:


If you are using Equinox, you can edit the config.ini and add "org.w3c.dom" to org.osgi.framework.system.packages key and import the same packages in your MANIFEST.MF




回答4:


in my case adding

org.osgi.framework.bootdelegation=xx...xxx,org.w3c.dom

solved my problem.



来源:https://stackoverflow.com/questions/8118404/osgi-expose-an-classnotfoundexception-org-w3c-dom-error-when-release

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!