calling java from a bash script 5
DESCRIPTION
Tutorial and step by step guide on how to call Java from a BashScriptTRANSCRIPT
Tutorial -Calling Java from a Bash Script
What is Bash?
Bash is a very flexible cross-platformscripting language with multiple uses and canintegrate with lots of other tools (ie DIff). It isbuilt into OS X and Linux and you candownload it for Windows. This post is aboutsome uses of Bash and how you can use it withJava. We will show you how we run our Javaprogram from Bash and hopefully give yousome ideas for how you can use Bash in yourown work.
What we use our BashScript for?
At , we specialize in converting . This needs a LOT of regressiontesting, and we use Bash to control all this. We alsouse it to automate tasks as much as possible.Recently I wrote a Bash Script (So we can run onboth Linux & Windows) to automatically pull therequired files from our FTP, convert the PDF filesused on our examples page, run PNG-Quant on themand re-upload the converted HTML5 output to theFTP. This way our will always show atrue representation of what our software cancurrently achieve (and I can get a coffee while thecomputer does all the hard work!)
IDRSolutions
examples page
PDF to HTML5
General Set up:
First things first:You will need to visit our webkit to download ourPDF to HTML5 Trial Jar from
Next step is to create a folder anywhere youlike and move the trial jar you justdownloaded into the folder you created, forthis tutorial ours will be called examplePDF.pdf
Next create a .txt fileand name it somethinglike Convert PDF this iswhere we will write ourBashScript
here.
Nearly all set up and good to go!
Next you will need to ensure that you havejava and PNGQuant installed:
Open Terminal and type the following line ofcode:
sudo apt-get install open jdk-7-jdk open jdk-7-jre
Once you have done that next type in thefollowing into terminal
sudo apt-get install pngquant
Now thats all done we can get down towriting some actual code!
Lets get coding!
Ok we are good to go and get started on writing ourBashScript.
Open up the text file that you had created with anapp such as gedit
First of all we will add the following twolines to the top of out text file which willtell the console that its a bash file and itwill also clear the console screen:
Coding the bash script
Now we’re going to add a variable which will hold thelocation where we want our output directory (where wewant the converted files to end up) and one morevariable to hold the PDF file name.
Notice the “echo” words? They just print text to theconsole, so they’re useful for documentation, givinginstructions and placing line-breaks.
Like so :
Coding the bash script
Now we will write some code which will automaticallycreate our OutputFile directory for us (makes our lifeeasier and cuts down the requirements), this is where ourconverted content will end up.Like so :
And finally the code which will run the PDFtoHTML5jar with the input PDF file name and the outputlocation.Like so :
Coding the bash script
All together your code should now look something likethis:
Giving the Scropt Permission and Running
Now we have written our BashScript to convert a PDFfile to HTML5 we need to give the file permission to run,so first thing we do is navigate to the directory whereour .txt file is located using terminal write:
And now give our BashScript permission to run bywriting in terminal :
And now we run it..
Checking out output file
If we now check our OutputFile directory we should seethe following file which contains our html5 files
Congratulations!! In just a few lines of code you havejust converted your first PDF file to HTML5 using just ajava .jar file and some home-brewed BashScript!As a bonus and for a bit of fun we’ll now run .png quantover all the .png files in the converted output folderwhich will reduce the file size dramatically and you canthen even host the content on your website if youwanted to!
Running PNGQuant
Ok so once again open up the BashScript we were editingNow add the following lines of code the bottom of theBashScript (assuming you installed PNGQuant correctly) :
Your BashScript should finally look like this:
Running PNGQuant
Now PNGQuant will run over our converted files once theyare converted. So finally, change the directory to where our BashScriptis located:
Run the Bash Script again
And that’s it! We have now not only written a BashScriptthat will convert a PDF file to HTML5 using Java but now itwill also compress any .png images in the convertedoutput, thus reducing file-size!!
Follow us
www.idrsolutions.com
http://blog.idrsolutions.com
With thanks to the author: Nathan Howard