我有一個Applet的方法從瀏覽器中的JavaScript函數調用。此方法最終會將文件發送到Amazon S3存儲桶。但是,如果此方法嘗試創建AmazonS3Client,則該方法將失敗,但不會顯示任何錯誤。Java AmazonS3Client構造函數失敗
方法中的代碼的相關片段是:
InputStream input = DigitalLive.class.getResourceAsStream("AwsCredentials.properties");
System.out.println("File loaded into Input Stream.");
PropertiesCredentials theCredentials = new PropertiesCredentials(input);
System.out.println("Credentials created.");
System.out.println("AccessKey is:" + theCredentials.getAWSAccessKeyId());
System.out.println("SecretKey is:" + theCredentials.getAWSSecretKey());
//All of the above strings print to the console with the correct AccessKey and SecretKey
try{
AmazonS3 s3Client = new AmazonS3Client(theCredentials);
//This message never prints
System.out.println("Client created.");
} catch(Exception e) {
//This message never prints
System.err.println("Error creating AmazonClient: " + e.getMessage());
}
在try/catch塊中的消息沒有以往打印。這個方法的代碼其餘部分(這裏爲了簡潔起見沒有顯示)也沒有被觸發。
該jar文件編譯沒有錯誤。我使用jdk 1.7.0_51和amazon aws-java-sdk-1.7.1。我使用Eclipse作爲IDE,並使用ANT構建具有構建文件的jar。
我不太確定還有什麼其他嘗試,而不是將代碼包裝在try/catch塊中。 有沒有不同的/更好的方式來調試Java代碼,以瞭解爲什麼不創建AmazonS3Client?或者,考慮到上面的代碼片段,它可能會失敗的一個更明顯的原因?
編輯:我添加了調用Java方法的JavaScript代碼try/catch塊,它拋出一個錯誤:
Error calling method on NPObject
Error: java.lang.reflect.InvocationTargetException
我想,這是什麼告訴我,在Java Applet其實拋出錯誤。我只是不確定如何找出哪一個以及如何解決它。我試圖在AmazonS3 s3Client = new AmazonS3Client(theCredentials);
周圍放置try/catch塊,但IDE告訴我Unreachable catch block for InvocationTargetException. This exception is never thrown from the try statement body
。
您是否嘗試過使用類而不是接口(AmazonS3Client而不是AmazonS3)? AmazonS3Client s3Client = new AmazonS3Client(theCredentials); – sallie
@sallie我剛剛嘗試過,但結果與以前一樣。 – Derrick
在黑暗的建議中的另一個鏡頭 - 嘗試system.out而不是system.err,以防萬一某物正在吃掉你的stderr。我沒有任何明智的建議。只是一個'嘗試一切'的方法。 – sallie