Friday, February 3, 2017

How to install Apache Poi in Java using Maven (eclipse)

If we need to use apache poi in a Maven Project, we can follow the next simple steps.

We enter to the next link https://mvnrepository.com/artifact/org.apache.poi/poi there we can choose the version of Apache Poi that we prefer, in this example i will use the 3.5 version

We copy the dependency and we download the Jar, as we can see in the following figure
dependency of apache poi for Maven project Java

In this example i saved the Jar in the following directory : D:\Poi\poi-3.15.jar

Later we going to Eclipse, and we press right click on the project -> Run As -> Maven Build...

In the field Goals we put:

 install:install-file -Dfile="D:\jdbc\poi-3.15.jar" -DgroupId=org.apache.poi -DartifactId=poi -Dversion=3.15 -Dpackaging=jar  

Click on Apply And latter click on Run.

Finally we open the pom.xml File and we paste the dependency:

 <dependency>  
    <groupId>org.apache.poi</groupId>  
    <artifactId>poi</artifactId>  
    <version>3.15</version>  
 </dependency>  

Now we can use the apache poi library in our java project with Maven

Thursday, January 26, 2017

How to install the Sqljdbc4 for use Sql server with hibernate, spring, maven and eclipse

If we want to use sql server with a maven project that uses hibernate and spring we need the jdbc for sql server, but the maven has not integrate this jar, for this reason we have install it manually, for get it we can follow these steps.

First we must download the jar jdbc for sql server, we can download it from this link
https://mvnrepository.com/artifact/com.microsoft.sqlserver

In this example i have downloaded the Sqljdbc4 and i saved in the D:\jdbc\

Later Right click on the project, click on Run As ->Maven Build...

In the field goal you must put the code

 install:install-file -Dfile="D:\jdbc\sqljdbc4.jar" -DgroupId=com.microsoft.sqlserver -DartifactId=sqljdbc4 -Dversion=4.0 -Dpackaging=jar  


Click on Apply -> Click on Run

The next step is put in the pom.xml file the next code

 <!-- https://mvnrepository.com/artifact/com.microsoft.sqlserver/sqljdbc4 -->  
 <dependency>  
   <groupId>com.microsoft.sqlserver</groupId>  
   <artifactId>sqljdbc4</artifactId>  
   <version>4.0</version>  
 </dependency>  


With these steps you will have the jdbc for sql server ready in the maven project

Wednesday, January 25, 2017

Java Create a Barcode using BarCode4J And Maven

If we want to create a barcode we can use the Barcode4J library.


If we are using Maven the dependency that we can use is this:

 <!-- https://mvnrepository.com/artifact/net.sf.barcode4j/barcode4j -->  
 <dependency>  
   <groupId>net.sf.barcode4j</groupId>  
   <artifactId>barcode4j</artifactId>  
   <version>2.1</version>  
 </dependency>  
Ref: https://mvnrepository.com/artifact/net.sf.barcode4j/barcode4j/2.1


 If we are using Gradle the dependency that we can use is this:

 // https://mvnrepository.com/artifact/net.sf.barcode4j/barcode4j  
 compile group: 'net.sf.barcode4j', name: 'barcode4j', version: '2.1'  
Ref: https://mvnrepository.com/artifact/net.sf.barcode4j/barcode4j/2.1


The code in Java that we can use for create a barcode using Barcode4J is the next:

 import java.awt.image.BufferedImage;   
  import java.io.File;   
  import java.io.FileNotFoundException;   
  import java.io.FileOutputStream;   
  import java.io.OutputStream;   
  import org.krysalis.barcode4j.impl.code39.Code39Bean;   
  import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;   
  import org.krysalis.barcode4j.tools.UnitConv;   
  public class CodeBarServicesImpl implements CodeBarServices{   
    public void generateCodeBar(String codeBarValue) throws Exception {   
       String fullPath = null;   
       try {   
         Code39Bean bean = new Code39Bean();   
         final int dpi = 150;   
         String path = "D:\\";   
         String extension = ".jpg";   
         fullPath = path + codeBarValue + extension;   
         // Configure the barcode generator   
         bean.setModuleWidth(UnitConv.in2mm(1.0f / dpi)); // makes the narrow   
         bean.setWideFactor(3);   
         bean.doQuietZone(false);           
         File outputFile = new File(fullPath);   
         OutputStream out = new FileOutputStream(outputFile);   
         try {             
            BitmapCanvasProvider canvas = new BitmapCanvasProvider(out,   
            "image/jpeg", dpi, BufferedImage.TYPE_BYTE_BINARY,   
            false, 0);             
            // We Generate the barcode   
            bean.generateBarcode(canvas, codeBarValue);   
            canvas.finish();   
         } finally {   
            out.close();   
         }    
       } catch (Exception e) {           
         e.printStackTrace();   
         throw new Exception(e);   
       }    
    }      
  }