![](/img/trans.png)
[英]java.lang.NoClassDefFoundError: com/google/gson/GsonBuilder error
[英]GSON library error with Lotus Notes Java Agent - java.lang.NoClassDefFoundError: com.google.gson.JsonObject
我正在嘗試使用GSON庫在Lotus Notes應用程序的Java代理中將Java對象轉換為JSON。 我已經將GSON jar文件添加到Project-> Java Build Path。 但是,當我運行代理程序時,出現錯誤-“線程“ AgentThread:JavaAgent” java.lang.NoClassDefFoundError:com.google.gson.JsonObject中的異常“。
基本上,我想要實現的是從外部API獲取一些JSON,然后將其保存在Lotus Notes數據庫中。 我能夠發送HTTP請求,但是作為回報,我得到了一個Java對象。 我想將其轉換為JSON。
這是課程文件,
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import org.apache.commons.validator.routines.UrlValidator;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
public class CustRestConsumer {
/**
* Method for receiving HTTP JSON GET request against a RESTful URL data source.
*
* @param myUrlStr the URL of the REST endpoint
* @return JsonObject containing the data from the REST response.
* @throws IOException
* @throws MalformedURLException
* @throws ParseException
*/
public static JsonObject GetMyRestData( String myUrlStr ) throws IOException, MalformedURLException {
JsonObject myRestData = new JsonObject();
try{
UrlValidator defaultValidator = new UrlValidator();
if(defaultValidator.isValid(myUrlStr)){
URL myUrl = new URL(myUrlStr);
URLConnection urlCon = myUrl.openConnection();
urlCon.setConnectTimeout(5000);
InputStream is = urlCon.getInputStream();
InputStreamReader isR = new InputStreamReader(is);
BufferedReader reader = new BufferedReader(isR);
StringBuffer buffer = new StringBuffer();
String line = "";
while( (line = reader.readLine()) != null ){
buffer.append(line);
}
reader.close();
JsonParser parser = new JsonParser();
myRestData = (JsonObject) parser.parse(buffer.toString());
return myRestData;
}else{
myRestData.addProperty("error", "URL failed validation by Apache Commmons URL Validator");
return myRestData;
}
}catch( MalformedURLException e ){
e.printStackTrace();
myRestData.addProperty("error", e.toString());
return myRestData;
}catch( IOException e ){
e.printStackTrace();
myRestData.addProperty("error", e.toString());
return myRestData;
}
}
}
我在這里調用函數
import com.google.gson.JsonObject;
import lotus.domino.*;
public class JavaAgent extends AgentBase {
public void NotesMain() {
try {
Session session = getSession();
AgentContext agentContext = session.getAgentContext();
// (Your code goes here)
Database db = agentContext.getCurrentDatabase();
String url = "https://jsonplaceholder.typicode.com/todos/1";
System.out.println("Reached Here - 1");
JsonObject myStuff = CustRestConsumer.GetMyRestData(url);
System.out.println("Reached Here - 2");
System.out.println(myStuff);
Document newNotesDoc = db.createDocument();
newNotesDoc.replaceItemValue("Form", "IBMForm");
// newNotesDoc.replaceItemValue("WebPageUS", dto.title);
newNotesDoc.computeWithForm(true, false);
newNotesDoc.save(true, true);
db.recycle();
} catch(Exception e) {
e.printStackTrace();
}
}
}
這是Java調試控制台的輸出,
Reached Here - 1
Exception in thread "AgentThread: JavaAgent" java.lang.NoClassDefFoundError: com.google.gson.JsonObject
at CustRestConsumer.GetMyRestData(Unknown Source)
at JavaAgent.NotesMain(Unknown Source)
at lotus.domino.AgentBase.runNotes(Unknown Source)
at lotus.domino.NotesThread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: com.google.gson.JsonObject
at lotus.domino.AgentLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(ClassLoader.java:852)
我是Java代理的新手,因此非常感謝您的幫助。 謝謝。
Project-> Java Build Path用於在XPages中使用JAR文件。 我相信可以將jar文件直接添加到代理或腳本庫中,但是分離它們存在內存泄漏問題,因此不建議這樣做。 推薦的方法是將它們添加到服務器以及可能正在嘗試編譯代碼的客戶端的jvm \\ lib \\ ext中。
在Domino中還有其他用於調度Java代碼的選項, https://www.intec.co.uk/tag/xots-microservice-scheduler-tutorial/涵蓋了其中的一些選項,但是還有其他一些可行的方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.