Issue
I'm trying to run this java application with maven from command line on ubuntu with OpenJDK 13
openjdk version "13.0.2" 2020-01-14
OpenJDK Runtime Environment AdoptOpenJDK (build 13.0.2+8)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 13.0.2+8, mixed mode, sharing)
The project is created with Intellij Idea.
I guess I've made the build successfully with these commands
git clone https://github.com/danvega/httpclient-tutorial.git
cd httpclient-tutorial
mvn package
However, I don't know how to run the application from command line.
I tried these commands
cd target/classes
java dev.danvega.Application
and got this error
Error: Unable to initialize main class dev.danvega.Application
Caused by: java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/type/TypeReference
What am I missing?
Solution
Here, you have maven project which has one dependency on jackson-databind
which in turn will have some more dependencies i.e jackson-core
and jackson-annotations
.
Classes from these dependencies are not bundled in your application jar, so you cannot just run the Application
main class from your project directly using java
command, you need to specify the dependent classes on java classpath so that java can load these dependent classes of your program.
Since, it is a maven project, these dependent jars
will be pulled into maven
default directory (.m2
) into your's home path and as you mentioned, you are using ubuntu that will be /home/<your username>/
, For example your username which you are logged in with is singularli
then your home path must be /home/singularli
, you can also check it with echo $HOME
command.
So, you would find the maven folder, which stores all the jar(s), into your home /home/singularli/.m2/repository
, now here you would find jars like jackson-databind
, jackson-core
(these will be little inside subdirectories, as it keeps according to the package name, given below command example will give you more idea about it).
At last, once you find these jars
, you would need to specify the classpath using -cp
flag and include these jars with your application jar which would look like as given below:
java -cp "target/httpclient-tutorial-1.0-SNAPSHOT.jar:/home/singularli/.m2/repository/com/fasterxml/jackson/core/jackson-core/2.11.4/jackson-core-2.11.4.jar:/home/singularli/.m2/repository/com/fasterxml/jackson/core/jackson-databind/2.11.4/jackson-databind-2.11.4.jar:/home/singularli/.m2/repository/com/fasterxml/jackson/core/jackson-annotations/2.11.4/jackson-annotations-2.11.4.jar" dev.danvega.Application
It should work the same way as shown in that video, you referred in your question.
Please notice that you may have different versions i.e com/fasterxml/jackson/core/jackson-annotations/2.11.4, I included 2.11.4 as an example, you may check the version in this project and include that, if different versions are there and you included anyone of them, it may cause some issue as some feature used in this project might not be present in that version
Answered By - code_mechanic