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 my shell script :

for file in $list_file; do

mv $INPUT_PATH/$file $INPUT_PATH/CIM301${day}_${count}.txt
count=`echo $count + 1 | bc`

done

I want to pass the variable $list_file to my java program..

Anybody can help??

Thanks in advance.

Well, it's as simple as putting the value of your shell variable in the call to the java program. The java program doesn't really *call* the argument(s); it accesses and maybe processes them.

Here's a plain-vanilla java program that displays the input parameters:

$ 
$ cat testArg.java
class testArg {   
  public static void main (String args[]) {
    System.out.println("\nInside the java program.");
    System.out.println("You passed : " + args.length + " parameters.");
    for (int i=0; i<=args.length-1; i++) {                             
      System.out.println("Parameter # " + (i+1) + "\tValue = " + args);
    }
  }
}

$
$ java testArg ant bat cat dog eel

Inside the java program.
You passed : 5 parameters.
Parameter # 1   Value = ant
Parameter # 2   Value = bat
Parameter # 3   Value = cat
Parameter # 4   Value = dog
Parameter # 5   Value = eel
$
$

I believe "list_file" contains a list of files separated by whitespace.
Here's how you can pass it to the java program shown above:

$
$ cat test_scr.sh
file_list="file1 file2 file3 file4"
echo "In the shell script."
echo "Value of file_list = $file_list"
echo "Now passing file_list to the java program..."
java testArg $file_list

$
$ . test_scr.sh
In the shell script.
Value of file_list = file1 file2 file3 file4
Now passing file_list to the java program...

Inside the java program.
You passed : 4 parameters.
Parameter # 1   Value = file1
Parameter # 2   Value = file2
Parameter # 3   Value = file3
Parameter # 4   Value = file4
$
$

tyler_durden

____________________________________
"The things you own end up owning you."

Hi durden_tyler,

Thanks for your code. I have try it but I encounter some error. For your info my java code and shell script is at different location.So, I try to specify the java code location into your shell script :

java .portal.DEV.PDEVFN12.Batch.common.Java.src.com.p1.bccs.batch.common.fileloader.testArg $file_list

I got this error when I run your script :

test_scr.ksh[2]: ^M: not found.
test_scr.ksh[5]: ^M: not found.
test_scr.ksh[6]: ^M: not found.
In the shell script.
Value of file_list = file1 file2 file3 file4
Now passing file_list to the java program...
Exception in thread "main" java.lang.NoClassDefFoundError: /portal/DEV/PDEVFN12/Batch/common/Java/src/com/p1/bccs/batch/common/fileloader/testArg

Thanks for your help:b:

The "^M" error is thrown due to differences in end-of-line characters across OSes. It might've been introduced when you ftp'ed your file with incorrect format from one OS to another, or when you copied/pasted the code to a text editor that does not "recognize/set" the end-of-line character of the underlying OS.
The NoClassDefFoundError is pretty self-explanatory - the class was not found by your JVM. Reset your classpath accordingly.

tyler_durden