Thursday, May 6, 2010

How to run command on Karaf command prompt from other java program

I wanted to uninstall 'clock_3.0' bundle which is running in karaf. So I can do it by typing 'features:uninstall clock_3.0' command on karaf command prompt.
But Now I wanted to run this command from some other Java program.

We can do this by running following command:

Process ls_proc1;
ls_proc1 = Runtime.getRuntime().exec("cmd.exe /C java -jar C:\\apache-felix-karaf-1.2.0\\lib\\karaf-client.jar features:uninstall clock_3.0");

where apache-felix-karaf-1.2.0 is installed at C: on my computer.

We can even display the output of this command:

try {
BufferedReader br = new BufferedReader(new InputStreamReader(ls_in));
while ((ls_str1 = br.readLine()) != null) {
System.out.println(ls_str1);
}
} catch (IOException e1) {
e1.printStackTrace();
}

Wednesday, April 21, 2010

How to display HTML tags, code in blogs

When you try displaying HTML tags in blogs, it as disappear when you try publishing it.

So if you want to display something like

<html>
Hello World
</html>

When you will try publishing the post, it will only show,

Hello World

All the HTML tags won't be displayed.

If you want to do that, there is an easy way around.

Just Replace

And you can display all your HTML tags in your blog.

Maven build lifecycle

I came across this really good article which talks about Maven build life cycle.


A Build Lifecycle is Made Up of Phases

Each of these build lifecycles is defined by a different list of build phases, wherein a build phase represents a stage in the lifecycle.

For example, the default lifecycle has the following build phases (for a complete list of the build phases, refer to the Lifecycle Reference):

  • validate - validate the project is correct and all necessary information is available
  • compile - compile the source code of the project
  • test - test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
  • package - take the compiled code and package it in its distributable format, such as a JAR.
  • integration-test - process and deploy the package if necessary into an environment where integration tests can be run
  • verify - run any checks to verify the package is valid and meets quality criteria
  • install - install the package into the local repository, for use as a dependency in other projects locally
  • deploy - done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.

These build phases (plus the other build phases not shown here) are executed sequentially to complete the default lifecycle. Given the build phases above, this means that when the default lifecycle is used, Maven will first validate the project, then will try to compile the sources, run those against the tests, package the binaries (e.g. jar), run integration tests against that package, verify the package, install the verifed package to the local repository, then deploy the installed package in a specified environment.

To do all those, you only need to call the last build phase to be executed, in this case, deploy:

mvn deploy

That is because if you call a build phase, it will execute not only that build phase, but also every build phase prior to the called build phase.

source:http://stackoverflow.com/questions/1544619/if-i-run-mvn-deploy-does-it-build-new-artifacts-or-it-just-deploy-the-already-exi

How can we turn on the display of folders/directories in web browser which present under tomcat ROOT directory.

I downloaded apache tomcat server and added a folder repository in 'apache-tomcat-6.0.26\webapps\ROOT' directory which contains many subfolders/subdirectories and html pages, jars inside those folders/directories.
I can go index page in 'apache-tomcat-6.0.26\webapps\ROOT' folder by typing the URL as 'http://localhost:8080'

But now if I want to download any jar present in some subfolder of repository directory, I can do it by typing the relative path of the jar file and adding the URL.

Suppose x.jar is present in the directory 'repository\xx\yy\x.jar', then I can download it by accessing following URL : 'http://localhost:8080/repository/xx/yy/x.jar'

But I type something like 'http://localhost:8080/repository/xx/yy' it will give me error page.

If you want to turn on the display of directories/folders in the web browser in case the default file like index.html is not present, then it should display the list of files and directories present under that directory mentioned in the URL.

For enabling this, you have to set 'listings' parameter in the default servlet to true. You have to edit the web.xml flie present at location: 'apache-tomcat-6.0.26\conf' directory.

Set it as shown below:

<init-param>
<param-name>listings</param-name>
<param-value>true</param-value>
</init-param>


Once you do this, You can see web browser will display the directory structure, the list of files and sub-directories present the directory mentioned in URL

Friday, March 26, 2010

Linear Search Java Example

Below is linear Searching Algorithm implemented in Java.



import java.util.Scanner;

//##################################################
// Linear Search
//##################################################
//Program has list of integers saved in an array and
//program will take inputs from user and search that
//if that input is present in Array of Integers.

public class linearSearch {

public static void main(String args[]) {

int[] listOfIntegers = { 1, 34, 53, 23, 77, 98, 101, 20, 45, 66, 84 };
int inputTobeSearch;

Scanner sn = new Scanner(System.in);
inputTobeSearch = sn.nextInt();
int token = -1;

for (int i = 0; i < listOfIntegers.length; i++) {

if (listOfIntegers[i] == inputTobeSearch) {
token = i;
}
}

if (token != (-1)) {
System.out
.println("Input integer " + inputTobeSearch
+ " is present at " + (token + 1)
+ "th place in the Array");
} else
System.out.println("Input integer " + inputTobeSearch
+ " is not present in the Array");

}

}

Thursday, March 25, 2010

Execute a command from Java Program on Windows Mac or *nix System

I was working on some project and I wanted to write a code which will execute command from Java Program and it should run irrespective of Operating system.

So wrote a code and thought it will be nice to share, or may be it will be good for me, if I want to look at it after a while :)

The code below will run command from Java file even if it is Windows or Mac OS or Unix





//Package name is of the package
package finalgui;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

/**
*
* @author Vinit Patwa
*/
public class RunCommand {

private static String ls_str;

public static void main(String args[]) {

try {
Process ls_proc;

//we want to run command "mvn clean install"
//It is expected that "mvn" is in your PATH
String mvnClean = "mvn clean install";

//We are storing the OS on system in String OS
String OS = System.getProperty("os.name");
System.out.println("OS is: " + OS);


if (OS.contains("Windows")) {
ls_proc = Runtime.getRuntime().exec("cmd.exe /C mvn clean install");
} else {
ls_proc = Runtime.getRuntime().exec(mvnClean);
}


//We can also display the output of the command Here
DataInputStream ls_in = new DataInputStream(
ls_proc.getInputStream());
try {
BufferedReader br = new BufferedReader(new InputStreamReader(ls_in));
while ((ls_str = br.readLine()) != null) {
System.out.println(ls_str);
}
} catch (IOException e1) {
System.exit(0);
}
} catch (Exception e) {
System.out.println(e.toString());
e.printStackTrace();

}
}
}

Friday, March 5, 2010

1. Tutorials on Socket:

C/C++

JAVA

2. Tutorials on Threads

Pthreads

JAVA

Source: http://vinitpatwa.wordpress.com/2009/09/01/socket-programming-and-multithreading-resources/