Run Heroku CLI command from a java application hosted on Heroku - java

I'm looking for a way to run Heroku CLI commands within a java application hosted on Heroku.
I'd like to be able to run commands like heroku pg:backup heroku pg:restore etc
Is there a way to do that ?
EDIT : I added the Heroku CLI to my app, now I'm looking for a way to invoke heroku commands from my java code.
Maybe you could use something like this :
ProcessBuilder pb = new ProcessBuilder("/path/to/herokuCLI", "myCommand");
Problem is I don't know how to find the right path.
If you look at the file structure of your app on Heroku you can only find your .war + some "configuration files" which is quite normal as you only push a war to Heroku : Procfile system.properties target
Besides I don't think that's the right way to do that.
I'd like to avoid doing backup/restore operations in pure java code.
Maybe you can't rely on Heroku cmd to do the job for you after all.

You'll need to add the Heroku CLI buildpack to your application. You can do so with: heroku buildpacks:add heroku-community/cli
Push a new build after you've done so and you'll now be able to run CLI commands from inside your application dynos. You can see some additional information about this in the documentation.

Related

HeroKu - Gradle SpringBoot - H14 Error Deployment

I recently run into an "H14 Error" While Trying To Deploy Gradle Spring Project into the HeroKu platform.
Error:
code=H14 desc="No web processes running"
In My Project Folder, I have a "Procfile"
web: java -jar build/server/webapp-runner-*.jar build/libs/*.war
web: java -jar target/ClockGUI-0.0.1-SNAPSHOT.jar
heroku ps:scale web=1
I have researched many solutions but not worked out.
Please help me. Thank you so much
There are at least two issues with your Procfile:
You can't declare two web processes.
What command runs your web server? That's the only one that should be declared this way.
(I'm not sure what ClockGUI-0.0.1-SNAPSHOT.jar is supposed to be, but assuming "GUI" means something like SWT, Swing, or JavaFX, it won't work on Heroku. Where would the interface appear?)
You've included a Heroku CLI command.
heroku ps:scale web=1 is a command that you might run locally to scale your application's web dynos. It's not something that should be baked into the application source code.
Consider an e-commerce application. At certain times of the year, you might see a spike in traffic. You could use this command to scale up additional web dynos to handle the load. That doesn't (and shouldn't) require redeploying the application.
Remove that line. It doesn't do anything in the Procfile anyway.
Commit the updated Procfile and redeploy. Then, if you don't have any web dynos running, run heroku ps:scale web=1 in a terminal, ideally from your project directory so Heroku can infer which app you want to scale.

How can I deploy a java web app to heroku?

I have a Java web app with Tomcat embedded in my jar file. I can containerize the app with Docker and run it with command java -jar -myapp.jar, but I can't run that container in Heroku. I tried using heroku CLI to dockerize and deploy, but Heroku gives me an error of "can't access jarfile".
I tried to fix this by using heroku deploy:jar to deploy my fat jar, but this erroneously gives me an error:
heroku deploy:jar target/*.jar -a medscanner2
-----> Packaging application...
- app: medscanner2
- including: target/medscanner2.jar
! ERROR: Could not get API key! Please install the Heroku CLI and run
`heroku login` or set the HEROKU_API_KEY environment variable.
! Re-run with HEROKU_DEBUG=1 for more info.
!There was a problem deploying to medscanner2.
!Make sure you have permission to deploy by running: heroku apps:info -a
medscanner2
I am signed into Heroku and I can use heroku auth:whoami to verify that, I can push containers and deploy them, so this error doesn't make any sense. I reran with HEROKU_DEBUG=1 and it did not return any more info.
I further tried to set the HEROKU_API_KEY variable in the CLI with a token I got from Heroku and this still caused the same error when I try to deploy the jar.
I am using a Procfile (although I am not sure it is necessary):
web: java -Dserver.port=$PORT -jar target/medscanner2.jar
Since the issue seems to be indicating there is an issue with access I don't see how the Procfile could be influencing it.
What is the best way for me to deploy a Java web app that does not using Spring Boot to Heroku? I have separately deployed the docker container successfully to Google app engine, so all this work for Heroku is very frustrating.
I ended up fixing this by using webapp-runner to deploy my app. It runs the webapp-runner jar which can run your .war files. This required adding the heroku-maven-plugin and maven-dependency-plugin.
I could then add a Procfile: web: java -jar target/dependency/webapp-runner.jar target/*.war --port $PORT
and use the Heroku CLI to add the app using git. The link with webapp-runner is a guide to deploying tomcat java apps with webapp-runner.

Running a java subprocess from a python app in Heroku

I am new to Heroku. The backend logic of my flask web app is actually running a Java subprocess (subprocess.call( ...) ) to get some helper data. (I know it's a bad thing to do)
On deployment, Heroku works as expected and installs Python runtime and frameworks from requirements.txt but not JDK.
Any way I can configure Java in the same and make this work ??
Run the following command:
$ heroku buildpacks:add heroku/jvm
Then redeploy with a git push heroku master. This will install the JDK into your slug.

Troubleshooting with installation newrelic agent on tomcat

Trying to install java agent on tomcat.
successfully run
java -jar newrelic.jar install
then run apache, but not see any newrilc initialization messages in catalina out, and not found any logs in newrelic directory.
General guidance on this install process:
The script should print out a good amount of detail on what it's done, including where it detected your Tomcat instance. If nothing's showing up in your CLI, then it's likely you haven't started the install script.
You can use -h to see usage help, and -s /path/to/applicationserver if you want to make sure the script find your web app. Note that a clear error message should show up if the install script can't find your web app.
The install script is fairly simple, and you can perform a manual install with the steps in the New Relic documentation (broken out by framework).

Deploy Java on Heroku, Cannot find main class in target\dependency

I try to deploy java on Heroku. I follow the steps on Heroku
,But I get error on
java -cp target\classes;"target\dependency\*" HelloWorld
the error is :
Error: Could not find or load main class HelloWorld
How can I solve it? Is it case sensitive?
I found helloworld key word in pom.xml, so I try to change it to HelloWorld. It still does not work. btw, I am running windows 7(64 bit) with jdk1.7.0_05.
Looks like you are using Windows-style path for the -cp flag. Heroku runs on Linux, so you probably need to change that to java -cp target/classes:"target/dependency/*" HelloWorld in the Procfile you are deploying to Heroku.
For any avid readers who are trying to follow Heroku's getting started tutorial and are attempting to deploy locally and are receiving this error...
It is likely that you haven't built the target classes locally (they don't explain it very well in the tutorial).
If you are following the getting started guide with Maven then execute the following maven command in your project root folder to build your target locally:
mvn clean install
After that the local deployment commands like:
heroku local web -f Procfile.windows
should work as expected. (Obviously substitute Procfile.windows with just Procfile if you are not deploying under Windows)
You should then see something like:
Started ServerConnector#4d29c891{HTTP/1.1}{0.0.0.0:5000}
and can access your application locally by navigating to localhost on the port that was displayed:
localhost:5000

Categories

Resources