目前,在我的测试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);
,但该行从未引发错误。
如果您能告诉我如何解决这个问题,我将不胜感激:(。