How To Create QR Codes in Java & Servlet

Nowadays, Quick Response (QR) Codes are becoming more and more useful as they have gone mainstream, thanks to the smart phones. Right from the bus shelter, product packaging, home improvement store, automobile, a lot of internet websites are integrating QR Codes on their pages to let people quickly reach them. With increase in number of users of smart phones day by day, the QR codes usage is going up exponentially.

Let us see a quick overview of Quick Response (QR) codes and also how to generate these codes in Java.

Introduction to QR Codes

A QR code (abbreviated from Quick Response code) is a type of matrix barcode (or two-dimensional code) first designed for the automotive industry. More recently, the system has become popular outside of the industry due to its fast readability and comparatively large storage capacity. The code consists of black modules arranged in a square pattern on a white background. The information encoded can be made up of four standardised kinds (“modes”) of data (numeric, alphanumeric, byte/binary, Kanji), or by supported extensions virtually any kind of data.

Created by Toyota subsidiary Denso Wave in 1994 to track vehicles during the manufacturing process, the QR code is one of the most popular types of two-dimensional barcodes. It was designed to allow its contents to be decoded at high speed.

Hello World QR Code in Java

Zebra Crossing (ZXing) is an awesome open source library that one can use to generate / parse QR Codes in almost all the platforms (Android, JavaSE, IPhone, RIM, Symbian etc). But if you have to generate simple QR Codes, I found it a bit clumsy to implement.

However QRGen is a good library that creates a layer on top of ZXing and makes QR Code generation in Java a piece of cake. It has a dependency on ZXing, so you would need ZXing jar files along with QRGen to create QR Codes in Java.

On the download page of ZXing, you will not find the JAR files. Instead we have to create JAR files using the source code. I have already generated these JAR files.

Here are the links:
zxing-core-1.7.jar (346 KB)
zxing-javase-1.7.jar (21 KB)

Also download the QRGen JAR File from their download page.
Include these JAR files in your Classpath and execute following Java code to generate QR Code.

package net.viralpatel.qrcode; import; import; import; import; import; import net.glxn.qrgen.QRCode; import net.glxn.qrgen.image.ImageType; public class Main { public static void main(String[] args) { ByteArrayOutputStream out = QRCode.from("Hello World") .to(ImageType.PNG).stream(); try { FileOutputStream fout = new FileOutputStream(new File( "C:\\QR_Code.JPG")); fout.write(out.toByteArray()); fout.flush(); fout.close(); } catch (FileNotFoundException e) { // Do Logging } catch (IOException e) { // Do Logging } } }
Code language: Java (java)

The code is pretty straight forward. We used QRCode class to generate QR Code Stream and write the byte stream to a file C:\QR_Code.JPG.

Download Source Code (339 KB)

If you open this JPEG file and scan using your iPhone or Android QR scanner, you’ll find a cool “Hello World” in it :)
Apart from generating Sterams of data using QRGen API, we can also use below APIs to create QR Codes:

// get QR file from text using defaults File file = QRCode.from("Hello World").file(); // get QR stream from text using defaults ByteArrayOutputStream stream = QRCode.from("Hello World").stream(); // override the image type to be JPG QRCode.from("Hello World").to(ImageType.JPG).file(); QRCode.from("Hello World").to(ImageType.JPG).stream(); // override image size to be 250x250 QRCode.from("Hello World").withSize(250, 250).file(); QRCode.from("Hello World").withSize(250, 250).stream(); // override size and image type QRCode.from("Hello World").to(ImageType.GIF).withSize(250, 250).file(); QRCode.from("Hello World").to(ImageType.GIF).withSize(250, 250).stream();
Code language: Java (java)

Website Link (URLs) QR Code in Java

One of the most common use of a QR Code is to bring traffic to a particular webpage or download page of website. Thus QR Code encodes a URL or website address which a user can scan using phone camera and open in their browser. URLs can be straight forward included in QR Codes. In above Java Hello World example, just replace “Hello World” string with the URL you want to encode in QR Code. Below is the code snippet:

ByteArrayOutputStream out = QRCode.from("//") .to(ImageType.PNG).stream();
Code language: Java (java)

QR Code in Servlet

Most of the time you would need to generate QR Codes dynamically in some website. We already saw how easy it is to generate QR code in Java. Now we will see how to integrate this QR Code generation in a Java Servlet.

Following is a simple Http Servlet that creates QR Code using QRGen and ZXing library. User provides the text for which QR Code is generated.
The index jsp file contains a simple html form with a textbox and submit button. User can enter the text that she wishes to generate QR code of and presses submit.

File: index.jsp

<html> <head> <title>QR Code in Java Servlet -</title> </head> <body> <form action="qrservlet" method="get"> <p>Enter Text to create QR Code</p> <input type="text" name="qrtext" /> <input type="submit" value="Generate QR Code" /> </form> </body> </html>
Code language: HTML, XML (xml)

The magic happens in Here we uses QRGen library along with ZXing and generates QR Code for given text (Text we get from request.getParameter). Once the QR Stream is generated, we write this to response and set appropriate content type.


package net.viralpatel.qrcodes; import; import; import; import; import; import; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.glxn.qrgen.QRCode; import net.glxn.qrgen.image.ImageType; public class QRCodeServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String qrtext = request.getParameter("qrtext"); ByteArrayOutputStream out = QRCode.from(qrtext).to( ImageType.PNG).stream(); response.setContentType("image/png"); response.setContentLength(out.size()); OutputStream outStream = response.getOutputStream(); outStream.write(out.toByteArray()); outStream.flush(); outStream.close(); } }
Code language: Java (java)

