Skip to Content
Technical Articles
Author's profile photo Maximilian Streifeneder

Max’s Adventure in SAP Cloud Platform: Debug your Java application in Cloud Foundry

Facing issues with your deployed Java application in Cloud Foundry (not particularly in SAP Cloud Platform, could be any Cloud Foundry environment) and you are not entirely sure what the root cause is? Most of the time debugging is an obvious solution approach. As long as the application is running on your own machine this might not be a challenge. But once your application is already deployed in Cloud Foundry..

There are numerous reasons for debugging an application. (not only those written Java) A few months ago I already wrote a blog post and philosophised a little about šŸ˜‰ Have a read!

So, since this is a technical blog – I don’t want to drop more empty phrases.

What you need:

  • A (free) Cloud Foundry space, e.g. in SAP Cloud Platform
  • An installed Cloud Foundry CLI (Command Line Interface)->Tutorial to both of them can be found here: Get started with Cloud Foundry
  • Some basic Java KnowHow: Building a Java app on your computer
  • Some basic Cloud Foundry KnowHow: Pushing an application to a Cloud Foundry space

What you will do:

  • Start your Java web server in DEBUG Mode
  • Use Cloud Foundry SSH to forward traffic from remote ports to ports on your computer
  • Configure Eclipse to debug remote applications

1) (optional) Set up your Java project:

If you don’t have a Java application yet and want to try debugging anyway, clone a random Java project (which is suited for Cloud Foundry). Either take one of the Cloud Foundry GitHub repositories, which are quite easy to clone and set up or even get started with tutorials for the S/4HANA Cloud SDK and also discover the capabilities of the recently released SDK.

Cloud Foundry Java Samples: Link
S/4HANA Cloud SDK Java: Tutorial

2) Build your app

To produce a .war or .jar (i.e. spring boot) file, build your project by the following command in the folder where your pom.xml is located:

mvn clean package


3) Update your manifest.yml

You should usually have a manifest.yml to deploy your application to a Cloud Foundry Space. Mine, without any debug configuration, is:

---
applications:
- name: myblogapp
  memory: 1024M
  instances: 1
  path: target/HelloWorld-0.0.1-SNAPSHOT.war

 

To enable remote debugging, add an environment variable to configure the application server:

---
applications:
- name: myblogapp
  memory: 1024M
  instances: 1
  path: target/HelloWorld-0.0.1-SNAPSHOT.war
  env:
    JAVA_OPTS: "-agentlib:jdwp=transport=dt_socket,address=8686,server=y,suspend=n"


JAVA_OPTSĀ 
is the standard environment variable that some servers and other java apps append to the call that executes theĀ javaĀ command.

For example in tomcat if you defineĀ JAVA_OPTS=’-Xmx1024m’, the startup script will executeĀ java org.apache.tomcat.Server -Xmx1024m.

What your additional line in the manifest.yml effectively does:

• transport=dt_socket : defines the transport mode used to connect to JVM (socket is a good choice, it can be used to debug a distant computer)
• address= 8686 : TCP/IP port exposed, to connect from the debugger,
• suspend=n : TheĀ suspendĀ parameter of theĀ -XrunjdwpĀ argument specifies whether you want to suspend the debugged JVM until the debugger attaches to it.

By default, theĀ suspendĀ parameter has the value ā€œyā€œ, which means that the virtual machine would be suspended before it had the opportunity to load the main class, and the attached debugger would resume the execution of the VM.

Since you usually need the web server to boot properly before you can debug your application, you should set theĀ suspendĀ parameter to ā€œnā€œ.

4) Push the application to Cloud Foundry

Before you can deploy your application to a Cloud Foundry space, you should be logged in to the according endpoint. If you are not yet familiar with the Cloud Foundry CLI make yourself familiar with it using this tutorial.

Deploy the application via:

cf push

5) Tunnel your configured port

As you set up your debugging port in step 3 to localhost:8686, this port is right now only available within your Cloud Foundry app instance, but not anywhere else. Thus you are going to use SSH to enable port forwarding.

SSH access can be enabled at different levels – either for an entire Cloud Foundry space or only for a certain app.

For the entire space:

cf allow-space-ssh [spacename]
cf disallow-space-ssh [spacename] 

In your specific case only for one app:

