File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Linux / UNIX and the fly likes How to return a value from java to script Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "How to return a value from java to script" Watch "How to return a value from java to script" New topic

How to return a value from java to script

ramjcp kris

Joined: Sep 05, 2005
Posts: 27
Hi All

my Requirement is I need to return a value from java program to a shell main class is being called from this shell script;

following is my

And my Hello class code is below

can any one tell me how to print the value returned from my java program to shell script
waiting for reply with anticipation
Freddy Wong
Ranch Hand

Joined: Sep 11, 2006
Posts: 959

I don't quite understand your question In your code Java code, it doesn't seem to print anything. If you invoke, of course you won't see anything.

If let's say you have a Hello World program, then you run it from your script, I'm pretty sure you can see the Hello World message in your shell.

SCJP 5.0, SCWCD 1.4, SCBCD 1.3, SCDJWS 1.4
My Blog
Sri Anand
Ranch Hand

Joined: Mar 06, 2005
Posts: 392
you can save the property in a file and retrieve either from java or shell. if this is not too much for what you are trying to achieve
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
While it's possible to use System.exit to pass a value to the shell, that's meant for status codes. If the program runs to completion without error, then it should return a 0 to the shell.

The standard approach would be to print the value to standard out. What kind of shell are you using?
Harjit Singh
Ranch Hand

Joined: Feb 01, 2007
Posts: 77
The way you can to it

1) when you are calling this class from the shell script. Redirect the output to a file

java my.class > out.txt

in the shell script you can read this value from the file

In the java class write it to the output stream using system.out

Hope that helps

- H
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

I agree with Ulf.
If you ignore the hint, you catch the exit code with $? (at least bash works that way):

But returncode not 0 indicates an error, and the wrong approach to return a correct result.
If the program fails, you can't recognize this anymore, and if you use it in a script, everybody is confused.

The right way is indeed to use System.out.println (result); and read that value, but of course without the detour of writing to a file:

nice and easy - isn't it?
Edmundo Carmona

Joined: Jul 19, 2007
Posts: 3
...or you can throw whatever you want to STDOUT (System.out, you know, right?) and from the bash side use "while read variable" to get the output and work on it:

Example (not java related):
$ dmesg | while read line; do echo $line; done

replace dmesg for the java command to run your application and enjoy!
I agree. Here's the link:
subject: How to return a value from java to script
It's not a secret anymore!