Java Runtime Environment
This article serves as a thorough introduction to Cloud Engine’s Java runtime environment. To quickly get started with Cloud Engine, see Getting Started With Cloud Engine.
You can deploy WAR and JAR projects built with Maven to Cloud Engine, or upload WAR files directly to Cloud Engine.
If you are looking to start a new project, we recommend that you use one of our sample projects as a boilerplate:
Running a Java program usually costs a large amount of memory. If you are using a trial instance with 256M of memory, Cloud Engine might encounter an OOM error when starting your Java program, causing your deployment to fail. Even if Cloud Engine is able to start your program, the program might frequently get restarted due to low memory.
We recommend that you pick an instance with at least 512 MB of memory. If your project is built with Spring Boot, a minimum of 1024 MB of memory is recommended. You can adjust the memory size at any time even if you've already deployed your project. See Cloud Engine Platform Features § Adjusting Quota and Number of Instances for more information on how to adjust the memory size of your instances.
Startup Command
Once the build is finished, Cloud Engine will look for a .war or .jar file under target:
- If it finds a
.warfile, it will place the file into a Servlet container (Jetty 9.x). - If it finds a
.jarfile, it will run the file withjava -jar.
Configure JVM Parameters
When Cloud Engine runs a Java application, it will give -Xmx a value equivalent to 70% of the size of the instance. The remaining 30% will be reserved for off-heap memory and other consumptions. You may customize the value of -Xmx if your application has special needs (like using a large amount of off-heap memory). For example, if your instance has a memory of 2 GB, you can add a custom environment variable on Cloud Engine’s Settings page with JAVA_OPTS as its name and -Xmx1500m as its value. This will limit the size of the JVM heap to 1.5 GB and leave 500 MB for PermGen, off-heap memory, and other stuff. Keep in mind that if the value of -Xmx is too small, a large amount of CPU might be wasted on repetitive garbage collection tasks.
Configure Java Version
To specify the Java version you want to use, create a file named system.properties under the root directory of your project:
java.runtime.version=11
At this time, Cloud Engine supports AdoptOpenJDK 8, 11, 12, 13, and 14.
If you don’t specify a Java version, the latest stable (LTS) version will be used.
Upload WAR File
If you’ve already built the WAR file on your local computer (with commands like mvn package), you can add the --war option when you deploy your project with the CLI. This tells the CLI to upload the WAR file rather than the source code.
tds deploy --war
In this case, Cloud Engine won’t install dependencies or build your project on the server side. Instead, it will run the WAR file by placing it into a Servlet container.
Install Dependencies and Build
If you’ve uploaded the source code, Cloud Engine will install the dependencies listed in pom.xml with Maven and build your project by running mvn package.
Customize Build Process
You can override the default behavior by specifying startup commands (run), dependency installation commands (install), and build commands (build) in leanengine.yaml:
run: echo 'run another command'
install:
- {use: 'default'}
- echo 'install additional dependencies here'
build:
- echo 'overwrite default build command here'
See Reference: leanengine.yaml for more information.
Below are a few examples:Upload Jar Without Building on the Server
runtime: java
install: []
build: []
run: java -jar your-package.jar
Here we have specified that we are using the Java Runtime Environment. We have skipped the dependency installation and build steps by leaving them empty. We also specified the run command (run your-package.jar in the root directory).