cf enable-ssh myblogapp
cf disable-ssh myblogapp

Before you can go over to the actual port forwarding, the app must be restarted to take the enabled ssh statement into account.

cf restart myblogapp

Taken from the official Cloud Foundry documentation:

In general:

$ cf ssh MY-AWESOME-APP -L [LOCAL-NETWORK-INTERFACE:]LOCAL-PORT:REMOTE-HOST-NAME:REMOTE-HOST-PORT


For your specific case:

$ cf ssh myblogapp -L 8686:localhost:8686

This means that traffic going to port 8686 in your Cloud Foundry instance will be forwarded to the your computer on port 8686, to be more precise localhost:8686.

Too long; didn’t read? Or want to see it in action?

6) Get your Eclipse ready!

Two things are inevitable to debug remote applications in Eclipse: On the hand the source code of your application that you are able to set breakpoints. And also the debugger needs the source code to stop and visualize where the execution actually is. On the other hand, Eclipse needs to know the connection details of where it’s supposed to listen for traffic.

1. Import your project to Eclipse (if not already done)
2. Go to Run > Debug Configurations

3. Go to Run > Debug Configurations
4. Create a new Debug Configuration for a remote application. Enter the connection properties from your previous steps – localhost:8686 and chose the right Project.

7) Start the Debug ConfigurationĀ 

Attach your debugger to the according port on your localhost with the previously created Configuration. Also, set a breakpoint in your coding – otherwise the execution might not stop šŸ˜‰

8) Gentleman, start your engine!

Hit the endpoint of your Cloud Foundry application or execute whatever logic in your application to stop at your breakpoint. Good luck fixing your bug šŸ˜‰

Assigned Tags

      8 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Ruchi Dhiman
      Ruchi Dhiman

      Hi Maximilian,

      after executing the steps you provided I get the error message saying: 'Launching DebugRemoteJavaApplication' has encountered a problem. Failed to connect to remote VM. Any ideas what the problem could be?

      Greetings,

      Ruchi

      Author's profile photo Achal Kansal
      Achal Kansal

      Hi Ruchi,

       

      You can try remote debugging with IntelliJ.

      You can also refer this link

       

      Thanks,

      Achal

      Author's profile photo Ivan Mirisola
      Ivan Mirisola

      Hi All,

      Debugging on Cloud Foundry depends on which JVM you are using (in other words: which buildpack). To get the several different options to enable debug on CF, you have to follow the steps described in the documentation:

      https://help.sap.com/viewer/65de2977205c403bbc107264b8eccf4b/Cloud/en-US/1e7376fa1a8a4cefbed5c87693af4e6a.html

      Best regards,
      Ivan

      Author's profile photo Krzysztof Bulanowski
      Krzysztof Bulanowski

      Hi,

      I have deployed my app to CF, the SSH is enabled for the app, but when I try to create the SSH tunel, after some time I get the following error:

      FAILED
      Error opening SSH connection: dial tcp 3.124.222.77:2222: connectex: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

      What could be the problem?

      Many thanks in advance,

      Chris.

      Author's profile photo Krzysztof Bulanowski
      Krzysztof Bulanowski

      Ok, found the issue - it was VPN of my company.

      After I disconnected from the VPN, I could log on to the vcap console.

      But then there is another issue - I loose the connection with my on-premise system exposed to CF using Cloud Connector.

       

      So, is there a way to connect to CF over SSH from inside the VPN network?

      Many thanks in advance,

      Chris.

      Author's profile photo Nathan Wang
      Nathan Wang

      In 3)Update your manifest.yml,Ā  the setting should be like:

      JBP_CONFIG_JAVA_OPTS: ā€˜[java_opts: ā€œ-agentlib:jdwp=transport=dt_socket,address=8686,server=y,suspend=nā€]’

      Otherwise, you may get failed when you started your debugging

      Author's profile photo Danil Menshykov
      Danil Menshykov

      Thank you! Solved my problem!

      Author's profile photo Matheus Leao
      Matheus Leao

      What would be the analogous steps for a Nodejs applicatication and Visual Code instead of Eclipse?

      I've tried the steps over at https://blogs.sap.com/2020/11/23/debugging-nodejs-application-in-vscode-running-on-sap-cloud-foundry/ but still getting a connection error when actually running the debug configuration.