passing parameters from java to shell script

I have created a shell script in unix, from that i am calling a java program. I need to send a parameter (string) back to the shell script. if anybody now how can i do this please tell me. I know we have system.exit(n) which will only return int.
[260 byte] By [shyam29] at [2007-9-30 13:58:02]
# 1
You can do two things:- maintain a table between integers you send back and Strings they match- write your string to a file and read it from your shell script.-Alexis
am74686 at 2007-7-4 23:56:05 > top of Java-index,Administration Tools,Sun Connection...
# 2
My requirement is such that I cannot write the information into a file. Is there any other way from writting it to a file and reading it back in shell script.
shyam29 at 2007-7-4 23:56:05 > top of Java-index,Administration Tools,Sun Connection...
# 3

You cannot pass anything but int as an exit parameter. This is true for all kinds of programs (corrent me, if I'm wrong).

If you want to have a string from a program, the most simple way is to make the java program print this to the standard output, and get it with the shell. For example:

A dummy java class:

public class dummyout {

public static void main(String[] args) {

System.out.println("Output string");

}

}

And the shell script:

#!/bin/bash

RESULT=`java dummyout`

echo "Result is: $RESULT"

Hope this helps.

Lacek at 2007-7-4 23:56:05 > top of Java-index,Administration Tools,Sun Connection...
# 4
I tried with the below code and when I ran it it is giving me the errortest1.ksh[2]: -classpath: not foundResult is: #!/bin/ksh RESULT=java -classpath ${CLASSPATH} dummyout echo "Result is: $RESULT"can you help on this?
shyam29 at 2007-7-4 23:56:05 > top of Java-index,Administration Tools,Sun Connection...
# 5

What you have is quite different from what Lacel recommended. Do it exactly as suggested, probably adding "-classpath ${CLASSPATH}" between "java" and "dummyout"

When you write command such as:

RESULT=java -classpath ${CLASSPATH} dummyout

The shell interprets it as follows: Assign string "java' to shell variable "RESULT" and execute command "-classpath" with arguments "${CLASSPATH}" and "dummyout". Since "-classpath" is not a command you get the error. You should be glad, if there was command named "-classpath" it would have executed and you would have been even more confused.

When you do it as suggested:

RESULT=`java -classpath ${CLASSPATH} dummyout`

Then shell interprets it as: execute the command within back-quotes and assign its output(first word of the output actually, I think) to the shell variable RESULT. This is what you are looking for, Look at man pages of shell for more...

hth.;

hemalpandya at 2007-7-4 23:56:05 > top of Java-index,Administration Tools,Sun Connection...
# 6
The shell variable will contain the entire output, not just the first word.
Lacek at 2007-7-4 23:56:06 > top of Java-index,Administration Tools,Sun Connection...
# 7
> The shell variable will contain the entire output, not> just the first word.Correct. I was thinking about the built-in command read.
hemalpandya at 2007-7-4 23:56:06 > top of Java-index,Administration Tools,Sun Connection...
# 8

The below thing is working:

Shell script:

#!/bin/ksh

javac TestEnvVarSet.java

typeset VAR=`java TestEnvVarSet`

echo $VAR

echo "Done"

Java:

public class TestEnvVarSet {

public static void main(String args[]){

System.out.println("test string");

}

}

I am wondering is there any approach for this. Thanks for all the help

Shyam

shyam29 at 2007-7-4 23:56:06 > top of Java-index,Administration Tools,Sun Connection...
# 9

The below thing is working:

Shell script:

#!/bin/ksh

javac TestEnvVarSet.java

typeset VAR=`java TestEnvVarSet`

echo $VAR

echo "Done"

Java:

public class TestEnvVarSet {

public static void main(String args[]){

System.out.println("test string");

}

}

I am wondering is there any other approach for this. Thanks for all the help

Shyam

shyam29 at 2007-7-4 23:56:06 > top of Java-index,Administration Tools,Sun Connection...
# 10
> I am wondering is there any other approach for this.Without writing to files? I think this pretty much covers it.
shankar.unni at 2007-7-4 23:56:06 > top of Java-index,Administration Tools,Sun Connection...