package com.Toby.Trains;
import com.Toby.StationManager;
import com.google.gson.Gson;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Trains {
public static void main(String[] args) {
Gson gson = new Gson();
BufferedReader bufferedReader = null;
try {
bufferedReader = new BufferedReader(new FileReader("routes.json"));
StationManager routes = gson.fromJson(bufferedReader, StationManager.class);
if (routes != null) {
System.out.print(routes);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
I'm trying to use Gson which requires me to put the Gson jar as a dependancy to use it's classes.
To do this, I've set it as a dependancy in the project settings under modules. This allowed me to import the correct classes so it would compile without errors.
This makes the jar show under external libraries.
When I try to run the compiled jar in terminal, it gives java.lang.NoClassDefFoundError
Exception in thread "main" java.lang.NoClassDefFoundError: com/google/gson/Gson
at com.Toby.Trains.Trains.main(Trains.java:16)
Caused by: java.lang.ClassNotFoundException: com.google.gson.Gson
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
It looks like my external libraries aren't being added. How do I run my jar with the correct dependancies?
Thanks!