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/

JBPM new web based BPMN editor


The jBPM Modeller is an advanced web based BPMN editor aimed at non technical business users. No software needs to be installed on the user's machine, a plain web browser is enough to get the job done. Business analysts can easiliy produce complex process models with the tool, which then later can be imported by developers in their Eclipse environment without any conversion needed.

The jBPM Modeller is based on the fantasic web based modeling tool by Signavio. In less than a minute, every stakeholder involved can view, edit and collaborate on process models through their own brower. source: www.jboss.org

Wednesday, March 3, 2010

java.io.IOException: CreateProcess: ... error=193

When I tried running an executable from from Java Jar file, while executing following code -

Runtime.getRuntime().exec("C:\\maven\\apache-maven-2.2.1\\bin\\mvn pax:provision");

I got following Exception -

java.io.IOException: Cannot run program "C:\maven\apache-maven-2.2.1\bin\mvn": CreateProcess error=193, %1 is not a valid Win32 application

To resolve this problem, the command should be invoked as:

Runtime.getRuntime().exec("cmd.exe /C C:\\maven\\apache-maven-2.2.1\\bin\\mvn pax:provision");

And it worked.

(Running executable from Java Jar file)

Tuesday, March 2, 2010

Install Maven Pax Plugin

MAVEN PAX PLUGIN:

The Pax Plugin is a "Swiss Army(tm) knife" for OSGi that provides goals to create, build, manage and deploy many types of OSGi bundles. While the easiest way to install and use this plugin is by using the Pax-Construct scripts, its goals can also be used directly on the Maven command line, or inside other Maven POMs.

Steps for installing Maven Pax Construct Plugin:

1. Get the pax zip and unzip it to temp (or any other directory you prefer):

http://repo1.maven.org/maven2/org/ops4j/pax/construct/scripts/1.4/scripts-1.4.zip

2. Open up the command prompt and enter the command:

set PATH=C:\temp\pax-construct-1.4\bin;%PATH%