Thursday 11 May 2017

How to split a string contians special characters in java

Java Split String Example showing special characters or regular expressions (regex):



Sometimes we need to split String in java, which contains the special characters such as '(',  '*' ,  '&', '<' ,  '^' etc., .

Now we will see how to split them in java.

The following example shows how to split a special characters '(', '*'.


Example: Splitspecialcharacter.java




package com.practice.corejava;

public class Splitspecialcharacter {

              public static void main(String[] args) {

                             String str = "CLOTHS(10*12*13*7)";
                                           String[] mystr = str.split("\\(");
                                          
                                           System.out.println("Splitted string After the special character '(':");
                                           for(String str2 : mystr){
                                                         
                                                          //Displaying the string after splitting it after the special character '('
                                                          System.out.println(str2);
                                                         
                                                          if(str2.contains("*")){
                                                                       
                                                                        String[] tempString = str2.split("\\*");
                                                                        System.out.println("Splitted string after the special character
                                                  '*':");
                                                                        for(String temp: tempString){
                                                                                      //Displaying the string after splitting it after the special
                                                                        character '*'
                                                                                      System.out.println(" "+temp);
                                                                                       }
                                                                        }                                         
                                                          }
                                     }
                                   }

Now we will run the above example in eclipse and see the output as follows:

Console:

Split After the special character '(':
 CLOTHS
 10*12*13*7)

Spliting after the special character '*':
 10
 12
 13
 7)



Read More »

Saturday 6 May 2017

Javax.xml.bind.JAXBElement Class

Javax.xml.bind.JAXBElement Class


The javax.xml.bind.JAXBElement class is the JAXB representation of an Xml Element.This class represents information about an Xml Element from both the element declaration within a schema and the element instance value within an xml document with the following properties:

1.   element's xml tag name
2.   value represents the element instance's atttribute(s) and content model
3.   element declaration's declaredType  (xs:element @type attribute)
4.   scope of element declaration
5.   boolean nil property. (element instance's xsi:nil attribute)

 

Class declaration:      


Following is the declaration for javax.xml.bind.JAXBElement class:

public class JAXBElement<T>
   extends Object
      implements Serializable


Field


Following are the fields for javax.xml.bind.JAXBElement class:



protected Class<T> declaredType − This is the Java datatype binding for xml element declaration's type

protected QName name − This is the xml element tag name.

protected boolean nil −   This is true if the xml element instance has          
                                   xsi:nil="true".

protected Class scope −  This is the scope of xml element declaration
                                  representing this xml element instance.

protected T value − This is the xml element value.



Methods inherited


This class inherits methods from the following classes −
javax.xml.Object

Example



The following example shows the usage of java.xml.bind.JAXBElement class. To proceed, consider the following Employee class which will be used to have objects for marshalling and unmarshalling purposes.


Employee.java



package com.jaxb.demo;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Employee { 
       private int employeeId;      
       private String employeeName;      
       private double employeeSal; 
       /**
        * @return the employeeId
        */
       public int getEmployeeId() {
             return employeeId;
       } 
       /**
        * @param employeeId the employeeId to set
        */
       @XmlAttribute
       public void setEmployeeId(int employeeId) {
             this.employeeId = employeeId;
       } 
       /**
        * @return the employeeName
        */
       public String getEmployeeName() {
             return employeeName;
       } 
       /**
        * @param employeeName the employeeName to set
        */
       @XmlElement
       public void setEmployeeName(String employeeName) {
             this.employeeName = employeeName;
       } 
       /**
        * @return the employeeSal
        */
       public double getEmployeeSal() {
             return employeeSal;
       } 
       /**
        * @param employeeSal the employeeSal to set
        */
       @XmlElement
       public void setEmployeeSal(double employeeSal) {
             this.employeeSal = employeeSal;
       }

}

Now let us create main class which will be used to marshal ie. convert Student object to XML. Here we will create a JAXBElement of a Student object and then update StringWriter object using JAXBContext. This example marshals the Student object and prints the XML.


JAXBDemoApp.java


package com.jaxb.demo;

import java.io.StringWriter;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.namespace.QName;

public class JAXBDemoApp {

       public static void main(String[] args) {
             //Create a employee object
             Employee employee = new Employee();

             //fill details of the employee
             employee.setEmployeeName("Best employee");
             employee.setEmployeeId(1);
             employee.setEmployeeSal(12000.00);

             try {

                 /*
                                         *  create JAXBElement of type Student
                                             Pass it the employee object
                                          */
                JAXBElement<Employee> jaxbElement =  new JAXBElement(
                   new QName(Employee.class.getSimpleName()), Employee.class,
                                                       employee); 
                /*
                                      * Create a String writer object which will be
                                          used to write jaxbElment XML to string
                                         */
                StringWriter writer = new StringWriter();

                // create JAXBContext which will be used to update writer            
                JAXBContext context = JAXBContext.newInstance(Employee.class);

                // marshal or convert jaxbElement containing student to xml format
                context.createMarshaller().marshal(jaxbElement, writer);
            
                //print XML string representation of Student object             
                System.out.println( writer.toString() );

             } catch (JAXBException e) {                  
                e.printStackTrace();
             }
          }
      }




Read More »

Thursday 4 May 2017

org/jboss/logging/BasicLogger: Hibernate Error Exception in thread "main" java.lang.NoClassDefFoundError: org/jboss/logging/BasicLogger

Hibernate Error Exception in thread "main" java.lang.NoClassDefFoundError: org/jboss/logging/BasicLogger



The following exception is being thrown due to missing of the library jboss-logging-3.1.0.GA.jar.

Exception in thread "main" java.lang.NoClassDefFoundError: org/jboss/logging/BasicLogger
       at java.lang.ClassLoader.defineClass1(Native Method)
       at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
       at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
       at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
       at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
       at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
       at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
       at java.security.AccessController.doPrivileged(Native Method)
       at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
       at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
       at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
       at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
       at org.hibernate.cfg.Configuration.<clinit>(Configuration.java:176)
       at com.hibernate.utils.HibernateUtil.getSessionFactory(HibernateUtil.java:15)
       at com.hibernate.application.RunApplication.main(RunApplication.java:16)
Caused by: java.lang.ClassNotFoundException: org.jboss.logging.BasicLogger
       at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
       at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
       at java.security.AccessController.doPrivileged(Native Method)
       at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
       at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
       at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
       at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
       ... 15 more

Solution:


Download the library jboss-logging-3.1.0.GA.jar and add it to the class path as shown below:





Or

Add the dependency in Maven’s pom.xml

<dependency>
      <groupId>jboss-logging</groupId>
      <artifactId>jboss-logging</artifactId>
      <version>3.1.0</version>

Read More »

Monday 1 May 2017

org/dom4j/DocumentException: Hibernate Error: Initial SessionFactory creation failed java.lang.NoClassDefFoundError:org/dom4j/DocumentException


Hibernate Error: Initial SessionFactory creation failed

java.lang.NoClassDefFoundError:org/dom4j/DocumentException


The below exception is very common and caused by missing the dependency library – dom4j.


Exception in thread "main" java.lang.NoClassDefFoundError: org/dom4j/DocumentException
       at com.hibernate.utils.HibernateUtil.getSessionFactory(HibernateUtil.java:15)
       at com.hibernate.application.RunApplication.main(RunApplication.java:16)
Caused by: java.lang.ClassNotFoundException: org.dom4j.DocumentException
       at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
       at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
       at java.security.AccessController.doPrivileged(Native Method)
       at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
       at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
       at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
       at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
       ... 2 more

Solution:

Download the dom4j  and the library jar file to the eclipse class path as shown below:


You can download the library here – 

Or

Add the dependency in Maven’s pom.xml
<dependency>
      <groupId>dom4j</groupId>
      <artifactId>dom4j</artifactId>
      <version>1.6.1</version>

</dependency>

You may also like to read the below posts:




Read More »