Friday, December 26, 2008

Details about installing java on different Operating Systems

Sun Microsystems has provided a very nice page providing all the details about steps and links for How to Install Java and also provided  links of different JDK’s for various operating systems.

 Here is the Link : http://www.java.com/en/download/manual.jsp

How do i verify whether an URL is active or not through Java

There are different ways in java through which we can verify whether any specific URL is active or not.
Here is the Java Program which displays success message if the URL is active and Failed message if URL is not active/dead.

URLVerification 
----------------
import java.net.*;
import java.io.*;

public class URLVerification {
    public static void main(String[] args) throws Exception {
        URL url_to_verify = new URL("http://www.google.com");
        try
        {
          URLConnection connObj = url_to_verify.openConnection();
          connObj .connect();
          InputStream stream_to_recieve_data = connObj .getInputStream();
          stream_to_recieve_data.close();  
          System.out.println("Successful in establishing connection.");
        }
        catch (Exception e)
        {
          System.out.println("Failed in establishing connection.");
        }
    }
}
-------------------
I also want to share the few scenarios where we can use this such as:

1. You have to write a code which will call a specific URL to get data after each interval
2. Before Sending request , you can write a filter to verify whether the url is active or not.
3. Directly you want to call a servlet/jsp through a java program to get the status 

I hope my above comments will help you to come up with new ideas as well.

Please provide comments if any...

Thursday, December 25, 2008

How do I pass numeric value as command line argument to Java

Here is the example to pass numeric value as a command line argument is Java Class.

CommandLineNumExample.java
---------------------------------
public class  CommandLineNumExample
{
  public static void main(String[] args) 
  {
    int i = Integer.parseInt(args[0]);
    double d = Double.parseDouble(args[0]);
    // multiplying the value of i i.e command line argument with 2
    System.out.println("The Argument Number Value is  "+ (i*2)); 

    // multiplying the value of i i.e command line argument with 3
    // you can see a decimal value also as the argument is converted to double
    System.out.println("The Argument Double Value is  "+ (d*3));
  }
}
---------------------------------
Please read my previous example on How to pass Command Line Arguments in Java to get details about CommandLine arguments and how to pass arguments thru command line

The output will be 
---------------------
The Argument Number Value is  44
The Argument Double Value is  66.0
---------------------

For your help, i am providing the commands in sequence:
Note: 22 in the below command is the argument
----------------------------
C:\Temp>javac CommandLineNumExample.java

C:\Temp>java CommandLineNumExample 22
The Argument Number Value is  44
The Argument Double Value is  66.0
----------------------------

If you any comments or suggestion, please provide.

How do i pass command line arguments in Java

Here is the example of how to pass command line arguments to a java file.
In the below example, one argument will be passed with the java command, and later within the the progrm it is retrieved as a String. 
Few other details, whenever we execute a java command, the main method is called, and in the below example you can see a main method defined and accepts array of String as parameters.
This means, whenever we pass any command line parameter, it is accpetd with this main method, and  "String s1 = args[0];", this line retrieves the value from the arguments passed to main method. We need to remember here one thing particularly, i.e. if we are passing one argument then we can retrieve only one otherwise if any mismatch occurs, then it will throw an exception as ArrayIndexOutOfBound exception.

CommandLineExample.java
-----------------------------

public class  CommandLineExample
{
  public static void main(String[] args) 
  {
    String s1 = args[0];
    System.out.println("Hello..  "+ s1);
  }
}
------------------------------
The above file is meant to recieve only one argument as command line argument.

How to execute and pass the command line argument, please follow the below instruction:
1. Open Command Prompt.
2. Go to the folder location where you have saved the above file as CommandLineExample.java.
3. Compile the Java file CommandLineExample.java
4. Once compilation is successfull, its turn for running the code..
5. Provide command as java CommandLineExample John and press enter
6. You can see a extra text is added other then java command and file name i.e. John, this is the string argument which i am passing to the CommandLineExample's main method.
Note: You can pass more then one number of arguments also. Whenever it is passed to the main method, all the arguments type are considered as String type. If you want to pass some numeric value then , you need to type cast it within the java code, and can use based on your requirement which is again a quite easy process.
6. You will se a message as Hello..  John

For extra help, i am providing the commandline texts here:
---------------------------------------------
--comment : below line will compile the file
C:\Temp>javac CommandLineExample.java
--comment : below line will execute the file, taking the last text i.e. John as command line argument
C:\Temp>java CommandLineExample John
Hello..  John
----------------------------------------------

