Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Monday, 24 April 2017

Set font style in a table for a row. in apache poi


XWPFTable table = document.createTable();
XWPFTableRow row = table.insertNewTableRow(0);
XWPFRun run = row.addNewTableCell().addParagraph().createRun();
run.setBold(true);run.setText("Your Text");

ref : how-to-format-the-text-in-a-xwpftable-in-apache-poi

Saturday, 27 August 2016

Java 8 JDBC

Java 8 : The JDBC-ODBC Bridge has been removed.

There is plenty of option to try. refer the Stackoverflow for futher references. 


Java 1.8 Concurrency feature "ConcurrentHashMap as a Set"

Java 1.8 Concurrency feature "ConcurrentHashMap as a Set"

The feature will return set of key in the map as Set.

public class VoiceConcurrentHashMap {

public static void main(String[] args) {

ConcurrentHashMap<String,String> map = new ConcurrentHashMap<String, String>();
System.out.println(map);
map.put("Lnkedin", "Networking");
map.put("Microsoft", "Windows10");
map.put("facebook", "Social");
map.put("Google", "SearchEngine");

KeySetView<String,String> set = map.keySet();
System.out.println(set);
}

}

Result : [Google, facebook, Microsoft, Lnkedin] .

Image :


ref :
Java API 


Behaviour of ConcurrentHashMap when it has Duplicate key.

Behaviour of ConcurrentHashMap when it has Duplicate key.

ConcurrentHashMap will overwrite previous value for a given key in Map. The object whos reference is lost are eligible for garage collection.

public class VoiceConcurrentHashMap {

public static void main(String[] args) {

ConcurrentHashMap<String,String> map = new ConcurrentHashMap<String, String>();
System.out.println(map);
map.put("Lnkedin", "profile");
map.put("Lnkedin", "profile");
map.put("Lnkedin", "jagdeesh");

}
}

ref :
1. Java API
2. Stackoverflow


Saturday, 2 July 2016

difference betweeh length and length() in java

difference betweeh length and length() in java

description : To find the length of array we use legth.

ex : define and intiger array and find the length of the array.
int[] number =  new int[3];
number[0] = 1;
number[1] = 3;
number[2] = 5;

int secondHighest = number.length;
output : 3

description : To find the length of string we use legth().

String name = "voice";
System.out.println(name.length() + " length");

output : 5

Wednesday, 15 June 2016

creating a maven web project from commandline

1)
a. creating a maven project from commandline

mvn archetype:generate -DgroupId={project-packaging}
-DartifactId={project-name}
-DarchetypeArtifactId=maven-archetype-webapp
-DinteractiveMode=false

ref : maven

b) need to create sample web application

ex : mvn archetype:generate -DarchetypeGroupId=com.sample.voice -DarchetypeArtifactId=maven-archetype-webapp -DarchetypeVersion= voice


mvn archetype:generate -DgroupId=com.voice -DartifactId=sample -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

Thursday, 26 May 2016

Functional style in java.

Functional style in java.

Function<T,R> function = new  Function<T,R>();
Type Parameters:
T - the type of the input to the function
R - the type of the result of the function

Ref : Oracle

ex :
It is the explicit intent to make it more convenient to program in a functional style in Java. Now, in mathematics, a function is generally written like

f: A -> B
(i.e., a function from As to Bs). This corresponds also to the notation in functional languages, Scala and already existing functional libraries for Java.

Ref : Stackoverflow 

Tuesday, 24 May 2016

WARN (SqlExceptionHelper.java:233) - Unknown column 'xxx' in 'field list'

 WARN  (SqlExceptionHelper.java:233) - Unknown column 'EditedOn' in 'field list'

Solution :
1) In hibernate check the entity class.
2) In table check the column names.
3) Compare both the entity and column name.
4.Caused due to spelling mistake.

Saturday, 21 May 2016

scope of public,private,protected in java for class method

public : method which are public there scope is available to child and which ever class wants to use it.
protected : scope visible only in child package
private :  scope visible only in class. 

Difference between pathparam and QueryParam in Rest.

Difference between pathparam and QueryParam in Rest.

Pathparam : used get the part of the string from URL.
ex:  @Pathparam("id")
ex : http://localhost:8080/voice{id}

QueryParam  : using this we are able to access the key,value after from URL after "?"
ex:  @QueryParam("id")
ex : http://localhost:8080/voice?id=1009

Log file in tomcat.


1) catalina.log -- logs all the  log at the time of server start
2) localhost_access_log : logs all the request type and URI
ex :
a) 0:0:0:0:0:0:0:1 - - [21/May/2016:11:21:52 +0530] "GET /voice/p1x/network HTTP/1.1" 200 -
3) server.log : logs "ContainerBackgroundProcessor" info and error stack.
4) error.log : logs all the error stack
5) hostmamager.log :
6) manager.log : logs info about HTMLManager,manager,
ex : INFO: HTMLManager: init: Associated with Deployer 'Catalina:type=Deployer,host=localhost'
ex : INFO: HTMLManager: init: Global resources are available
ex : INFO: HTMLManager: list: Listing contexts for virtual host 'localhost'

