
Integrate executable Java code in your blog posts
While developing the Foojay Quickstart Java Tutorial, I was looking for an easy way to integrate runnable Java code examples into the Foojay pages and blogs. That’s when I discovered jdoodle.com. I started by using their online editor, but with this blog I want to show you an even easier method to integrate runnable code here on Foojay.
Integration examples
Single file code
To integrate “plain” Java code in your post or page, wrap it in the jdoodle shortcode:
{{< jdoodle >}}
// This is the place to put the code
{{< /jdoodle >}}Nothing else is needed: the page loads the JDoodle script by itself when it finds the shortcode.
For example, this:
{{< jdoodle >}}
public class MainArguments {
public static void main (String[] args) {
System.out.println("Number of arguments: " + args.length);
if (args.length > 0) {
System.out.println("First argument: " + args[0]);
}
for (int i = 0; i < args.length; i++) {
System.out.println("Argument " + (i + 1) + ": " + args[i]);
}
}
}
{{< /jdoodle >}}Will produce the following output. Hit the “Execute” button to run the code.
public class MainArguments {
public static void main (String[] args) {
System.out.println("Number of arguments: " + args.length);
if (args.length > 0) {
System.out.println("First argument: " + args[0]);
}
for (int i = 0; i < args.length; i++) {
System.out.println("Argument " + (i + 1) + ": " + args[i]);
}
}
}As you can see, the reader of your Foojay post can modify the code, execute it, and even add CommandLine Arguments to change the behavior of the code as you can see in this screenshot:

Code with external data files
In one of the more advanced tutorial steps, I wanted to read data from a text file. This can also be done with JDoodle. Put the data file itself in
assets/jdoodle/ in the Foojay repository, name it in the shortcode's
files parameter, and open it from /uploads/:
{{< jdoodle files="testdata.csv" >}}
// This is the place to put the code
{{< /jdoodle >}}Foojay's own JDoodle client id is what allows the use of external files, and the shortcode adds it for you. Create your own account on the JDoodle site if you want to use this functionality on another website.
This is a simple example to read data from a CSV file:
{{< jdoodle files="testdata.csv" >}}
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ReadTextFile {
public static void main (String[] args) {
File file = new File("/uploads/testdata.csv");
try (Scanner scanner = new Scanner(file)) {
while (scanner.hasNextLine()) {
System.out.println(scanner.nextLine());
}
} catch (FileNotFoundException ex) {
System.err.println("Could not find the file to be loaded");
}
}
}
{{< /jdoodle >}}A full example with this approach can be found in the tutorial: "Reading a Text File".
Conclusion
JDoodle allows to experiment with code in the browser and provides many other easy tools. Thanks to JDoodle you can now also add executable code to your Foojay content!
In a next post that will be published soon, we'll talk with the creator of JDoodle, Gokul Chandrasekaran.
Found a mistake, or something to add? Edit this page on GitHub