Please provide you comments if any...

Wednesday, December 24, 2008

How to do I sum and print two numbers in Java Program

Here is the sample program , which will print two number sum in different ways:

public class  SumNumbers
{
  /*
    Create a java file as SumNumbers.java
    Save this code in it.
  */
  public static void main(String[] args) 
  {
    /* defining two variables.
      1. int x
      2. int y
      Variable x and y are prefixed by keyword called int.
      'int' means integer datatype can contain any integer non decimal value.
    */
    int x = 10;
    int y = 20;
    // Different ways we can print the sum value as mentioned below.
    int sum = x+ y;
    System.out.println(" Sum of two numbers : Style 1 : "+sum);
    System.out.println(" Sum of two numbers : Style 2 : "+(x+y));
    System.out.println(" Sum of two numbers "+x+" and "+y+" is : Style 3 : "+(x+y)); 
  }
}
Output will be:
-----------------------
Sum of two numbers : Style 1 : 30
Sum of two numbers : Style 2 : 30
Sum of two numbers 10 and 20 is : Style 3 : 30

Tuesday, December 23, 2008

Storing java command output to text file.

Its quite simple to store Java command output of the console window of MS Windows i.e command prompt to store in a text file. The need for this is when we have huge text as an output on executing a java program, sometimes we face problem in searching specific text.
The command is:
commandprompt>java GenerateLog >> UserFile.txt

here in the above example, java GenerateLog is the command to execute GenerateLog java file, and output will be dislayed on the console. But by providing the ">> UserFile.txt" at the end of it , the output will be directed to the file called UserFile.txt

By using the above command, a text file named UserFile will be stored in most probably c drive or in the current location
You can easily find this file.

Comment will be highly appreciated...

How do I configure User tools in editplus to compile and run java file

I would like to demonstrate how easy it is to configure java environment in editplus.

1.Open your installed editplus editor
2.Go to Menu -> Tools -> Configure User Tools
3. A Preferrences titled window will be opened on your desktop.
4.Find Add Tools button under the section Groups and tools item: section on the right side of the window.
5. Click the button Add Tools -> Program
6. It will create an item as New Program in the List.
7. Below the list you will find few input boxes such as menu, command, argument etc.
8. Fille those with following details
Menu Text: CompileJava
(Note: Here i am creating a user tool which will compile the java program written in edit plus without going to the command prompt and compiling manually)
Command: %JavaInstalledDir%\jdk1.5.0_14\bin\javac.exe
(Note: %JavaInstalledDir% means the location where you have installed Java. for example c:\jdk1..5\bin\javac.exe, so write c:\jdk1..5\bin\javac.exe in command inout box.Dont copy paste %Installed......... content. Please verify wether javac.exe file is available in the given location)
Arguments: $(FileDir)\$(FileName)
(Note: place the above content as it is)
9. Select the capture output window check box.
10.Click Apply and Ok.
11. A CompileJava item will be added under the menu of editplus i.e. Menu -> Tools -> CompileJava
12. Now its very simple to compile the Java file. Open editplus , create a simple java file having no user defined import packages, save it to whichever location you want.Keep the file opened on the editor and go to the itemCompileJava which you have created in step 11. Click it.
It will create a small partition in lower half of editplus and will show the message as
Output Completed.. .. Normal Termination.
13. A class file will be created in the same location where you have placed the file.

How to Configure User Tool for executing the Java Class file in Edit plus.
14. Follow the steps from 2 to 7.
15. The input parameters as
Menu Text: ExecuteJava
Command: %JavaInstalledDir%\jdk1.5.0_14\bin\java.exe
(Note: Follow the note in the above sections, written for Arguments input box value.)
Arguments: -classpath $(FileDir) $(FileNameNoExt)
(Copy paste as it is)
16. Follow steps 9-10.
17. Verify the new tool item as ExecuteJava in your edit plus editor
17. Use steps 12-13 to create a java class file using editplus editor , and the click ExecuteJava item in Menu -> Tools > ExecuteJava , created by you to run/execute it.
You can see the output in the lower half of the editplus editor.
I hope, i am able to help you in setting up Java environment in Edit Plus.

For your convenience, i have added the image of the user configuration tool settings of Edit Plus below:



Note: View the image by increasing the view size of your browser
Your comments will be higly appreciated..