Posted on Leave a comment

Building Simple HTML Server Using JavaFX & Undertow

I recently had a customer who wants to view swagger-ui documentation for his APIs but he didn’t know how to set up a server and he wants to run in on his own laptop, not hosting anywhere else. With developers like us, there is no problem setting up a quick docker project and view the content. However, installing Docker is quite a lot for a non-tech user.

So, I decided to create a small JavaFX application that can run right away (given that the customer has JRE installed or can download and install quickly).

Application source code

You can download the source code from github and try yourself here:

https://github.com/datmt/undertow-simple-http-server

After that, you can simply run the Launcher file to have the server running.

How to use the application

When you launch the application, a small window appears like this:

Simple http server

There isn’t much thing to do, simply select the folder to be the web root and the port you want to run and click on start server.

If I visit the link http://localhost:9988 in my browser, this is what I see:

And you can easily navigate through the files.

One nice thing is you can use this to serve your videos (*.mp4) and watch on your phone (by accessing via your computer’s IP).

Posted on Leave a comment

JavaFX Starter Project – Quickly create and run

JavaFX is a great tool to quickly deploy desktop applications. However, the setup is always a pain for me, especially after JDK8, you need to download JavaFX SDK separately.

I create this JavaFX starter project to help myself and people who want to start working with JavaFX without going through the pain of setting up.

You can download the code here on github:

https://github.com/datmt/JavaFX-11-Starter-Project

Let’s get started.

Step 1: Download JavaFX SDK

Head to this site https://gluonhq.com/products/javafx/ and download JavaFX SDK 11. Save it somewhere on you computer. You will need it later.

Step 2: Clone the starter project

Clone the starter project here if you haven’t

https://github.com/datmt/JavaFX-11-Starter-Project

I assume that you use IntelliJ. Click on File->Project Structure->Libraries and then the (+) button:

Add javafx library to the project

Select Java and navigate the /lib folder inside the JavaFX SDK you have just downloaded.

select the bin folder

Click OK twice and you are ready.

Step 3: Run the application in the IDE

As you can see, we have two classes with the main function in the starter project. The one inside Main is created by the IDE. If you run the main function in this class, you may face some weird problems. The reason is given here.

So, instead of running Main, we run Launcher and your application can start just fine.

Step 4: Package the application as a Jar file

You can package and distribute the application with just a few clicks:

Go to Project Structure->Artifacts, click on the (+) icon:

Package javafx starter project as jar

On Main class selection, make sure you select Launcher, not Main:

To build the artifact, click on Build->Build artifact

Your fat jar file should be inside the out folder.

So, that’s all you need to do to create and run a JavaFX application. Using this starter project, you don’t have to deal with problems that can easily cost you hours to solve (Location is required!, for example).