0
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.

The Modules Menu

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!

Toby
  • 3
  • 2
  • 1
    Do yourself a favor and work with a build system, like Maven or Gradle. All of these integrate nicely with Idea, and they also all support the creation of a "fat JAR", which contains not only your App, but also all libraries you use. In Maven, this would be done with the [maven-shade-plugin](https://maven.apache.org/plugins/maven-shade-plugin/) – Sean Patrick Floyd Mar 06 '17 at 16:36
  • Please [see this answer](http://stackoverflow.com/a/42200519/104891) for the solution. – CrazyCoder Mar 06 '17 at 17:20

1 Answers1

0

As Sean mentioned either user build systems which can help you to manage your dependency, It makes you life easy. Or if you want to use just the jar then instead of keeping your jar to a different location, create a directory (let's say lib) within you project and move the required jar there. Include the created directory as dependency using your IDE which will add it to your classpath.

For intellij (2016.3), go to "Project Settings" --> Libraries --> create a new library using "Java" option and select the directory which you want to use as library. It will give you option if you want to use it as jar directory. Select this option and then it will ask you to select the module you want to use this library for (If you have multi module project). Once selected, it will add the library to your selected module(s).

Rajesh Dwivedi
  • 382
  • 2
  • 13