Skip to content

Quick way to change system variables windows

Typically we need to append to system path variables while setting up tools in our dev/test env, windows power shell can help you to do this quickly –

 

E.g below I am appending xmlbeans to system path.

[Environment]::SetEnvironmentVariable( “Path”, $env:Path+”;C:\Users\rsaini\Downloads\xmlbeans-2.6.0\xmlbeans-2.6.0\bin”, [System.EnvironmentVariableTarget]::Machine )

🙂

Windows – GetShortPathName

Many times we face problem due to space in windows file or folder path. Windows provide GetShortPathName  api to get short path name from the long name. Sometime we need to quickly find out short name while middle to doing something. We can use windows power shell to find that out quickly. Here is quick code to do that,

1) Create filesystem object


$a = New-Object -ComObject Scripting.FileSystemObject

2)  Get the directory or file object


$f = $a.GetFolder("C:\Program Files\MySQL\MySQL Server 5.1\bin")

3) Print the short path


$f.ShortPath

That’s all you need to do, to get the short path from long path.

Step by step spring mvc app using java config

Spring 3.0 provides option of configuration via java code. With out going into theory or advantages or disadvantages I will get into steps directly.

Step 1 : Create simple web  application using maven


mvn archetype:generate -DgroupId=org.opencab
    -DartifactId=cabms -DarchetypeArtifactId=maven-archetype-webapp
    -DinteractiveMode=false

Step 2: Convert solution to eclipse project

mvn eclipse:clean eclipse:eclipse -DdownloadSources=true
    -DdownloadJavadocs=true -Dwtpversion=2.0

Step 3: Create directory structure under src\main\java folder. rest is created by maven.

── pom.xml
── src
├── main
│   ├── java
│   │   └── org
│   │       └── opencab
│   │           ├── config
│   │           │   ├── Initializer.java
│   │           │   └── WebAppConfig.java
│   │           └── controller
│   │               └── HelloController.java
│   ├── resources
│   │   └── database.properties
│   └── webapp
│       ├── index.jsp
│       └── WEB-INF
│           └── web.xml
└── test

Step 4: Modify pom.xml


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.opencab</groupId>
<artifactId>cabms</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>cabms Maven Webapp</name>
<url>http://maven.apache.org</url>

<properties>
<spring.version>3.2.3.RELEASE</spring.version>
<jdk.version>1.6</jdk.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>

<!-- Servlet -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>cabms</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

Step 5 : Implement WebApplicationInitializer in your class and override onStartup method.

package org.opencab.config;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

public class Initializer implements WebApplicationInitializer {

@Override
public void onStartup(ServletContext servletContext)
throws ServletException {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(WebAppConfig.class);
servletContext.addListener(new ContextLoaderListener(ctx));

ctx.setServletContext(servletContext);

Dynamic servlet = servletContext.addServlet("dispatcher",
new DispatcherServlet(ctx));
servlet.addMapping("/");

servlet.setLoadOnStartup(1);
}

}

Step 6 : Write webconfig

package org.opencab.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.JstlView;
import org.springframework.web.servlet.view.UrlBasedViewResolver;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "org.opencab")
@PropertySource({ "classpath:database.properties" })
public class WebAppConfig {
@Bean
public UrlBasedViewResolver setupViewResolver() {
UrlBasedViewResolver resolver = new UrlBasedViewResolver();
resolver.setPrefix("/WEB-INF/pages/");
resolver.setSuffix(".jsp");
resolver.setViewClass(JstlView.class);
return resolver;
}
}

Step 7 : Write simple hello controller

package org.opencab.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloController {

@RequestMapping(value = "/")
public String home() {
System.out.println("HomeController: Passing through...");
return "home";
}
}

That’s all you need to bring up simple application using spring javaConfig.
Note that web.xml file will to empty, since all the configuration are moved into java classes.
Full source code is available at https://github.com/rajendersaini/javaCfg

How to change Perl @INC variable permanently.

Recently I worked on legacy Perl application. The target OS was Solaris 10. I needed a  specific version of Perl to make this application work.

