Tutorial – Calling Java from a Bash Script

Bash is a very flexible cross-platform scripting language with multiple uses and can integrate with lots of other tools (ie DIff). It is built into OS X and Linux and you can download it for Windows. This post is about some uses of Bash and how you can use it with Java. We will show you how we run our Java program from Bash and hopefully give you some ideas for how you can use Bash in your own work.

At IDRSolutions, we specialize in converting PDF files to HTML5. This needs a LOT of regression testing, and we use Bash to control all this. We also use it to automate tasks as much as possible. Recently I wrote a Bash Script (So we can run on both Linux & Windows) to automatically pull the required files from our FTP, convert the PDF files used on our examples page, run PNG-Quant on them and re-upload the converted HTML5 output to the FTP. This way our examples page will always show a true representation of what our software can currently achieve (and I can get a coffee while the computer does all the hard work!)

In this tutorial I’ll be using our Java  PDF to HTML5 jar which converts files automatically with a Bash Script as an example of running a Java program from Bash. I will also cover how to run a really useful free image compression tool (PNGQuant) on the converted files in Bash.

Getting Started

General Setup :

  • First things first, you’ll need to download our PDF to HTML5 trial jar from here.
  • Next step is to create a folder anywhere you like and move the PDF to HTML5 jar into that folder, we’ll call it “ExampleFile”.
  • Next, get any PDF you wish to convert and move it into the ExampleFile folder you created, for this tutorial ours will be called examplePDF.pdf.
  • Next, create a .txt file and name it something like “ConvertPDF”, this will be where we write the BashScript.

Your ExampleFile directory should now look something like this :

dirStructure

  • Ensure you have Java and PngQuant installed using terminal write :

sugoGetJava

sugoGetPngQuant

Coding the BashScript

Ok so let’s get coding!

Open up the text file you created with an app such as gedit.

First we will add these two lines to the top of the text file which will tell the console that its a bash file and to clear the console screen :

#!/bin/bash
clear

Now we’re going to add a variable which will hold the location where we want our output directory (where we want the converted files to end up)  and one more variable to hold the PDF file name.

Notice the “echo” words? They just print text to the console, so they’re useful for documentation, giving instructions and placing line-breaks.

Like so :

echo "---------Starting Script---------"
echo
echo "---------Generating Vars---------"
OUTPUT="OutputFile/"
FILE="examplePDF.pdf"

Now we will write some code which will automatically create our OutputFile directory for us (makes our life easier and cuts down the requirements), this is where our converted content will end up.

Like so :

echo "---------Making Directories---------"
mkdir -p OutputFile/

And finally the code which will run the PDFtoHTML5 jar with the input PDF file name and the output location.

Like so :

echo "---------Converting---------"
java -Xmx512M  -jar jpdf2html.jar $FILE $OUTPUT
echo
echo "---------Finished Script---------"

Your code should now look something like this :

#!/bin/bash
clear
echo "---------Starting Script---------"
echo
echo "---------Generating Vars---------"
OUTPUT="OutputFile/"
FILE="examplePDF.pdf"
echo "---------Making Directories---------"
mkdir -p OutputFile/
echo "---------Converting---------"
java -Xmx512M  -jar jpdf2html.jar $FILE $OUTPUT
echo
echo "---------Finished Script---------"

Giving Script Permission & Running :

Now we have written our BashScript to convert a PDF file to HTML5 we need to give the file permission to run, so first thing we do is navigate to the directory where our .txt file is located using terminal write:

cdExampleFIle

And now give our BashScript permission to run by writing in terminal :

examplePermission

And finally we run it,

unit

startScript

If we now check our OutputFile directory we should see the following file which contains our html5 files.

outputFile

 

Congratulations!! In just a few lines of code you have just converted your first PDF file to HTML5 using just a java .jar file and some home-brewed BashScript!

As a bonus and for a bit of fun we’ll now run .png quant over all the .png files in the converted output folder which will reduce the file size dramatically and you can then even host the content on your website if you wanted to!

Running PNGQuant

Ok so once again open up the BashScript we were editing

Now add the following lines of code the bottom of the BashScript (assuming you installed PNGQuant correctly)  :

echo "-----Starting PNGQuant Script-----"
find . -name '*.png' -exec pngquant --ext .png --force 256 {} \;
echo "-----Finished PNGQuant Script-----"

Your BashScript file should finally look like this :

#!/bin/bash
clear
echo "---------Starting Script---------"
echo
echo "---------Generating Vars---------"
OUTPUT="OutputFile/"
FILE="examplePDF.pdf"
echo "---------Making Directories---------"
mkdir -p OutputFile/
echo "---------Converting---------"
java -Xmx512M  -jar jpdf2html.jar $FILE $OUTPUT
echo "-----Starting PNGQuant Script-----"
find . -name '*.png' -exec pngquant --ext .png --force 256 {} \;
echo "-----Finished PNGQuant Script-----"
echo
echo "---------Finished Script---------"

Now PNGQuant will run over our converted files once they are converted.

So finally, cd to where our BashScript is located :

cdExampleFIle

And run the BashScript again :

unit

You should now see this :

pngquanted

And that’s it! We have now not only written a BashScript that will convert a PDF file to HTML5 using Java but now it will also compress any .png images in the converted output, thus reducing file-size!!

This post is part of our “Java Articles Index“ series. In these articles, we aim to explore the world of Java and Javafx. Have a look through!

If you’re a first-time reader, or simply want to be notified when we post new articles and updates, you can keep up to date by social media (TwitterFacebook and Google+) or the  Blog RSS.


Related Posts:

The following two tabs change content below.
Nathan is a developer at IDRSolutions, focusing mainly on the development of HTML5 and Form support for the online PDF converter. He was a speaker and exhibitor at JavaOne 2013, co-presenting a session titled 'Lessons Learned from Using GlassFish with NetBeans". Nathan also enjoys writing technical blog-articles.
Nathan

About Nathan Howard

Nathan is a developer at IDRSolutions, focusing mainly on the development of HTML5 and Form support for the online PDF converter. He was a speaker and exhibitor at JavaOne 2013, co-presenting a session titled 'Lessons Learned from Using GlassFish with NetBeans". Nathan also enjoys writing technical blog-articles.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>