You are not logged in.

#1 2012-05-08 20:39:49

ezzetabi
Member
Registered: 2006-08-27
Posts: 947

Java program with return value, what is the best way?

I was thinking something like this:

public class A {
   public static int intMain(String... argv) {
      ///real program here
   }
   public static void main(String... argv) {
      System.exit(intMain(argv));
   }
}

But System.exit does not await user threads and I use a library which creates threads where I do not have control.
Is there a way to await every user thread without explicitly knowing them?

Offline

#2 2012-05-09 16:12:59

holland01
Member
Registered: 2011-12-22
Posts: 50

Re: Java program with return value, what is the best way?

All I can tell you is that this isn't C/C++. Java is a very different language, with a very different programming methodology. Using intMain in a Java program to me seems a overly complex...unless you're doing a switch/if else statement and returning runtime codes as integers in your System.exit function. In that case, I suppose it is useful.

What library are you using for threading?

Offline

#3 2012-05-10 08:17:46

ezzetabi
Member
Registered: 2006-08-27
Posts: 947

Re: Java program with return value, what is the best way?

I just want to return a value to the OS... zero in case of normal quit, non-zero in case of abnormal termination. I do not see anything complex in that. Actually it I think is crazy that Java mains are only void.

About threads I use rabbitmq and it uses threads for its messaging. I noticed the problem because if I forget to correctly close rabbitmq objects the ``void main'' does not terminate while the ``int main'' does since system.exit does not wait.
At the moment the question is more about curiosity, really there is no simple way to say ``await all users threads''?

Offline

#4 2012-05-10 09:28:16

hesse
Member
Registered: 2011-12-08
Posts: 88

Re: Java program with return value, what is the best way?

If you want to return a value to the shell (C-style), depending on your program state on exit, use

// successful state
System.exit(0)
// erroneous  state
System.exit(1)

Last edited by hesse (2012-05-10 09:28:35)

Offline

#5 2012-05-10 09:42:17

Ramses de Norre
Member
From: Leuven - Belgium
Registered: 2007-03-27
Posts: 1,289

Re: Java program with return value, what is the best way?

This should work, as long as intMain does not return before the program is actually done.
You can explicitly make a thread wait for another one to complete, but I wont start explaining the whole java threading model here, there are very good tutorials online.

public class A {
  public static int intMain(String... argv) {
    ///real program here
  }
  
  public static void main(String... argv) {
    final int code = intMain(argv);    
    System.exit(code);
  }
}

Offline

Board footer

Powered by FluxBB