I had very restricted permission on this system. I could not install of make any kind of changes on this system. So I copied the Perl libraries local to machine from some other server. But It did not work as Perl was build on other server. So when I was trying to run my application Perl was looking for some path when it was build.

I did some search on internet and first thing I found was this link

http://www.perlmonks.org/?node_id=486084

But it did not help me at all. I went to next link

http://perlmaven.com/how-to-change-inc-to-find-perl-modules-in-non-standard-locations

what i got from this –

@INC is defined when Perl is compiled and it is embedded in the binary code. You cannot change that, unless you recompile Perl. Not something we would do every day.

Finally stack overflow came to rescue – the following link describe how @INC is constructed.

http://stackoverflow.com/questions/2526804/how-is-perls-inc-constructed-aka-what-are-all-the-ways-of-affecting-where-pe

specifically I was able to change @INC which was encoded into binary just using simple

no lib <'path'>

We also need to add new path using export PERL5LIB env variable.

Hope this helps you sometime when you don’t have option to compile and other restriction on system where you are working.

Error while installing rails

I was getting my hands dirty with ruby on rails as part of Software as a Service course by Berkeley university. After installing RubyGems next I needed to install rails framework, but I got following error

I am working on ubuntu 12.10 64 bit

sudo gem install rails
Building native extensions.  This could take a while…
ERROR:  Error installing rails:
ERROR: Failed to build gem native extension.

/usr/bin/ruby1.9.1 extconf.rb
/usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require’: cannot load such file — mkmf (LoadError)
from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require’
from extconf.rb:1:in `<main>’

Gem files will remain installed in /var/lib/gems/1.9.1/gems/json-1.7.5 for inspection.
Results logged to /var/lib/gems/1.9.1/gems/json-1.7.5/ext/json/ext/generator/gem_make.out

It was referring some package from the ruby1.9.1-dev So I went ahead and installed ruby1.9.1-dev with following command

sudo apt-get install ruby1.9.1-dev

that’s it and sudo gem install rails worked like charm

Minimal Java web services sample.

Minimal Java web services sample.

I needed to write small java web service for POC, I normally start step by step using command line keying in source code in vi editor, so that I can even remember the packages and classes in use. Which normally become transparent when we use IDE. I am not undermining IDE in any way they are great and make development real breeze. But while learning new concepts and tools I prefer to start with minimal sample and then build on them later using IDE etc.

Here is minimal java web service Step by step.

1) Write the web service class implementation to keep the sample minimal I am not going to write interface.

package org.ws.hello;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.WebMethod;
@WebService(name="hello")
@SOAPBinding(style=SOAPBinding.Style.RPC)
public class hello {
@WebMethod()
public String hello(){
return "hello world...";
}
}

2) Publish the Web Service using Jdk endpoint.

package org.ws.hello;
import javax.xml.ws.Endpoint;
public class h {
public static void main(String []args){
hello he = new hello();
Endpoint.publish("http://localhost:8000/", he);
}
}

This will start publishing end point on port 8000. You are done for server-side. Now to consume this service we need to generate proxies to communicate with server.

3) Use wsimport utility to generate proxies

wsimport –d <generated source dir> <web service URL>

e.g wsimport -d gen http://localhost:8000/HelloService/hello?wsdl4) Write the client to consume service.

import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import org.ws.hello.Hello;
import org.ws.hello.HelloService;
public class helloClient {
public static void main(String args[]) {
try {
HelloService hs = new HelloService(new URL(
"http://localhost:8000/HelloService/hello?wsdl"),
new QName("http://hello.ws.org/", "helloService"));
Hello h = hs.getHelloPort();
System.out.println(h.hello());
} catch (MalformedURLException e) {
e.printStackTrace();
}
}

That’s it you are done in few steps. For theory you can refer to lot of documentation on the web.

How to fix your laptop keyborad if keys comes out and not broken

while cleaning up my laptop keyboard one of key came out and I thought of fixing it myself. I came across this video on youtube and with in few minute I fixed my keyboard. Below is link if you face similar issue.