• 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 get the return value from java method by shell script

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
https://coderanch.com/t/419711/Linux-UNIX/How-to-capture-shell-scripts
 
Author
Posts: 836
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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"):
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying the same in a shell script on solaris OS.



both the above options are not working.
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
anirudh jagithyala,

I did that exactly as you have said.

Its not working.
 
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

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: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

So save this, compile it, run it:

 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Lasagna is spaghetti flvored cake. Just like this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic