• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to return a value from java to script

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
reply
    Bookmark Topic Watch Topic
  • New Topic