3

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???

steady_progress
  • 3,311
  • 10
  • 31
  • 62
  • Can you verify that, after running `mvn clean install` locally, `javaee-web-api-7.0.jar` is present in your `target/dependency` directory? – Malax Mar 30 '20 at 10:15
  • @Malax: thank you very much for your answer and sorry for the late reply. I checked in folder target ... javaee-web-api-7.0.jar is NOT in that folder after running "mvn clean install" ... how can I change that? – steady_progress Apr 22 '20 at 19:53
  • Can you show the `` section where you defined the JEE API? – dan1st Apr 22 '20 at 20:10

1 Answers1

2

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
Vikas
  • 6,868
  • 4
  • 27
  • 41
  • thank you very much for your answer ... I think this is the solution. Unfortunately, I cannot be sure right now because now - even when running the app locally (which was working without problems a couple of weeks ago) - I get another error message. For that other issue I asked a separate question here: https://stackoverflow.com/questions/61394742/failed-to-execute-goal-org-codehaus-mojoexec-maven-plugin1-5-0exec – steady_progress Apr 23 '20 at 18:52
  • I started a bounty of 500 for that other question (https://stackoverflow.com/questions/61394742/failed-to-execute-goal-org-codehaus-mojoexec-maven-plugin1-5-0exec) – steady_progress Apr 25 '20 at 20:54
  • 1
    @steady_progress added the answer. please check. I hope it helps. – Vikas Apr 26 '20 at 09:46
  • thank you very much for your answer to the other question ... someone else gave the same answer before you so I had to give the bounty to him ... but I will give you additional 250 points for the answer provided above ... I still can't say for sure whether your answer above solves the problem because now I suddenly got a 404 error ... but I approved the answer now so you get 250 points for sure ... once I know the answer solves my problem, you will get 250 points more – steady_progress Apr 26 '20 at 17:29
  • I created a new question regarding the 404 error here: https://stackoverflow.com/questions/61445520/getting-error-message-failed-to-load-resource-the-server-responded-with-a-stat – steady_progress Apr 26 '20 at 18:01
  • No need for the additional bounty. For the other question, it's not the same answer (Root cause explanation, javax.json-api is not required). I am glad it was helpful. – Vikas Apr 27 '20 at 06:59