The below web.xml simply maps with /qrservlet URL.

File: web.xml

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="" xmlns="" xmlns:web="" xsi:schemaLocation="" id="WebApp_ID" version="2.5"> <display-name>QR_Code_Servlet</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>QRCodeServlet</servlet-name> <servlet-class>net.viralpatel.qrcodes.QRCodeServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>QRCodeServlet</servlet-name> <url-pattern>/qrservlet</url-pattern> </servlet-mapping> </web-app>
Code language: HTML, XML (xml)

Download Source Code (340 KB)




Generating QR Codes in Java is not only easy, but quite straight forward. Integrating this functionality with any existing Java based app is just a piece of cake! In this tutorial we saw how to generate these QR codes in Java and also with Servlet.

Hope you’ll like this :)

Get our Articles via Email. Enter your email address.

You may also like...


  1. Good QR code tutorial

    Java Programming

  2. The hyperlink for the servlet source code is incorrect. It points to the same zip file as for the Java code example. The correct URL should be

    • Gerry, this is the second time you catching up the typos on my articles :) Thanks a ton mate!!! I’ve updated the link.

      • Yon says:

        Hi, how to start this project.
        The netbeans or eclipse?

  3. Den says:

    Here is a wrapper for using Google Charts QR-codes:

  4. Ashish says:

    nice one!

  5. raj says:

    Good one

  6. Abhijit says:

    im using the 3 .JAR files in my blackberry project..
    ive copied them into a new “lib” folder in my project folder..
    then ive added them to the build path in thru project properties..

    the classes are recognized whil coding but i get this when packaging:
    “Packaging project QR_Generator
    C:\Eclipse\plugins\net.rim.ejde.componentpack5.0.0_5.0.0.25\components\bin\rapc.exe -quiet codename=deliverables\Standard\5.0.0\QR_Generator deliverables\Standard\5.0.0\QR_Generator.rapc -sourceroot=C:\Blackberry\workspace\QR_Generator\src;C:\Blackberry\workspace\QR_Generator\res -import=C:\Eclipse\plugins\net.rim.ejde.componentpack5.0.0_5.0.0.25\components\lib\net_rim_api.jar;C:\Blackberry\workspace\QR_Generator\lib\qrgen-1.0.jar;C:\Blackberry\workspace\QR_Generator\lib\zxing-core-1.7.jar;C:\Blackberry\workspace\QR_Generator\lib\zxing-j2se-1.7.jar C:\Blackberry\workspace\QR_Generator\bin
    net/glxn/qrgen/exception/QRGenerationException.class: Error!: Invalid class file: Incorrect classfile version
    Packaging project QR_Generator failed (took 1.12 seconds) ”

  7. Pratik says:

    useful stuff…..

  8. Omar says:

    Now what….lets go for the next step…..where I can found some info about QR “READER”….I would like to develop a program for smartphones, specifically for my nokia symbian phone, Id like to have some help in this because I have some personal use for this qr reader custom-made!!Thanks for the help!!

  9. Prabhakar Deshpande says:

    Thanks Viral for sharing code on QR code encoding.

    Do you have similar code for QR code decoding?

    Can you please share it with me

  10. QR Coder says:

    I’ve only java 1.4.2 in my project..pls suggest

  11. Dev says:

    This is good and useful.

  12. VJD says:

    Can anyone tell me how to a add muitlple data in the QR code .
    Like Name,Phone number,website url ………..
    Appreciate some sample code :-)

  13. doaa says:

    it didn’t work with me no errors it run but this out put don’t appear plz help me quickly

  14. Rambabu says:

    java.lang.NoClassDefFoundError: java.awt.image.BufferedImage at at

    This is the error get when i am intigrate the project with android platform
    Can u please tell me about how to integrate the qr code generator file in android project. because AWT is not supported on Android please give me some tips to include qr code generator in android project

  15. fauzi says:

    I already try the tutorial above and it works well. But, my question is what about J2ME, how to
    make qrcode using zxing in j2me

    thank you

    • Kayla Kluge says:

      Fauzi, I am looking to do the same thing – have you found a solution?

      thank you

  16. harish says:

    Is there a way to showtext at the bottom of the QRCode Image generated. Something like ShowText() method?

  17. kash says:

    Good one…

  18. Rohit says:

    Nice Explanation….
    Is there any chance of setting the QR code Error Correction Level…
    If not what is the Error Correction Level does the QR code is assigned by zxing

  19. zoel says:

    i’m sorry, i confused with QRCode.from because i can’t see QRCode.from in core library, is there any way to do this?

  20. Pintesh Kumar says:

    i have include all the specified libraries in my class path but when i am trying to execute the code its giving following errors:
    package net.glxn.qrgen does not exist
    package net.glxn.qrgen.image does not exist
    please help me out…..Thanks.

  21. Wajeeha says:

    Its not working for android .I am getting this error “java.lang.NoClassDefFoundError: net.glxn.qrgen.QRCode ” .Any one know solution kindly shre it with me as well

  22. aneesh says:

    i want an example code to read qr code..thanks in advance

  23. Uday says:

    great Tuto

  24. nkr says:

    Thanks dude. it worked for me.

  25. rishika says:

    cant qr codes be created using C language?

  26. mark says:

    excellent post! :D

  27. shiran says:

    can you help me , and explain how to add the jar’s file to the class path?
    I have a maven project and i now i nee to add dependence to the pom.xml,
    but i don’t know what to write.

  28. Thnx..patel

  29. Don Peter says:

    I search all over the place to integrate this code with android.
    so far the only problem is with the class awt (java.awt.* ) Its not available for android.
    Is there any method by with I can integrate the code over to android,

  30. Prashant Agarwal says:

    Thanks for the tutorial :

    I have downloaded the source code and unable to run those code , it says :

    “qrservlet could find”

    Please help

  31. Ahmed says:

    Great Post Viral.

    Is there also a way to set the error correction level using this library?

  32. sainath says:

    thnx sir…its working…

  33. Laurenz says:

    Great Script.
    I only have one problem. When I create the jar file the script doesn’t work anymore. If I run it with netbeans it works correctly but when I try to use the jar file it doesn’t work anymore
    LG Laurenz

  34. Samuel Fan says:

    Hey, thanks for posting this. I tried your code. The compiler throw errors:

    cannot access net.glxn.qrgen.QRCode
    bad class file: D:\workSpace\tech\referenceClass\net\glxn\qrgen\QRCode.class
    class file has wrong version 50.0, should be 48.0
    Please remove or make sure it appears in the correct subdirectory of the classpath.
    import net.glxn.qrgen.QRCode;

    I download both qrgen-1.0.jar & qrgen-1.jar for testing. I got the above result. Am I doing something wrong?

    • Samuel Fan says:

      Never mind. I find the solution to my own problem. If I use jdk1.6, it will pass the compiler. This is what I learn and would like to share with everyone:
      jdk1.4 compiler error: class file has wrong version 50.0, should be 48.0
      jdk1.5 compiler erro: class file has wrong version 50.0, should be 49.0
      jdk1.6 compiler NO ERROR

  35. sainath says:

    thanks sir…can u sugest for decoding?

  36. ander says:

    hi, tje code works excelent, but, how can i get a bigger image??

  37. Yon says:

    help, how can I open this project .
    The Netbeans or Ecliese its ok

  38. Bharath says:

    Cool article man, Helped me Just in Time :-)

  39. galao says:

    is there any way to change the color of the generated QR image?

    • Puri says:

      I assume you have the problem that the QR code is red-ish when creating JPG files.
      This is a bug in the MatrixToImageWriter in version 1.7 of zxing javase.
      You can simply upgrade to the latest zxing version ( to fix the problem.

  40. Kike Navarro says:

    Muchas gracias, me ha servido bastante.

  41. Michael says:

    very nice

  42. Toper Del Socorro says:


  43. Tran Thai Hoa says:

    great!!! thanks you very much

  44. justin_lzy says:

    Excellent Thx

  45. Jay says: error: package net.glxn.qrgen does not exist
    import net.glxn.qrgen.QRCode;
    ^ error: package net.glxn.qrgen.image does not exist
    import net.glxn.qrgen.image.ImageType;
    ^ error: cannot find symbol
    symbol: variable ImageType
    location: class QRCodeServlet error: cannot find symbol
    ByteArrayOutputStream out = QRCode.from(qrtext).to(
    symbol: variable QRCode
    location: class QRCodeServlet
    4 errors

    It shows me these errors while compiling this code.
    Please solve it…

  46. vaibhav says:

    really works!!!!! so helpful!!

  47. vikash verma says:

    fist thanks for this code ……. its work really good and also generate OR code.
    but i want to save this QR code to the particular,plz help me and give me code for saving this image to folder.

  48. Galdino says:

    Man, thank you so much! That’s amazing this work. I was needing to do some work and I didn’t know how I Would make it. Thanks and nice Blog.

  49. Neal says:

    Link to QRGen JAR is broken (1.0 version); Can be found here:

  50. Yoshi says:

    Wow man it helps me a lot

  51. JPW says:

    This worked for me – thanks for the clear instructions and demonstration !

  52. emma says:

    When I use and do whatever you did My browser doesnot display the qr code it instead throws an exception i.e it shows
    ” javax.servlet.ServletException: Servlet execution threw an exception

    root cause

    java.lang.NoClassDefFoundError: net/glxn/qrgen/QRCode

    root cause

    java.lang.ClassNotFoundException: net.glxn.qrgen.QRCode

    What could be the problem?

  53. Lennonerd says:

    integrate qr code scanner for android in c#

  54. Johnny says:

    please, i use this source code to generate QR code as servlet and it works, but i need to recuperate the image in image tag as

    <image src=" ">

    and thanks for help

  55. Showeb says:

    Very nice examples.

  56. Chandan says:

    Please anyone let me know how to read a contents of a file and generate the QR codes


  57. bobbell2 says:

    Hello,i find a sample code of how to generate qr qode in java class,but i am not professional in this files,can anyone tell me is this code available?
    Sample code:(

    public static void main(String[] args)

    BarCode barcode = new BarCode();


    { barcode.draw(“c://qrcode.gif”);

    catch (Exception e)

  58. Deeptomay Roy says:

    it shows
    ” javax.servlet.ServletException: Servlet execution threw an exception”

    Please help .

  59. Kat Rollo says:

    For those who might encounter “NoSuchMethodError”, it means the qrgen jar is not compatible with the zxing 1.7 jars.

    To fix this, it’s either you
    (1) get the updated zxing jars to work with the latest qrgen jar or
    (2) get a lower version of qrgen to work with the zxing 1.7 jars.

    I took option 2 and I can confirm qrgen-1.2.jar works with the zxing version on this page.

    The QRGen download link also no longer exists, the new download page can be found at|-852965118

  60. Anil says:

    The above code which you have provided is perfectly working fine with one input textbox.
    What i need is to generate the QR code with more Textboxes.
    Can any one help me in this regard?
    Thanks in advance.

  61. Abhinav Gupta says:

    Hello Sir I want to make a card format with this qr code… Please help me
    [ code java] package net.viralpatel.qrcodes;

    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import net.glxn.qrgen.QRCode;
    import net.glxn.qrgen.image.ImageType;

    public class QRCodeServlet2 extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    String qrtext = request.getParameter(“qrtext”);

    ByteArrayOutputStream out = QRCode.from(qrtext).to(


    OutputStream outStream = response.getOutputStream();




  62. Medea says:

    Hello Sir.
    Thank you, it helps me a lot :))

  63. Lai says:

    Hello, actually i need do a QR Code scanner system ,also included generate QR Code function but i not familiar in Java. So can you teach me and help me?Please.

  64. Sir/Madam i m new to java coding. i not getting how to run the above mentioned code.Please help me

  65. naylay says:

    i have face this error , So what should i do ? pls

    Exception in thread “main” java.lang.NoSuchMethodError:;Lcom/google/zxing/BarcodeFormat;IILjava/util/Map;)Lcom/google/zxing/common/BitMatrix;
    at net.glxn.qrgen.QRCode.createMatrix(
    at net.glxn.qrgen.QRCode.writeToStream(
    at ictti.CodeGen.main(

  66. sarika bhutare says:

    How to download the generated QR code image.
    Please guide me….

  67. Psalmgajey says:


    i click more time but not get .some error page.( (340 KB))

    how to download this zip file.please guide me.

Leave a Reply

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