deploying java web app to heroku: NoClassDefFoundError caused by ClassNotFoundException

914 Views Asked by At

I followed this instruction:

https://devcenter.heroku.com/articles/deploying-java

to deploy a java web app to heroku.

The content of my Procfile is the following:

web: java $JAVA_OPTS -cp target/classes:target/dependency/* com.michael.optimizer.Optimizer

(Optimizer is the name of my main class.)

Executing the command

mvn clean install 

builds the project successfully.

But then executing the command

heroku local web

results in the following error message:

Exception in thread "main" 
5:33:48 PM web.1 |  java.lang.NoClassDefFoundError: javax/json/Json
5:33:48 PM web.1 |  at com.michael.optimizer.api.JsonRequest.doJsonRequest(JsonRequest.java:29)
5:33:48 PM web.1 |      at         com.michael.optimizer.api.StationApi.doJsonRequest(StationApi.java:150)
5:33:48 PM web.1 |      at     com.michael.optimizer.api.StationApi.areaSearch(StationApi.java:73)
5:33:48 PM web.1 |      at com.michael.optimizer.Optimizer.main(Optimizer.java:23)
5:33:48 PM web.1 |  Caused by: java.lang.ClassNotFoundException: javax.json.Json
5:33:48 PM web.1 |      at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
5:33:48 PM web.1 |      at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
5:33:48 PM web.1 |      at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
5:33:48 PM web.1 |      at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
5:33:48 PM web.1 |      ... 4 more

What I don't understand is this: when I run the app locally, everything works fine. (And it's no surprise that it works fine because inside folder ~/.m2/repository/javax/javaee-web-api/7.0/javaee-web-api-7.0.jar there is the json package where it should be.)

Only when I try to deploy to heroku, class javax.json.Json is apparently not found.

What's wrong???

1

There are 1 best solutions below

6
Vikas On BEST ANSWER

You may want to use copy-dependencies,

mvn install dependency:copy-dependencies 

This copy all the dependencies in target/dependencies.

If you want to exclude test scope dependencies,

mvn install dependency:copy-dependencies -DexcludeScope=test