B4J Tutorial [GCE] For Noobs, Part 9 - Running Your B4J App

Update: 05 Nov 2017 - Included a section at the end to explain how to access your app from outside of the VM.

Congratulations! You’re at Tutorial 9 and you’re now ready to run your B4J app.

At this point we can run your app on the VM. There’s still more we can do that will allow your app to run as a service but, that’s for another Tutorial (the next one I believe).

However, before we get your app running as a service we just want to make sure that everything is good and nothing unexpected happens. If any issues are found during this step then now is a good time to fix them.

To start your app type the following from the command line in the console

java -jar ~/my-super-app/my-super-app.jar​

After a brief moment, the app will be running. If you have Log() statements in your code then these will be displayed on the screen.

What did we do? We have called upon java to execute a jar. As good practice, you should provide the full path of the jar file to run. In the above example we have used ‘~’ to imply a path from within our home directory.

While your app is running you’re not able to get back to the command line. What the heck?!? Ok chillax and let me explain. You’re app is running but it’s running in the foreground. What this means is that your app will run until you break out of the app, or it crashes, or the server is restarted.

For now, let’s stop your app from running. To do this from the console press ctrl-c to quit out.

Now there’s a way to run the app from the console so that it does ‘effectively’ run in the background but I do not recommend it. If you are interested in finding out then search for ‘nohup’ in the forums here or use Google. The nohup command does have is uses but I will not be teaching that method here. Why? Good question again! The nohup command will run your app in the background until your VM is rebooted or the app crashes. Another reason is that there’s no ‘nice’ way to stop the app aside from killing the process. Also, there is nothing to restart your app should your app does crash or the server reboots.

Accessing Your App Externally
Before you can access your app you will need to know two (2) things. 1) the IP address of your VM and 2) the Port number your app is listening to (this part you should already know). Follow these steps to get the IP address.
  • Click on the ‘hamburger’ menu in the top-left of the GCP console to show all of the available services.

  • Scroll down until you find the ‘Compute Engine’ menu under the section labelled ‘COMPUTE’ and click.

  • You should now see the ‘VM Instances’ page showing you a summary of all the instances that you have created (which, if you’ve been following the series, there’ll be exactly one).

  • If you look closely at the columns in the summary you should see a column labelled ‘External IP’ (example image shown below)

    (in the above image I have three VMs in my list )

  • The IP address in the column ‘External IP’ is the IP address that you would use to access your service/web app externally.


    If your B4J app is listening to a port then you will need to add the port to the end. For example, if your B4J app is listening to port 16084 then use the following URL


    You must use a ‘colon’ (‘:’) to separate the IP and Port.

    If your B4J app is a web app then you can quite easily let your app listen to port 80. Using this port on your own VM means that you do not need to add the port to the end of the URL

    Note: the IP address shown in the 'External IP' column is not a 'static' IP address by default. This means that the IP address could change the next time the server is rebooted.
In a later tutorial we will go over some details of using other services that can add a lot of benefit to your web app as well as the difference between a 'static' and 'ephemeral' IP address.

In the next tutorial we will look at making your console app run as a service.
 
Last edited:
Cookies are required to use this site. You must accept them to continue using the site. Learn more…