Saturday 3 November 2012

Formatting Code in eclipse

Eclipse has one beautiful feature to create a new rule to format code in eclipse and then export the same set of rules in XML format and import the same set of rules(XML File) and hence supports quick formatting.
It is used for formatting indentation,braces,white spacing,blank lines,new line,control statement,line wrapping and comment section.

Steps to follow to create a set of rules for formatting the code according to your business requirements are described below:-
  1. Go to Windows(Ctrl + W) -> Preferences(P).Now select Java -> Code Style -> Formatter.
    Formatting in Eclipse
    Formatting in Eclipse
  2. Create a new profile.Click on new and give a name to your profile.
    Creating new Formatting Profile
    Creating new Formatting Profile
  3. Set the rules for formatting by checking the rules that you want to use in your application.
    Create rules for formatting
    Create rules for formatting
    For formatting the width,Select Line Wrapping and set maximum line width and set line breaking policy and indentation policy for Class Declarations,Constructor Declarations,Method declarations,enum Declarations,Function Calls,Expressions,Statements.After setting all the formatting rules,click on apply -> OK.You can see the code after formatting preview in the Preview of the setting.
    Line Wrapping rules for formatting
    Line Wrapping rules for formatting
  4. Click on Export in top right corner to export these settings.Give a name to the file and Click on Save.
    Export these Settings
    Export these Settings
  5. To use these settings throughout the project team,import these settings by importing the XML file created above and click on Apply-> OK.
  6. If you want this setting to be applicable for a particular project.Go to Project Properties,select Formatter from Java Code Style and click on the check-box Enable project specific settings.Select the Active Profile and click on Apply.
    Project Specific Settings
    Project Specific Formatting
  7. Now to apply formatting to the code everytime you save a File,follow following process:-
    Under Preferences, choose Java -> Editor -> Save Actions. Check the "Perform the selected actions on save", and check the "Format source code" box.You can select the profile to be used for formatting from there.

  8. Njoy...:)

Wednesday 24 October 2012

