Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Need an executable jar to return a value

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I have an executable jar which must return a value. However, it seems the main class must follow this signature: public static void main(String[] args), and since the jar file executes the main class, how do I get this jar file to return a value? By the way, I am using netbeans 7 to generate the jar. Thanks.
 
Saloon Keeper
Posts: 12303
259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you need the return value for?
 
Patrick Noah
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This jar will be called from another program, and I need the return value from the jar to call another method from this program.
 
Marshal
Posts: 25842
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The way for a program to return a value is to call System.exit(n), where n is the value you want to return. This value can then be examined by the shell script which called the program; I expect that's what you had in mind when you said your jar had to return a value.
 
Patrick Noah
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:The way for a program to return a value is to call System.exit(n), where n is the value you want to return. This value can then be examined by the shell script which called the program; I expect that's what you had in mind when you said your jar had to return a value.



Thanks, I need to return a string though, is there a way to do this?
 
Stephan van Hulst
Saloon Keeper
Posts: 12303
259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you just call some random method from the jar directly? If you don't want to do this, you can execute the jar using a process, and read from the jar's standard output.
 
Paul Clapham
Marshal
Posts: 25842
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Patrick Noah wrote:Thanks, I need to return a string though, is there a way to do this?



Return a string to what? Presumably to something which is "calling" your executable jar. The only things I know which can do that are shell scripts, which can't possibly require a string to be returned. So what's the thing which is calling your jar and wants a string from it?
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My jar file is call from a shell script. I would like this jar to return an int so that I know whether the jar is working or not.

system.exit(); does not work as expect.
in my code, I have system.exit(8);
when it does exit on that line, I do echo $?. It return 0.

Is it because void main(...)?
I change the signature to int main(...), I cannot execute the jar; why?
What do I have to change to make it works?
 
Tedy Kwan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ted Quan wrote:My jar file is call from a shell script. I would like this jar to return an int so that I know whether the jar is working or not.

system.exit(); does not work as expect.
in my code, I have system.exit(8);
when it does exit on that line, I do echo $?. It return 0.

Is it because void main(...)?
I change the signature to int main(...), I cannot execute the jar; why?
What do I have to change to make it works?



I realize that I made mistake; I verify that it system.exit() do return the expected integer value & learn that signature void main(...) should not be changed. Thanks!
 
Rancher
Posts: 1041
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ted Quan wrote:
in my code, I have system.exit(8);
when it does exit on that line, I do echo $?. It return 0.



Make sure that no other program is called, no command is issued before capturing $?.
It applies to the last return value.

 
Whatever. Here's a tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic