IDE(Eclipseなど)を使用せずに、jacksonライブラリを使用して、jsonのファイルをパースしようと試みている者です。
以下のようなソースコードを作成し、コンパイルも通りましたが、実行してみると、ObjectMapperを初期化するところで、ClassNotFoundが出て落ちてしまいます。どなたか、原因がわかる方、ご教授いただけないでしょうか??
■実行環境
MacOS
$ java -version
java version "1.8.0_121"
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)
$
■コンパイルコマンド
$ javac ./Main.java -cp jackson-databind-2.3.5.jar:jackson-core-2.3.5.jar
■実行ディレクトリ
$ ls -l
total 2224
-rw-r--r-- 1 uratah staff 1816 2 3 16:52 Main.class
-rw-r--r-- 1 uratah staff 1667 2 3 17:08 Main.java
-rw-r--r--@ 1 uratah staff 199671 2 2 17:07 jackson-core-2.3.5.jar
-rw-r--r--@ 1 uratah staff 915717 2 2 13:45 jackson-databind-2.3.5.jar
-rw-r--r-- 1 uratah staff 348 2 2 11:28 test.json
-rw-r--r-- 1 uratah staff 4741 2 2 20:01 test.txt
$
■実行結果
$ java Main test.json
Exception in thread "main" java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/ObjectMapper
at Main.main(Main.java:38)
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.databind.ObjectMapper
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
$
■javaソース
java
1import java.io.File; 2 2 import java.io.FileReader; 3 3 import java.io.BufferedReader; 4 4 import java.io.FileNotFoundException; 5 5 import java.io.IOException; 6 6 import com.fasterxml.jackson.databind.JsonNode; 7 7 import com.fasterxml.jackson.databind.ObjectMapper; 8 8 import java.util.Iterator; 9 9 10 10 public class Main { 11 11 12 12 public static void main(String[] args) { 13 13 14 14 // ファイルを引数で受け取る 15 15 String fileName = args[0]; 16 20 17 21 try { 18 22 19 23 // ファイルを読み込む 20 24 File file = new File(fileName); 21 36 22 37 // ここからjacksonのdatabindを使用してパースする。 23 38 ObjectMapper mapper = new ObjectMapper(); 24 39 25 40 // rootNodeを取得 26 41 JsonNode rootNode = mapper.readValue(file, JsonNode.class); 27 42 28 43 // 29 44 JsonNode current = rootNode.get(1); 30 45 31 46 JsonNode nameNode = current.get("name"); 32 47 // rootノーどで何が取れるか確認して見る。 33 48 Iterator<String> fieldStrings = current.fieldNames(); 34 49 35 50 while (fieldStrings.hasNext()) { 36 51 String fieldString = fieldStrings.next(); 37 52 System.out.println(fieldString); 38 53 } 39 54 40 55 }catch(FileNotFoundException e) { 41 56 42 57 System.out.println("ファイルがないです。"); 43}catch(IOException e) { 44 59 45 60 System.out.println("IOExceptionが出たよ。"); 46 61 } 47 62 48 63 System.out.println(strBuf); 49 64 } 50 67 } 51
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/02/03 08:34
2017/02/03 08:42
2017/02/03 10:52