我正在尝试从Java类AuthenticationService验证到alfresco。下面是代码:
public void Authenticate() throws AuthenticationException {
authenticationService.authenticateAsGuest();
}
问题是我不能运行这个函数,因为它说
No exception of type AuthenticationException can be thrown; an exception type must be a subclass of Throwable
我用的是Alfresco 4.2.
相关类是从:
导入的import org.alfresco.repo.security.authentication.AuthenticationException;
import org.alfresco.service.cmr.security.AuthenticationService;
反编译org.alfresco.repo.security.authentication.AuthenticationException
并检查。
如果你想为你的Java类将要抛出的异常取一个有意义的名字,你可以自己写一个exception类。http://www.java2novice.com/java_exception_handling_examples/create_custom_exception/
如果你真的想使用这个类,但你怀疑你的JVM正在加载错误的,你可以在下面的某个地方部署JSP代码(例如,将其部署在web应用程序的根文件夹中,作为'whereis.jsp'),启动Alfresco服务器,然后检查'whereis.jsp'页面的结果:
<%@ page import="java.security.*" %>
<%@ page import="java.net.URL" %>
<%
Class cls = org.alfresco.repo.security.authentication.AuthenticationException.class;
ProtectionDomain pDomain = cls.getProtectionDomain();
CodeSource cSource = pDomain.getCodeSource();
URL loc = cSource.getLocation();
out.println(loc);
// it should print something like "c:/jars/MyJar.jar"
%>
这将告诉您JVM正在加载的确切jar,它可能有助于您了解发生了什么。
异常说明了一切:你不能仅仅因为类名这么说就抛出这样一个异常。它必须源自Exception
或至少实现Throwable
。
编辑:关于框架,可能类没有被设计成像普通Java异常一样抛出。
您是否有将throws-Statement放在Authenticate
方法上?
编辑2:根据http://dev.alfresco.com/resource/docs/java/datamodel/org/alfresco/repo/security/authentication/AuthenticationException.html,它确实实现了Throwable
。你确定你不是在什么罐子地狱里吗?例如,也许在应用程序容器中运行的是不同的版本?
我解决了这个问题。感谢马塞洛的帖子。首先,我创建了一个类,并进行了测试:
public class main {
/**
* @param args
* @throws AuthenticationFault
*/
public static void main(String[] args) throws AuthenticationFault {
// TODO Auto-generated method stub
Class cls = org.alfresco.repo.security.authentication.AuthenticationException.class;
ProtectionDomain pDomain = cls.getProtectionDomain();
CodeSource cSource = pDomain.getCodeSource();
URL loc = cSource.getLocation();
System.out.println(loc);
}
运行程序后,您将在库中获得缺失的类。这个类由AuthenticationException使用。我得到了错误:
java.lang.ClassNotFoundException: org.alfresco.error.AlfrescoRuntimeException
然后我将包含org.alfresco.error.AlfrescoRuntimeException的alfresco-core.jar添加到我的类路径中。我又开了一次班,一切都很好。
我得到了这个错误很多次,我已经用这个方法修复了它。