0

I have a JavaFX project with two classes and one external library. When I run it from IDE (IntelliJ Idea) - everything works well. But when i run it from command line using command:

java Main

the program starts, but when I try to use the functionality associated with the external library, i have an exception:

Exception in thread "Thread-4" java.lang.NoClassDefFoundError: com/google/gson/Gson

Why does everything work well when I run it through IDE, and when I run it through the console, it stops seeing the library Gson?

2 Answers 2

1

When you run your program from your IDE, like IntelliJ or eclpse, it's probably including the external jars automatically to it's own the classpath for you, depending on your setup.

If you want to run it from the command line you'll need to add the gson jar to your classpath using the -classpath argument.

java -classpath external-library.jar Main

Reference:

Windows - https://docs.oracle.com/javase/8/docs/technotes/tools/windows/classpath.html

Unix - https://docs.oracle.com/javase/8/docs/technotes/tools/unix/classpath.html

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! My command is: java -classpath "path_to_library.jar;path_to_class\" Main
1

Most common reason will be that your Gson library isn't exported when you create your jar.

When the IDE runs your code it can find it. But when you export it to a JAR and it doesn't contain the relevant dependencies, you will get this error.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.