Java.lang.NoClassDefFoundError: org/apache/chemistry/opencmis/client/api/SessionFactory 这个错误意味着在运行Java程序时,找不到org/apache/chemistry/opencmis/client/api/SessionFactory类。

9

我使用的是Alfresco Community 4.0。

我使用CMIS更新了在Alfresco中的一个文档。

我已经在Alfresco注册了一个文档,这是保存方法后检索到的文档ID:b08e8bce-1b88-489e-a357-1e6385f180a1

现在我想用其他内容更改此文件的内容。 我使用了这个方法:

   public void saveVersioning(File file, String filename, String userName, String pwd, String docId)
        throws Exception {


        SessionFactory factory = SessionFactoryImpl.newInstance();
        Map<String, String> parameters = new HashMap<String, String>();

        // User credentials.
        parameters.put(SessionParameter.USER,userName);
        parameters.put(SessionParameter.PASSWORD, pwd);

        // Connection settings.
        parameters.put(SessionParameter.BINDING_TYPE, BindingType.ATOMPUB.value());

        parameters.put(SessionParameter.ATOMPUB_URL, "http://localhost:9080/alfresco/service/cmis"); // URL to your CMIS server.

        // Create session.
        // Alfresco only provides one repository.
        Repository repository = factory.getRepositories(parameters).get(0);

        Session session = repository.createSession();

        System.out.println("Connected to repository:" + session.getRepositoryInfo().getName());
        System.out.println("Repository id:"+session.getRepositoryInfo().getId());

         // Get the contents of the file
        Document doc = (Document) session.getObject(docId);
        ContentStream contentStream = doc.getContentStream(); // returns null if the document has no content
        if (contentStream != null) {


                String mimetype = "text/plain; charset=UTF-8";
                String content = "";

                if (contentStream != null) {
                    filename = contentStream.getFileName();
                    mimetype = contentStream.getMimeType();
                    content = getContentAsString(contentStream);
                    System.out.println("file name "+filename);
                    System.out.println("minetype "+mimetype);
                    System.out.println("content "+content);
                }



                String updatedContents = content + "\nLine added in new version";

                byte[] buf = updatedContents.getBytes("UTF-8");
                ByteArrayInputStream input = new ByteArrayInputStream(buf);


                contentStream = session.getObjectFactory().createContentStream(
                        filename, buf.length, mimetype, input);



                System.out.println("Document version history");
                {
                    List<Document> versions = doc.getAllVersions();
                    for (Document version : versions) {
                        System.out.println("\tname: " + version.getName());
                        System.out.println("\tversion label: " + version.getVersionLabel());
                        System.out.println("\tversion series id: " + version.getVersionSeriesId());
                        System.out.println("\tchecked out by: "
                                + version.getVersionSeriesCheckedOutBy());
                        System.out.println("\tchecked out id: "
                                + version.getVersionSeriesCheckedOutId());
                        System.out.println("\tmajor version: " + version.isMajorVersion());
                        System.out.println("\tlatest version: " + version.isLatestVersion());
                        System.out.println("\tlatest major version: " + version.isLatestMajorVersion());
                        System.out.println("\tcheckin comment: " + version.getCheckinComment());
                        System.out.println("\tcontent length: " + version.getContentStreamLength()
                                + "\n");
                    }
                }
            }



    }

我将使用以下代码调用此方法:
  File file = new File("C:/test.pdf");
       saveVersioning(file, "test.pdf", "admin","admin","b08e8bce-1b88-489e-a357-1e6385f180a1");

我在我的Java项目中使用了这个jar包:

enter image description here

所有的jar包都已经添加到了类路径中,但是当我测试我的应用程序时,我遇到了以下错误:
Caused by: java.lang.NoClassDefFoundError: org/apache/chemistry/opencmis/client/api/SessionFactory

    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:107)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
    at com.sun.proxy.$Proxy24.sendTransfer(Unknown Source)
    at 
    ... 111 more
Caused by: java.lang.ClassNotFoundException: org.apache.chemistry.opencmis.client.api.SessionFactory
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1387)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1233)
    ... 124 more

我的 Alfresco Community 4 服务器包含这个 jar 文件:

enter image description here

更新:

我发现在 Alfresco Community 4.0.0 版本中,我应该使用 chemistry-opencmis-client 0.6.0alfresco-opencmis-extension-0.2.jar

此外,在我的代码中,我应该使用这个 URL:http://localhost:9080/alfresco/cmisatom

我尝试使用以下代码获取 cmis 会话,但没有成功:

Map<String, String> parameter = new HashMap<String, String>();

// user credentials
        parameter.put(SessionParameter.USER, "admin");
        parameter.put(SessionParameter.PASSWORD, "admin");

        // connection settings
    parameter.put(SessionParameter.ATOMPUB_URL, "http://localhost:9080/alfresco/cmisatom");

    parameter.put(SessionParameter.BINDING_TYPE, BindingType.ATOMPUB.value());

    // create session
    SessionFactory factory = SessionFactoryImpl.newInstance();
        Session session = factory.getRepositories(parameter).get(0).createSession();

如我所说,我使用了这个jar包:

enter image description here

我从chemistry-opencmis-client-impl-0.6.0-with-dependencies.tar.gz下载了所有的库,并且我还下载了这个jar包alfresco-opencmis-extension-0.2.jar。

当我测试时,我遇到了相同的错误。

我在我的代码中添加了这一行:

parameter.put(SessionParameter.OBJECT_FACTORY_CLASS, "org.alfresco.cmis.client.impl.AlfrescoObjectFactoryImpl");

我认为这个问题与缺少jar包版本有关,而不是我的Java代码有问题。但是我尝试更改这一行,但没有成功:

parameter.put(SessionParameter.ATOMPUB_URL, "http://localhost:9080/alfresco/cmisatom");

使用:

parameter.put(SessionParameter.ATOMPUB_URL, "http://localhost:9080/alfresco/service/cmis");

使用Maven吧!它会让事情变得“不那么复杂” :) XD - Tilak Madichetti
2个回答

2

我不确定这是否是正确的答案,但希望您能找到错误并获取一些提示。

  1. 首先下载最新的CMIS Workbench或下载0.6.0版本
  2. 使用Workbench连接到Alfresco检查是否一切正常,例如创建/读取/更新/删除
  3. 阅读关于CMIS的大量信息在此处和其他许多地方..
  4. 在你阅读的时候,你会希望你在一个比4.0更高的Alfresco版本上。如果您需要稳定的CMIS环境,则应升级到4.2.f甚至更高版本。
  5. 放弃当前的Java项目,从下载页面下载带依赖项的0.6.0
  6. 到达“实际”读取和保存文件的点。
  7. 在您当前的设置中,无法在Alfresco上保存文件,也不会出现NoClassDefFoundError,并在更新时出现错误。要么您总是在构建时出现错误,要么您会遇到其他错误。
  8. 将sessionFactory和create session代码移动到公共的init()或setupConnection()方法中,以便您不重复编写代码,从而有更多犯错的机会

0

我会假设你使用的测试类路径与截图上显示的不同。

Caused by: java.lang.ClassNotFoundException: org.apache.chemistry.opencmis.client.api.SessionFactory
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1387)

你在Tomcat容器中运行测试吗?启动测试时,请仔细检查类路径。如果类路径确实具有CMIS依赖项,则请检查CMIS客户端版本冲突。所有冲突版本都可以通过Maven(例如)轻松解决。我建议将项目创建为Maven项目以避免此问题。此外,当您运行测试时,Maven将包含所有CMIS依赖项在类路径中。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接