Deployment in Tomcat Apache Server


  1. Export the project in WAR file by selecting the project to deploy -> Export -> WAR file

    Select Project > Export >  WAR file
    Export project as WAR
  2. There are two ways to deploy WAR file in Apache Tomcat Server.First is through tomcat manager.The steps to deploy WAR from tomcat manager is described below:-
    1.            Open tomcat-users.xml present at  appBase location(default appBase is "$CATALINA_BASE)/config or from eclipse go to server >Tomcat v6.0 Server at localhost-config and add user with manager role.For Ex:- Add the following line
      <user username="admin" password="admin" roles="manager"/>

      tomcat-users.xml
      tomcat_users.xml
    2. Start the tomcat server by executing startup.bat file present at bin folder of apache-tomcat.Open the Apache Manager by clicking http://localhost:8080 and select Tomcat Manager.   
      http://localhost:8080/
      Apache tomcat manager
      Enter user name and password created using tomcat-users.xml.For our case it will be :-

      Username = “admin”

      Password=”admin"
    3. Upload WAR file as shown below.
      Add WAR file
      Add WAR file

  3. Deployment on Tomcat startup
    1. The location you deploy web applications to for this type of deployment is called the appBase which is specified per Host. Copy the WAR file to be deployed to appBase location.(default appBase is "$CATALINA_BASE/webapps").This method will work only if Host's deployOnStartup attribute is "true".








Saturday 30 June 2012

Creating new Profile in WAS





Creating and configuring Profile in WAS server with SCA Support


  1.  First go to windows -> preferences

  2.  

  3.  Then Click on Server -> WebSphere Application Server


  4.  Click on Run Profile Management Tool and select yes in the pop up box.You will get this window(If they above process doesn’t work,then go to C:\Program Files(x86)\IBM\SDP\runtimes\base_v7\bin\ProfileManagement and click on pmt.bat)


  5.  Click on Launch Profile Management Tool. You will get the window shown below.


  6.  Click on Create and Select Application Server and click on next.Again click on next.
  7.  Uncheck the Enable Administrative Security(IF U DON'T WANT LOGIN USERNAME AND PASSWORD) and click on next.


  8.  Click on create -> Finish and the new profile with AppSrv02 is created.
  9.  Select the newly created profile and click on Augment 


  10.  Add one by one all augments (Application Server with Feature Pack SCA must be included otherwise it won’t support SCA)


If you have any other problem in configuring WAS,feel free to ask or mail me @ adiadidas9000[at]gmail.com



Sunday 3 June 2012

DOS Tricks in JAVA

One of the most important DOS command is tasklist command.We can perceive this command in many ways.

"tasklist" is a DOS command that displays the running processes in the Windows OS.It displays all the applications and services running with their PID(process ID).


Syntax

tasklist[.exe] [/s computer] [/u domain\user [/p password]] [/fo {TABLE|LIST|CSV}] [/nh] [/fi FilterName [/fi FilterName2 [ ... ]]] [/m [ModuleName] | /svc | /v]

ParametersUses
/S systemSpecifies the remote system to connect to.
/U [domain\]userSpecifies the user context under which the command should execute.
/P [password]Specifies the password for the given user context. Prompts for input if omitted.
/M [module]Lists all tasks currently using the given exe/dll name. If the module name is not specified all loaded modules are displayed.
/SVCDisplays services hosted in each process.
/VDisplays verbose task information.
/FI filterDisplays a set of tasks that match a given criteria specified by the filter.
/FO formatSpecifies the output format. Valid values: "TABLE", "LIST", "CSV".
/NHSpecifies that the "Column Header" should not be displayed in the output. Valid only for "TABLE" and "CSV" formats.


Filters
Filter NameValid OperatorsValid values
STATUSeq, neRUNNING | NOT RESPONDING | UNKNOWN
IMAGENAMEeq, neImage name
PIDeq, ne, gt, lt, ge, lePID value
SESSIONeq, ne, gt, lt, ge, leSession number
SESSIONNAMEeq, neSession name
CPUTIMEeq, ne, gt, lt, ge, leCPU time in the format of hh:mm:ss.hh - hours,mm - minutesss - seconds
MEMUSAGEeq, ne, gt, lt, ge, leMemory usage in KB
USERNAMEeq, neUser name in [domain\]user format
SERVICESeq, neService name
WINDOWTITLEeq, neWindow title

MODULES

eq, ne

DLL name

 


Now let us see how we can make utilization of this command with the help of some examples:-

  •  tasklist /s Computer
tasklist /s
Process info of local/remote System
  • tasklist -v
  • tasklist -fi
tasklist -fi
Filter Result of tasklist
  • tasklist -v -fi "ImageName eq VLC.exe" /fo csv
To get VLC Windows Title


CODE SNIPPET to get current sound Track name in VLC


public String CallMe()
{
  Runtime runtime = Runtime.getRuntime();
           String cmds[] = {"cmd", "/c", "tasklist","/v","/fi","ImageName eq VLC.exe","/fo","CSV"};
           Process proc;
           String val = null;
try {
proc = runtime.exec(cmds);
           InputStream inputstream = proc.getInputStream();
           InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
           BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
           String line;
           int count = 0;
while ((line = bufferedreader.readLine()) != null) {
   if(count==1)
   {
   String[] a = line.split(",");
val = a[9];
   }
   count++;
   }
} catch (IOException e) {
e.printStackTrace();
}
return val;
}


This can be used to set curent soundtrack as your GTalk status by using SMACK api to communicate with GOOGLE.
This will be explained in more depth in my next Blog.

Sunday 15 April 2012

Three Sum Problem

A three sum problem is one in which sum of 3 numbers from a given inter array is zero.Find all pairs from the given array of n size along with the algorithm complexity.
A[i]+A[j]+A[k]=0

Approach

1) Sort the array
2) Loop i from 1 to n.
3)Initialize j to i and k to n-1
4)while k>j:do
  • sum = a[i]+a[j]+a[k]
  • if sum greater than zero increment j
  • else decrement k
 Complexity :-O(n2)

For Eg:-
In JAVA,
The source code for ThreeSum problem will be:-


package com.collection.collection_ex;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

public class ThreeSum {
static Set<ArrayList<Integer>> l=new HashSet<ArrayList<Integer>>();
public static void main(String args[])
{
int[] array = {2,7,6,-7,0,8,-3,-5,-1,7,-7,14};
System.out.println(threeSum(array));
System.out.println(threeSum(array).size());
}
public static Set<ArrayList<Integer>> threeSum(int[] array) {
   if(array == null) return null;
   int n = array.length;
   if(n < 3) return null;
   Arrays.sort(array);
   int count = 0;
   for(int i = 0; i < n - 1; i++) {
       int j = i ;
       int k = n - 1;
       while(k >j) {
        count++;
           int sum = array[i] + array[j] + array[k];
           if(sum == 0)
           {
            ArrayList<Integer> s = new ArrayList<Integer>();
                     s.add(array[i]);
                     s.add(array[j]);
                     s.add(array[k]);
                     Collections.sort(s);
                     l.add(s);
           }
           
           if(sum < 0) j++; else k--;
       }
   }
   return l;
}
}