It's not a secret anymore!*
The moose likes Linux / UNIX and the fly likes how to get the return value from java method by shell script Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "how to get the return value from java method by shell script" Watch "how to get the return value from java method by shell script" New topic
Author

how to get the return value from java method by shell script

rama ilango
Greenhorn

Joined: Dec 11, 2008
Posts: 24
Hello,

java program:
class test{
public static void main(String args[]){
}
public String sample(){
return "success";
}
}

using shell script, how to call the test.sample() method and get the return value.
please reply.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15947
    
  19

http://www.coderanch.com/t/419711/Linux-UNIX/How-to-capture-shell-scripts

Customer surveys are for companies who didn't pay proper attention to begin with.
Charles Lyons
Author
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
rama ilango wrote:using shell script, how to call the test.sample() method and get the return value.
please reply.

You cannot call the method directly. The shell cannot interpret Java by itself, you must first start a JVM executing your code (i.e. with the "java" command). This will always call the main() method (which may do anything you like). So in order to invoke sample(), you need to invoke it from main(), then have your shell script run "java test". You cannot call Java methods directly from the shell. If you want to capture the output, either of these would do it in Bash (storing result in "out"):


Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / Amazon Amazon UK )
Chandrasekaran SanthanaKrishnan
Greenhorn

Joined: Mar 29, 2011
Posts: 7
I am trying the same in a shell script on solaris OS.



both the above options are not working.
anirudh jagithyala
Ranch Hand

Joined: Dec 07, 2010
Posts: 41
Chandrasekaran ,

Did you do
So in order to invoke sample(), you need to invoke it from main(), then have your shell script run "java test".

and then try
out=`java test`
out=$(java test)
Chandrasekaran SanthanaKrishnan
Greenhorn

Joined: Mar 29, 2011
Posts: 7
anirudh jagithyala,

I did that exactly as you have said.

Its not working.
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

Chandrasekaran SanthanaKrishnan wrote:anirudh jagithyala,

I did that exactly as you have said.

Its not working.


'Not working' is a poor error description. What didn't work? Did you get an error message? Which? How did you experience that it didn't work? What result did you expect, what did you get?

Note, that you just use one form, and it's the second:

Backticks are deprecated (Ommm, Ommm) because you can't nest them easily. Since classes should wear uppercase initials, you call `java Test`, not `java test`.

Note too, that the initial code was wrong in multiple ways:

* should be public
* named with capital T
* main method is empty - so nothing happens
* sample-method returns a String, but does no output.

http://home.arcor.de/hirnstrom/bewerbung
Chandrasekaran SanthanaKrishnan
Greenhorn

Joined: Mar 29, 2011
Posts: 7
Hi stefan wagner,

'Not working' is a poor error description. What didn't work? Did you get an error message? Which? How did you experience that it didn't work? What result did you expect, what did you get?


Sorry for that vague reply.
I meant the solution did not workout.
I did not get any error message. I did not get the returned value in the variable in my script.
I expect a string to be returned from the class and that is not happening.

Backticks are deprecated (Ommm, Ommm) because you can't nest them easily. Since classes should wear uppercase initials, you call `java Test`, not `java test`.


The example i have quoted here has class name starting will small letters, but when I am trying here locally my class name is proper.

should be public
* named with capital T
* main method is empty - so nothing happens
* sample-method returns a String, but does no output.


yes for all the above lines.

Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923


So save this, compile it, run it:

Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404
    
  81

The other thing you should try is to just run the Java program from the command line and confirm it is working - don't try to capture the output at this stage, just get confirmation that your application is working as expected first.

One of the things that might be an issue is that you might be running your application as root, and on many systems you will get classpath issues if you run as root. This can be quickly discounted if you run the program from the command line without trying to capture the output as a first step.


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Chandrasekaran SanthanaKrishnan
Greenhorn

Joined: Mar 29, 2011
Posts: 7
Andrew Monkhouse,

The first step I did after creating the java program was to run it from command prompt and test it.

It was working.

And I don't have to root access to this machine.
 
Don't get me started about those stupid light bulbs.
 
subject: how to get the return value from java method by shell script
 
Similar Threads
how to get output string parameter from Java to shell script
problem in standalone java returning value to shell script
issue in passiing variable to shell script
return value from main
How to return a value from java to script