2016-03-19 70 views
-1

您好,我在嘗試將對象轉換爲JSON字符串時遇到以下異常。我不確定爲何顯示此錯誤?Java異常com/fasterxml/jackson/core/util/Instantiatable

String json = ""; 
    ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter(); 
    try 
    { 
     // Convert our object to a string 
     json = ow.writeValueAsString(obj); <----Exception throwing code 
    } 
    catch (JsonProcessingException e) 
    { 
     ConsoleLog.DisplayToConsole("Exception: parse JSON: " + e.getMessage()); 
    } 

我已經加入了傑克遜庫的:

public class AccData extends AbstractData 
{ 
private static ArrayList<Float> Acc_For = new ArrayList<Float>(); 
private static ArrayList<Float> Acc_Side = new ArrayList<Float>(); 
private static ArrayList<Float> Acc_Up = new ArrayList<Float>(); 


public static ArrayList<Float> getAccFor() 
{ 
    return Acc_For; 
} 

public static ArrayList<Float> getAcSide() 
{ 
    return Acc_Side; 
} 

etc 

堆棧跟蹤:我想嘗試轉換到JSON

Lib Dir

對象定義

Exception in thread "main" java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/util/Instantiatable 
at com.fasterxml.jackson.databind.ObjectWriter._configureJsonGenerator(ObjectWriter.java:729) 
at com.fasterxml.jackson.databind.ObjectWriter._configAndWriteValue(ObjectWriter.java:601) 
at com.fasterxml.jackson.databind.ObjectWriter.writeValueAsString(ObjectWriter.java:530) 
at controller.JSONController.ObjectToJSON(JSONController.java:35) 
at controller.MongoDBController.generateJSONStrings(MongoDBController.java:64) 
at view.ChartWindow$1.widgetSelected(ChartWindow.java:164) 
at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:248) 
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84) 
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1057) 
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4170) 
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3759) 
at view.ChartWindow.main(ChartWindow.java:186) 
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.core.util.Instantiatable 
at java.net.URLClassLoader.findClass(Unknown Source) 
at java.lang.ClassLoader.loadClass(Unknown Source) 
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) 
at java.lang.ClassLoader.loadClass(Unknown Source) 

我刪除了jackson-annotations.jar文件夾並添加了更新的版本。但現在我得到這個錯誤:

java.lang.NoClassDefFoundError: com/fasterxml/jackson/annotation/JsonAutoDetect

這裏時拋出(如上):

ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter(); 

堆棧跟蹤:

Exception in thread "main" java.lang.NoClassDefFoundError: com/fasterxml/jackson/annotation/JsonAutoDetect 
at com.fasterxml.jackson.databind.introspect.VisibilityChecker$Std.<clinit>(VisibilityChecker.java:172) 
at com.fasterxml.jackson.databind.ObjectMapper.<clinit>(ObjectMapper.java:195) 
at controller.JSONController.ObjectToJSON(JSONController.java:32) 
at controller.MongoDBController.generateJSONStrings(MongoDBController.java:64) 
at view.ChartWindow$1.widgetSelected(ChartWindow.java:164) 
at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:248) 
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84) 
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1057) 
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4170) 
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3759) 
at view.ChartWindow.main(ChartWindow.java:186) 
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.annotation.JsonAutoDetect 
at java.net.URLClassLoader.findClass(Unknown Source) 
at java.lang.ClassLoader.loadClass(Unknown Source) 
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) 
at java.lang.ClassLoader.loadClass(Unknown Source) 
... 11 more 
+0

您尚未發佈堆棧跟蹤。請發佈完整的堆棧跟蹤(包含所有「引起來的」部分),並在代碼中標識引發異常的行。 –

+3

你應該使用像maven這樣的真正的依賴關係管理器,而不是手動創建一個「lib」文件夾 –

+0

查看jackson-databind的依賴關係,它依賴於jackson-annotations以及jackson-core http://search.maven .ORG /#artifactdetails%7Ccom.fasterxml.jackson.core%7Cjackson-數據綁定%7C2.7.3%7Cbundle –

回答

2

我認爲你是包括傑克遜的錯版核心。

我從Maven Central下載了屏幕截圖中顯示的Jackson Core 2.0.0-RC1的JavaDoc。

http://search.maven.org/#artifactdetails%7Ccom.fasterxml.jackson.core%7Cjackson-core%7C2.0.0-RC1%7Cbundle

的com.fasterxml.jackson.core.util包有沒有丟失的實例化的類。

JavaDoc for the latest version does。

https://fasterxml.github.io/jackson-core/javadoc/2.7/com/fasterxml/jackson/core/util/Instantiatable.html

的NoClassDefFoundError的異常,當被引用類的不能在運行時類路徑中找到被拋出。 com.fasterxml.jackson.databind.ObjectWriter類嘗試加載缺少的類。所以com.fasterxml.jackson.databind.jar是針對不同版本的Jackson Core構建的。

我會嘗試用Maven Central中的Jackson Core,Annotation和Databind jars的最新版本替換您的jar。