This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
rama ilango
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 18277
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://www.coderanch.com/t/419711/Linux-UNIX/How-to-capture-shell-scripts
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"):
 
Chandrasekaran SanthanaKrishnan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying the same in a shell script on solaris OS.



both the above options are not working.
 
anirudh jagithyala
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
anirudh jagithyala,

I did that exactly as you have said.

Its not working.
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Chandrasekaran SanthanaKrishnan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

So save this, compile it, run it:

 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 11914
209
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Chandrasekaran SanthanaKrishnan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic