Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to return a value from java to script

 
ramjcp kris
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All

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

following is my test.sh


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
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't quite understand your question In your code Java code, it doesn't seem to print anything. If you invoke test.sh, 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.
 
Sri Anand
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic