Call java program from shell and pass values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Call java program from shell and pass values
# 1  
Old 05-17-2010
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
# 2  
Old 05-17-2010
Quote:
Originally Posted by Neha Goyal
...
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.
...
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 -

Code:
java <my_java_class_file_without_extension> <arg1> <arg2> ... ...

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

Code:
$
$
$ # 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[i]);
    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

Last edited by durden_tyler; 05-17-2010 at 01:30 PM..
This User Gave Thanks to durden_tyler For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Pass RegEx to java program in bash script

I can't seem to get this right. I've tried it every way imaginable using every trick I see on stackexchange and such. No luck. So nothing major here, something like: #!/bin/bash SEARCH="ARG1 ARG2 '((^EXACT$)|(.*InTheMiddle*)|(^AtBeginning*))'" java -cp /my/class/path MyClassName $SEARCH... (3 Replies)
Discussion started by: stonkers
3 Replies

2. Shell Programming and Scripting

Need help to write to log file whether the shell script call pass fail

I have the below script triggered daily at 330am in the morning, since last 7 days job not writing anything to database. below impala shell calling shell file which has sql , it is extracting data and loads to a flat file txt file. which is going wrong for last 1 week. need help, echo... (2 Replies)
Discussion started by: cplusplus1
2 Replies

3. UNIX for Dummies Questions & Answers

how to pass input from c program to shell script?

Hello.. I am developing a Graphical User Interface using GTK. As part of our project I need to take inputs from GTK entries and pass those inputs to shell script and use them in shell script. The problem which struck me is only limited number of inputs are getting passed to shell script. For now... (14 Replies)
Discussion started by: kalyanilinux
14 Replies

4. Shell Programming and Scripting

How to pass the environment name while calling java program from unix script?

Hi, I'm trying to test one unix shell script in dev environment. But I'm not sure how to pass the environment in my java program calling code. I'm trying to use -DconsumerEnv="DEV" but unfortunately I get 'null' while trying to print the value from java class. System.out.println("Environment: "+... (4 Replies)
Discussion started by: Pramit
4 Replies

5. Shell Programming and Scripting

Pass string from shell script to java

Hi, I,m writing a program in shell script and currently this script is calling a java program. I have a problem to pass string variable from my shell script to the java program. I don't know on how to pass it and how the java program can call what I have pass from the shell script. This is... (3 Replies)
Discussion started by: badbunny9316
3 Replies

6. Shell Programming and Scripting

how to call java prog from shell script

I have a java program to validate a XML file. I want to call this java program in a shell script which will be registered as concurrent program in oracle apps. Can anyone please let me know the step by step appraoch required to call java program in shell script like ....intial steps... (1 Reply)
Discussion started by: kcp_pavan
1 Replies

7. Shell Programming and Scripting

Call C Program From Shell Script

Hi, Could anybody please let me know how to call a C_Program from shell script. I know through command "system" we can call shell script from C program. Awaiting response. Thanks and regards, Chanakya M (4 Replies)
Discussion started by: Chanakya.m
4 Replies

8. Shell Programming and Scripting

Shell script to call multiple java commands

Hi, I want to call multiple java commands through a .sh file. I have drafted one with my least knowledge. But its not working. Pls help. I am trying to run this from Sun Solaris (OS 5.10) server with 10g oracle database. echo \* starting script AUTORUN echo \* get the Monitor path... (4 Replies)
Discussion started by: vivekdn
4 Replies

9. Shell Programming and Scripting

How to call Java by using shell script

Hi All, I am new to shell script, just wanted you guy to help. How do i call a java program by using shell script, pls give some samle code for it, thank you ver much. (2 Replies)
Discussion started by: aaabbb123123
2 Replies

10. Shell Programming and Scripting

getting date from shell and pass it to java

Hi, I have a variable called asOfDate in shell script. I need to pass it as a command line argument to a java command which will be called from the same shell script. The format of that variable is "MM/DD/YYYY" while doing echo, it is printing correctly in the java command. ie.... (0 Replies)
Discussion started by: vanathi
0 Replies
Login or Register to Ask a Question