Deploying Gradle project on Heroku - java

The problem is when I have the project deployed on Heroku after running
heroku local web
it would show the following
C:\Users\Richer\OneDrive\Project>heroku local web
5:22:24 PM web.1 | Error: Could not find or load main class $JAVA_OPTS
[DONE] Killing all processes with signal SIGINT
5:22:25 PM web.1 Exited with exit code null
and if I run
heroku ps
the shell will show
C:\Users\Richer\OneDrive\Project>heroku ps
Free dyno hours quota remaining this month: 549h 21m (99%)
Free dyno usage for this app: 0h 0m (0%)
For more information on dyno sleeping and how to upgrade, see:https://devcenter.heroku.com/articles/dyno-sleeping
=== web (Free): java -Dserver.port=$PORT $JAVA_OPTS -jar build/libs/*.jar (1)
web.1: crashed 2018/10/12 17:22:22 +0300 (~ 14m ago)
my Procfile looks like this
web: java -Dserver.port=$PORT $JAVA_OPTS -jar build/libs/*.jar
If it helps I have two java classes at src/main/java folder as it should be and after many attempts to upload and tun the project I've also built a jar file and its location is at out/artifacts/1_jar/1.jar though this didn't help.
So what do I do?
In addition, to successfully build a project I had to put this 3 strings in the build.gradle as it was described here
ask stage(dependsOn: ['build', 'clean'])
build.mustRunAfter clean
heroku config:set GRADLE_TASK="build"

On Windows, environment variables are used with the $ notation. You'll need to create a second file called Procfile.windows with the contents:
web: java -Dserver.port=%PORT% %JAVA_OPTS% -jar build\libs\*.jar
Then run heroku local web -f Procfile.windows
But leave the $ for Heroku, which uses Linux.

Related

Heroku process crashing as soon as the process starts

I have a simple Micronaut server I am trying to launch on Heroku by building it with a heroku.yml but for some reason when I check the logs the process exits as soon as it starts. The docker image runs just fine locally and nothing else prints out in the logs so I can't find out why.
Here is my Dockerfile
FROM node as build-frontend
WORKDIR /app
COPY frontend/myfrontend/package.json .
COPY frontend/myfrontend/public ./public
COPY frontend/myfrontend/src ./src
RUN npm install .
RUN npm run build
FROM gradle:6.8.2-jre11 AS build-env
# Set the working directory to /home
WORKDIR /home
COPY --chown=gradle:gradle backend ./
COPY --from=build-frontend /app/build /home/src/main/resources/public
# Compile the application.
RUN ./gradlew assemble
FROM openjdk:11.0.10-jre-slim-buster
# Set the working directory to /home
WORKDIR /home
# Copy the compiled files over.
COPY --from=build-env /home/build/libs/myjar-0.1-all.jar /home/myjar.jar
# Starts the java app
# Using EntryPoing to pass env. variables as describe in this article
ENTRYPOINT exec java -XX:+UseContainerSupport -XX:MaxRAMPercentage=80.0 -noverify -XX:+AlwaysPreTouch -jar myjar.jar
Here is my heroku.yml
setup:
addons:
- plan: heroku-postgresql
as: DATABASE
build:
docker:
web: Dockerfile
run:
web: "exec java -XX:+UseContainerSupport -XX:MaxRAMPercentage=80.0 -noverify -XX:+AlwaysPreTouch -jar myjar.jar"
and finally my Micronaut application.yml which just sets some configs
micronaut:
application:
name: mypackage
router:
static-resources:
default:
enabled: true
paths:
- classpath:public
mapping: /**
server:
port: ${PORT:8080}
cors:
enabled: true
datasources:
default:
driverClassName: org.postgresql.Driver
dbUrl: jdbc:postgresql://mydbhost.com:port/dbname?sslmode=require
dbUsername: dbuser
dbPassword: dbpasswd
scan:
packages: mypackage
netty:
default:
allocator:
max-order: 3
When I just do docker build -t test-image:latest . and docker run -p 80:8080 test-image:latest I can connect just fine on localhost and the docker container runs the micronaut server. If it fails for some reason I see an output in the container logs detailing why. When I upload this to heroku (through a github deployment) All i see in the logs are
2023-01-09T22:25:50.145942+00:00 heroku[web.1]: Starting process with command `/bin/sh -c exec\ java\ -XX:\+UseContainerSupport\ -XX:MaxRAMPercentage\=80.0\ -noverify\ -XX:\+AlwaysPreTouch\ -jar\ myjar.jar`
2023-01-09T22:25:51.381760+00:00 heroku[web.1]: Process exited with status 0
2023-01-09T22:25:51.439962+00:00 heroku[web.1]: State changed from starting to crashed
I have tried:
To run it locally connected to the heroku addon postgres database, works just fine
Simplifying the build as much as possible
Removing the default on the port (to ensure it picks up $PORT), and running it locally with export PORT=8080 set (works just fine, docker container picks up the env port like we expect in heroku)
And I cannot figure out why it just quits immediately on Heroku. editted: originally thought it had to do with the port value but but I figured out how to give micronaut a port through the command line and it still doesn't work on Heroku (works locally)
edit:
I tried changing my application.yml to this with the DB hardcoded and still nothing. Just does not seem to work. App still crashes and there is nothing to indicate why in the logs
setup:
config:
PORT: $PORT
MICRONAUT_SERVER_PORT: $PORT
build:
docker:
web: Dockerfile
run:
web: "exec java -Dmicronaut.server.port=$PORT -XX:+UseContainerSupport -XX:MaxRAMPercentage=80.0 -noverify -XX:+AlwaysPreTouch -jar myjar.jar"
I still have no extra output from Heroku. No stacktrace or stderr or stdout at all.
Well, it isn't much of an answer. And nothing in the Heroku docs points me to why this works but removing ENTRYPOINT exec java -XX:+UseContainerSupport -XX:MaxRAMPercentage=80.0 -noverify -XX:+AlwaysPreTouch -jar myjar.jar from my Dockerfile works. With entrypoint removed I see logs again and can see my micronaut server starting. With ENTRYPOINT defined in my Dockerfile I just get what I posted about above "Process starting" followed immediately by process crashed

Keeping my JAVA (jar) program running on Heroku

I have made a bot in Java and it works great when I run the command:
heroku run nohup java -jar bot.jar
However, after 24h Heroku closes it because of their Dyno policy. So, I thought of making a Procfile in order for it to execute the command above everytime it comes alive again.
I've tried placing the following commands inside the Procfile:
worker: java -jar bot.jar
worker: nohup java -jar bot.jar
And I even made a run.sh file that contains:
#!/bin/bash
nohup java -jar bot.jar
In order to make a Procfile that has:
worker: sh run.sh
Sadly, none of this works, because everytime the Dyno executes the Procfile (right after I push a new version of it through git), the bot never comes alive. The logs show:
2017-06-07T13:17:33.651806+00:00 app[api]: Starting process with command `sh run.sh` by XXXX
2017-06-07T13:17:37.958327+00:00 heroku[run.2087]: State changed from starting to up
2017-06-07T13:17:37.948696+00:00 heroku[run.2087]: Awaiting client
2017-06-07T13:17:38.283581+00:00 heroku[run.2087]: Starting process with command `sh run.sh`
2017-06-07T13:17:52.807189+00:00 heroku[run.2087]: Client connection closed. Sending SIGHUP to all processes
2017-06-07T13:17:53.343009+00:00 heroku[run.2087]: Process exited with status 129
2017-06-07T13:17:53.359331+00:00 heroku[run.2087]: State changed from up to complete
2017-06-07T13:24:32.000000+00:00 app[api]: Build started by user XXXX
2017-06-07T13:24:43.090567+00:00 app[api]: Deploy YYYY by user XXXX
2017-06-07T13:24:43.090567+00:00 app[api]: Release v17 created by user XXXX
2017-06-07T13:24:32.000000+00:00 app[api]: Build succeeded
I have no idea what to do in order for my bot to run from the Procfile.
Can you help me?
Thanks.
Try running:
$ heroku ps:scale worker=1
This will ensure that one worker process is always running.

Docker build hangs during downloads

Mac 10.10.5 here, using docker-machine to create a VirtualBox host VM for my local Docker. I have a project that builds an executable JVM located at build/libs/myapp-SNAPSHOT.jar. My Dockerfile, which is located in the root of the project, looks like:
FROM frolvlad/alpine-oraclejdk8:slim
VOLUME /tmp
ADD build/libs/myapp-SNAPSHOT.jar myapp.jar
RUN sh -c 'touch /myapp.jar'
ENTRYPOINT ["java","-jar","/myapp.jar"]
Please note, I don't wish to push my images to any registry, just keep/run them locally (for now). When I run:
docker build -t myorg/myapp .
I get the following console output:
myuser#mymachine:~/sandbox/myapp$docker build -t myorg/myapp .
Sending build context to Docker daemon 42.69 MB
Step 1 : FROM frolvlad/alpine-oraclejdk8:slim
slim: Pulling from frolvlad/alpine-oraclejdk8
d0ca440e8637: Downloading [=================================================> ] 2.295 MB/2.32 MB
0f86278f6be1: Downloading [=================================================> ] 3.149 MB/3.172 MB
c704a6161dca: Download complete
And then the command-line just hangs after printing that "Download complete" message. I've waited for as long as 30 minutes (!!!) and nothing happens.
Any ideas where I'm going awry?
The VM is probably hanging. Try the following: https://github.com/docker/machine/issues/1819#issuecomment-138981139
docker-machine rm -f default
rm -fv ~/.docker/machine
docker-machine -D create -d virtualbox default
There are more issues about this on OSX.
I think the best practice is to setup a Linux native build box if you are doing any serious development. That way you can run docker without any VM overhead(which is ironically one of the major pain points docker is trying to solve)
There's also a Docker Beta program which runs on libcontainer natively on OSX and Windows.

Run war with tomcat in docker

I've followed these two post 1 & 2 and neither work. I'm currently building my tomcat with the below.
Build File
FROM tomcat:8.0
COPY server/build/libs/server.war /usr/local/tomcat/webapps/server.war
CMD ["catalina.sh", "run"]
Terminal
docker build -t my_server .
docker run -it -rm -p 8080:8080
When I go to http:localhost:8080 I see the manager home page but http:localhost:8080/server or http:localhost:8080/server/webapp/do not show up. My terminal tells me that my war is getting added, but nothing that says it's expanded
INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application archive /usr/local/tomcat/webapps/server.war has finished in 2,518 ms
For removing the manager app, you need to put the following RUN command before you copy the WAR in DockerFile.
RUN rm -rf /usr/local/tomcat/webapps/*
The above command removes the default apps available in tomcat.
Your application should be available at http:localhost:8080/server/

Java Heroku application run locally with foreman

I'm following the "Getting Started with Java on Heroku" guide at
https://devcenter.heroku.com/articles/getting-started-with-java
I follow the steps till I deploy and execute succesfully the application downloaded from GitHub.
When I try to execute it locally on Windows XP with the command
foreman start web
I get the error:
web.1 | started with pid 3388
web.1 | Error: Could not find or load main class Main
web.1 | exited with code 1
system | sending SIGKILL to all processes
My Procfile is:
web: java %JAVA_OPTS% -cp target\classes:target\dependency\* Main
And
>echo %JAVA_OPTS%
-Xms256m -Xmx512m
Can anyone suggest me how to solve?
Quotes and semicolon
web: java %JAVA_OPTS% -cp target\classes;"target\dependency\*" Main
I ran into this issue while running through the https://devcenter.heroku.com/articles/getting-started-with-java tutorial.
After tinkering with some of these answers I discovered that step six at https://devcenter.heroku.com/articles/getting-started-with-java#define-a-procfile tells the answer.
When you see instructions to run your app with foreman, append an extra -f Procfile.windows flag to ensure your Windows-specific Procfile is picked up. For example: foreman start web -f Procfile.windows
Once I switched to the foreman start web -f Procfile.windows command, everything worked smoothly.
Same problem with java-getting-stared app downloaded from heroku server. Changing to ";" works on Windows. Still need ":" on heroku linux server.

Categories