Pass string from shell script to java


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pass string from shell script to java
# 1  
Old 05-17-2009
Error 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.
# 2  
Old 05-17-2009
Quote:
Originally Posted by badbunny9316
...
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.
...
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:

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

$
$ 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
$
$


Quote:
Originally Posted by badbunny9316
...
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..
...
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:

Code:
$
$ 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."

Last edited by durden_tyler; 05-17-2009 at 10:44 AM..
# 3  
Old 05-17-2009
File java and shell script is at different location

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 helpSmilie
# 4  
Old 05-18-2009
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
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. UNIX for Dummies Questions & Answers

How to write Config shell script to pass variables in master shell script?

Dear Unix gurus, We have a config shell script file which has 30 variables which needs to be passed to master unix shell script that invokes oracle database sessions. So those 30 variables need to go through the database sessions (They are inputs) via a shell script. one of the variable name... (1 Reply)
Discussion started by: dba1981
1 Replies

3. Shell Programming and Scripting

Pass a variable string in To_Date Oracle function in shell script

Hello, I am trying to execute an SQL query from shell script. A part of script is something like this: fromDate=`echo $(date +"%F%T") | sed "s/-//g" | sed "s/://g"` $ORACLE_HOME/sqlplus -s /nolog <<EOD1 connect $COSDBUID/$COSDBPWD@$COSDBSID spool... (4 Replies)
Discussion started by: sanketpatel.86
4 Replies

4. Shell Programming and Scripting

Unable to pass shell script variable to awk command in same shell script

I have a shell script (.sh) and I want to pass a parameter value to the awk command but I am getting exception, please assist. diff=$1$2.diff id=$2 new=new_$diff echo "My id is $1" echo "I want to sync for user account $id" ##awk command I am using is as below cat $diff | awk... (2 Replies)
Discussion started by: Ashunayak
2 Replies

5. 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

6. Shell Programming and Scripting

Pass string arg from shell to perl

My shell script generates a bunch of lines of text and passes this text as an argument to a perl script. I'm able to do this, but for some reason newlines don't get recognized in the perl script and so the script just prints actual '\n' instead of carriage returning, otherwise everything gets... (3 Replies)
Discussion started by: stevensw
3 Replies

7. Shell Programming and Scripting

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 (1 Reply)
Discussion started by: Neha Goyal
1 Replies

8. Shell Programming and Scripting

Not able to pass string with spaces in shell

I have to pass a sentence in a file, the specs are as: cat run | sed 's/SRT/'$8'/g' | sed 's/plength/68/g' | sed 's/stcol/'$5'/g' | sed 's/encol/'$6'/g' | sed 's/brdtype/'$1'/g' | sed 's/brdtxt/'$3'/g' | sed 's/demotxt/Total '$2'/g' | sed 's/bantxt/ban_'$7'/g' | sed 's/validcodes/'$4'/g' >... (15 Replies)
Discussion started by: patilrakesh1984
15 Replies

9. Shell Programming and Scripting

Pass a string with spaces to a shell script

I'm a beginner and wasn't able to google my problem... I would like to pass a string with spaces to a shell script. my test_shell: #!/bin/sh -vx ####################################################### # generate_documentation (c) Ch.Affolter Nov. 2009 Version 1.0 #... (3 Replies)
Discussion started by: vivelafete
3 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