Sunday, February 12, 2012

Excellent tip for importing static methods in eclipse

One of the great features of Java 1.5 is Static Imports. In order to configure Eclipse to search for static imports in a particular class, you have to perform the following steps:


1. Navigate to Preferences by clicking on the Window -> Preferences Menu Item

2. Navigate to Java -> Editor -> Content Assist -> Favorites using the menu tree (or search for Favorites using the search bar at the top)

3. Click the New Type button
4. Type in the name of the Class that has static methods that you would like to be used when using Eclipse's Content Assist / Code Completion (eg Assert)
5. Click on the Browse button which will bring up the Open Type Dialog using what you entered previously as the search criteria
6. Find the class that you would like to add, and then click Okay on the Open Type Dialog
7. Then Click Okay on the New Type Favorite Dialog.

Now when you are editing Java code, instead of typing Assert.assertEquals, you only need to type assertEquals, with Ctrl-Space, and the Assert Type will be searched for in order to resolve the static import.

Sunday, February 05, 2012

Inject Util Class with Google Guice vs static Methods?

http://stackoverflow.com/questions/4370683/inject-util-class-with-google-guice-vs-static-methods


It depends on the nature of your convert() method.
If it's something
  • simple
  • deterministic (i.e. doesn't depend on additional parameters)
  • have no side effects
  • is unlikely to change
  • etc
you can keep it as a static utility method.
Otherwise it's a good candidate for dependecy injection (you can rename it to ConversionService to make it more clear).

Monday, January 30, 2012

Adding Recent File List Plugin to VIM

From: http://stackoverflow.com/questions/3171284/recent-file-history-in-vim

MRU (Most Recently Used) Plugin
http://www.vim.org/scripts/script.php?script_id=521

How to add on unix:
On vim enter:
:help add-plugin


Steps:

mkdir ~/.vim
mkdir ~/.vim/plugin
cd 
~/.vim/plugin
wget -O mru.vim http://www.vim.org/scripts/download_script.php?src_id=11919


To Use:
While working on vim, enter the command:

1. :MRU
2. Select a file, press enter, thats it.

ProcessBuilder Runtime Redirecting Output and Exec uting

The following discuss details of the problem:


ProcessBuilder redirecting output


http://stackoverflow.com/questions/5986324/processbuilder-redirecting-output

Shell redirection operators are unknown to ProcessBuilder. Put your command in a shell script and execute it, as shown here. Alternatively, use bash -c, as shown here.



Or prepending "bash -c" to the exec


http://stackoverflow.com/questions/5740390/printing-my-macs-serial-number-in-java-using-unix-commands/5740673#5740673


How to use OutputStream for output redirection


http://stackoverflow.com/questions/3777654/problem-with-runtime


Split a string containing command-line parameters into a String[] in Java


http://stackoverflow.com/questions/3259143/split-a-string-containing-command-line-parameters-into-a-string-in-java
Wonderful answer here:


Here is a pretty easy alternative for splitting a text line from a file into an argument vector so that you can feed it into your options parser:
This is the solution:
public static void main(String[] args) {
    String myArgs = Commandline.translateCommandline("-a hello -b world -c \"Hello world\"");
    for (String arg:myArgs)
        System.out.println(arg);
}

Git User Manual, Looks Very Good

Git, how to diff a file with its previous commits

git diff HEAD 0d82 -- myfile.java

Git, How to revert to a previous commit?

Best on this is:http://stackoverflow.com/questions/4114095/git-revert-to-previous-commit-how

I like this way:

git checkout 0d1d7fc32 .
git commit
Also you can do the following if you have some local modifications:
git stash
git checkout 0d1d7fc32
git stash pop
git commit


Friday, January 27, 2012

Saturday, January 14, 2012

Display files modified in the last 10 minutes on linux

find /var/log -type f -mmin -10

Wonderful command.

Monday, December 12, 2011

How to comment code in VI or VIM


Mark the area which is to be commented using the *blockwise* visual mode (CTRL-V, in Windows this is CTRL-Q).

Press I (capital i) and write the text you want to prepend to each line of the selected block, e.g. #####.

Then press ESC and the text will be inserted to the left of each line of the selected block.

getResouce and Files in java. I just hate having to search for this every time.

To get a file inside a jar, you have to do the following:

URI resourceURI = this.class.getResource("/path/to/file/in/jar/config.txt").toURI();

To open the file using a file you can do:

File theFile = new File(resourceURI);

To read the contents of the file:

List<String> lines = Files.readLines(theFile, Charsets.UTF_8);


To read the files in a directory:



// Load the directory as a resource
URL dir_url = ClassLoader.getSystemResource(dir_path);
// Turn the resource into a File object
File dir = new File(dir_url.toURI());
// List the directory
String files = dir.list()

Monday, October 31, 2011

"Run On Server" not showing up in Eclipse with m2eclipse?

Okay, I went through this problem in Eclipse Indigo for J2EE Developers release that m2eclipse wont show me the Run On Server selection in the Run menu when I import a maven project with war packaging (ie. the maven-archetype-webapp).

Here is how to fix it:
1. Uninstall the current m2eclipse plugin you have in eclipse:
  (a) Go to "Help > Install New Software"
  (b) Click on the "What is already installed?" link
  (c) Select "m2e - Maven Eclipse Integration" and click on the "Uninstall" button
  (d) Restart eclipse when prompted

2. Next, you have to install the m2eclipse:
  (a) Go to "Help > Eclipse Market Place"
  (b) Search for the word m2eclipse
  (c) Select "Maven integration for eclipse"
  (d) Click the "Install" button

3. Install the "Maven Integration for Eclipse WTP"

  (a) Go to "Help > Eclipse Market Place"
  (b) Search for the word m2eclipse
  (c) Select "Maven integration for eclipse WTP
  (d) Click the "Install" button

Restart when prompted.

Now, importing a maven project with war packaging will work out of the box. Enjoy.

Sunday, October 23, 2011

Configuring Eclipse to run with a specific JDK or JRE


1. Open the file eclipse.ini for editing

2. Add the two lines to specify the virtual machine to use (the bolded lines):

-startup
plugins/org.eclipse.equinox.launcher_1.2.0.v20110502.jar

-vm 
C:\Program Files\Java\jdk1.6.0_29\bin\javaw.exe

--launcher.library
plugins/org.eclipse.equinox.launcher.win32.win32.x86_64_1.1.100.v20110502

3. Now start eclipse.

Wednesday, October 19, 2011

Wonderful Iphone Icon Generator Online

Give your divs a simplistic nice shadow with css

I loved the middle box shadow in the shopify.com website, and therefore decided to dig it up. The CSS used for it is:

-moz-box-shadow: 0px 0px 15px rgba(0,0,0,0.1);
-webkit-box-shadow: 0px 0px 15px rgba(0,0,0,0.1);
box-shadow: 0px 0px 15px rgba(0,0,0,0.5);