Friday, 20 May 2016

what is purpose of calling chain.doFilter() in servlet


what is purpose of calling chain.doFilter() in servlet
chain.doFilter(request, response);

The name chain suggests that you have a sequence of filters, with each filter doing some processing and then passing on to the next in sequence, so each object has a chain member to point to the next filter in the sequence, which gets called after the filter has performed its own processing. The last in the sequence will then probably have null as the chain value, or it knows on its own that it is the last one in the sequence.

Ref : stackoverflow
author JK

public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
...

chain.doFilter(request, response);
}

after executing the method  on calling chain.doFilter() it will call filter present in web.xml in the order of defination.


<filter>
  <filter-name>VoiceLoginFilter</filter-name>
  <filter-class>com.voice.filters.VoiceLoginFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>VoiceLoginFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

when to use boolean and Boolean in java

when to use boolean and Boolean
1. if you want to have true or false use boolean.
2. if you want to have true or false or Null use boolean.

Ref : stackoverflow

Thursday, 19 May 2016

Generic Maps in Collections


Generic Maps in Collections
IF we want to restrict the type of object we can go for Generic Map using Map<key,value> pair

Map<String, Object> map = new HashMap<String, Object>();
In the above example the map object will accept key of type string and value of type Object

Wednesday, 11 May 2016

Can not construct instance of java.util.Date from String value

Can not construct instance of java.util.Date from String value

SEVERE: Servlet.service() for servlet [Jersey Rest Service] in context with path [/voice] threw exception [com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not construct instance of java.util.Date from String value '12-01-2016': not a valid representation (error: Failed to parse Date value '12-01-2016': Can not parse date "12-01-2016": not compatible with any of standard forms ("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd"))
 at [Source: org.apache.catalina.connector.CoyoteInputStream@aca01d2; line: 6, column: 22] (through reference chain: com.voice.dto.VoiceSurveyRequestDto["feedbackAt"])] with root cause
com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not construct instance of java.util.Date from String value '12-01-2016': not a valid representation (error: Failed to parse Date value '12-01-2016': Can not parse date "12-01-2016": not compatible with any of standard forms ("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd"))

From the error stack provide date in following format.
1. yyyy-MM-dd'T'HH:mm:ss.SSSZ"
ex : 2016-10-01T09:45:00.000

2."yyyy-MM-dd'T'HH:mm:ss.SSS'Z'".
ex : 2016-10-01T09:45:00.000

3. "EEE, dd MMM yyyy HH:mm:ss zzz"
ex : 2016-10-01T09:45:00.000

4."yyyy-MM-dd"
ex : 2016-10-01

Saturday, 7 May 2016

why to choose enum in java

why to choose enum in java

This are special data type that enables for a variable to set  predefined constants REF[1]

If you use enums instead of integers (or String codes), you increase compile-time checking and avoid errors from passing in invalid constants, and you document which values are legal to use REF[2]


Let us take an example where you want to send the status code for service call requested.

ex :

define a service class which will take a request

# make a Http request.
@GET
#define the path for request.
@PATH(/voice)
#define mediatype to produce/consume
@Produces(MediaType.APPLICATION_JSON)
#start the service method
public Response VoiceResponse(){
ResponseBuilder responseBuilder = Response.status(ServerStatus.Server_Error);

}



Because they are constants, the names of an enum type's fields are in uppercase letters. REF[1]

define a class of enum type.

public enum ServerStatus {

#define status code
SUCCESS(200,'OK'),

#define server error.
Server_Error(500,'Internal server error')
}





REF :
1.oracle
2. stackoverflow

Monday, 25 April 2016

set the java path permanently in linux

c) set the java path

export JAVA_HOME=/usr/java/jdk1.8.0_45

d) Setup JRE_HOME Variable

export JRE_HOME=/usr/java/jdk1.8.0_45/jre

e) Setup PATH Variable

export PATH=$PATH:/usr/java/jdk1.8.0_45/bin:/usr/java/jdk1.8.0_45/jre/bin

environment variable :

/etc/environment

to set the variable permanently.

go to directory
/etc
open profile.
cmd : vi profile

set all the variable at the bottom of the pofile.
check from new command window weather you are able to see the path.

find java path in linux.

find java path in linux.

a) which java
output : /usr/bin/java.

b) readlink -f $(which java)
output : /usr/java/jdk1.8.0_45/jre/bin/java

Thursday, 17 March 2016


Java was started but returned exit code=13. while starting the eclipse.

This is happen because of you might have updated addon/plugin and might have installed a older version of java which will set path to the older version.

So here are the solution what i have tried.

1.uninstall the older version of  java.
2. set the latest java path to the environment variable.
3. Restart the eclipse.