Call java program from shell and pass values

Hi All,
Can anybody please help me with how can i call my java program from shell and also pass parameter along with it so that the program can interpret the value/int and update the database.

Thanks in advance
Neha

Calling a Java program is as simple as passing the class file to the Java application launcher tool (which is called "java").

I shall assume that you have either created a source file and compiled it into a ".class" file, or obtained a compiled file from somewhere else. In either case, you have a ".class" file with you.

You run the program by doing so -

java <my_java_class_file_without_extension> <arg1> <arg2> ... ...

Here's a Java program that simply displays the parameters passed to it:

$
$
$ # display the content of the Java source file
$ cat -n ShowArgs.java
     1  /*
     2  A java program that displays the input parameters passed to it.
     3  */
     4  public class ShowArgs {
     5    public static void main (String[] args) {
     6      System.out.println("No. of input parameters = "+args.length);
     7      if (args.length > 0) {
     8        System.out.println("The input parameters are listed below:");
     9        for (int i=0; i<=args.length-1; i++) {
    10          System.out.println("Parameter # "+i+" => "+args);
    11        }
    12      }
    13    }
    14  }
$
$
$ # see if I have the corresponding class file
$ ls -1 ShowArgs*
ShowArgs.class
ShowArgs.java
$
$
$ # Yes, I do. So just launch it now by feeding the *class* file to "java"
$
$ java ShowArgs
No. of input parameters = 0
$
$ # once more
$
$ java ShowArgs the quick 123.45 brown fox -9e10 jumps "over the lazy" dog
No. of input parameters = 9
The input parameters are listed below:
Parameter # 0 => the
Parameter # 1 => quick
Parameter # 2 => 123.45
Parameter # 3 => brown
Parameter # 4 => fox
Parameter # 5 => -9e10
Parameter # 6 => jumps
Parameter # 7 => over the lazy
Parameter # 8 => dog
$
$

About "updating the database" - that is a very broad topic. Java programs typically make use of JDBC to interact with a database. There are a lot of things you need to take care of while doing JDBC programming, and it's something a book would explain better than me.

I suggest you go through Sun's JDBC Tutorial to get an idea of JDBC - The Java� Tutorials

Or better still, you may want to buy a good book on JDBC from your favorite bookstore.

HTH,
tyler_durden

1 Like