目前,在我的测试 EJB project
中,我有以下Stateless
bean:
@WebService(serviceName = "TestService")
@Stateless()
public class TestService {
@WebMethod(operationName = "testUpload")
public void testUpload() {
// DUMMY LINE
Log log = LogFactory.getLog(AmazonS3Client.class);
// END DUMMY LINE
File file = new File("path_to_test_file");
AWSCredentials credentials = new BasicAWSCredentials(AMAZON_KEY_ID, AMAZON_SECRET_KEY);
AmazonS3 conn = new AmazonS3Client(credentials);
conn.setEndpoint("https://s3-ap-southeast-1.amazonaws.com");
PutObjectRequest por = new PutObjectRequest(AMAZON_BUCKET_NAME, "test.txt", file);
conn.putObject(por);
}
}
在我的测试 WAR project
中,我有以下ManagedBean
:
@Named(value = "mrBean")
@RequestScoped
public class MrBean {
@WebServiceRef(wsdlLocation = "WEB-INF/wsdl/localhost_8080/TestService/TestService.wsdl")
private TestService_Service service;
private TestService port;
@PostConstruct
public void test() {
port = service.getTestServicePort();
port.testUpload();
}
}
当我部署 EJB project
并运行test.xhtml
页面时,我总是在以下行遇到错误:
AmazonS3 conn = new AmazonS3Client(credentials);
一开始,错误如下所示:
Caused by: java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
at com.amazonaws.services.s3.AmazonS3Client.<clinit>(AmazonS3Client.java:193)
...
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
经过几次试运行后,错误变为:
Caused by: java.lang.NoClassDefFoundError: Could not initialize class com.amazonaws.services.s3.AmazonS3Client
有一些奇怪的事情:
testUpload()
函数并粘贴到ManagedBean
,则函数可以完美地运行。我100%确定我已经在两个项目中导入了所有库。但是,功能testUpload()
只能在 WAR project
内部使用,而不能在 EJB project
内使用。Log log = LogFactory.getLog(AmazonS3Client.class);
,但错误从未被抛出。如果你能告诉我如何解决这个问题,我将非常感激:(。
答案 0 :(得分:0)
要解决此问题,我必须将EJB Project
打包到EAR Project
内。只有当我部署EAR Project
而不是直接部署EJB Project
时,才能正确加载库。
我还没有找到这个问题的原因。如果有人能在可能的情况下给我解释,我将非常感激:)