Can "main" in java return a String?

17,081

Solution 1

In short - no, it can't. You can always print to stdout from the main method (using System.out.print or System.out.println), but you can't change the return type of main.

Solution 2

The main method's return type must be void, because the java language specification enforces it. See 12.1.4.

For interprocess communication you can either use:

  • System.in and System.out
  • Sockets

Solution 3

No. The to be a main() method, it must return nothing (ie be void).

However, you could refactor your code if you need the functionality of your method returning something:

public static void main(String[] args) throws IOException {
    myMain(args);
}

public static String myMain(String[] args) throws IOException {
    // your method, which can now be called from anywhere in your code
}

Solution 4

No you can't... once the main is finished the program is dead.. So you don't have any benefit from that.. What is you purpose? What you are trying to achieve?

You can wrap all in other method that will return String to your main.

public static void main(String[] args) throws IOException {
   String result = doSomething();
return result;


}
public static String doSomething() {
   String str = null;
   TurkishMorphParser parser = TurkishMorphParser.createWithDefaults();
   str = new Stm(parser).parse("bizler");
   System.out.println("str = " + str);
   String replace = str.replace("[","");
   String replace1 = replace.replace("]","");
   List<String> result1 = new ArrayList<String>(Arrays.asList(replace1.split(",")));
   String result = result1.get(0);
   System.out.println("Result = " + result);
}

Solution 5

Yes you can but you can't run that class. You will get error

class Test {

        public static String main(String[] args) {
                return "1";
        }
}

You will get error as

Error: Main method must return a value of type void in class Test, please 
define the main method as:
   public static void main(String[] args)
Share:
17,081
Jeren
Author by

Jeren

Updated on June 12, 2022

Comments

  • Jeren
    Jeren about 2 years

    Is it possible that public static void main(String[] args) in java returns String instead of void? If yes, how?

    public static String main(String[] args)
    

    instead of:

    public static void main(String[] args)
    

    when I change my code as below:

    public static String main(String[] args) throws IOException {
        String str = null;
        TurkishMorphParser parser = TurkishMorphParser.createWithDefaults();
        str = new Stm(parser).parse("bizler");
        System.out.println("str = " + str);
        String replace = str.replace("[","");
        String replace1 = replace.replace("]","");
        List<String> result1 = new ArrayList<String>(Arrays.asList(replace1.split(",")));
        String result = result1.get(0);
        System.out.println("Result = " + result);
        return result;        
    }
    

    I receive this error:

    Error: Main method must return a value of type void in class Stm, please define the main method as:
    public static void main(String[] args)
    
  • Makoto
    Makoto about 10 years
    The declarations would have the same signature if run in the same class.
  • kviiri
    kviiri about 10 years
    @Makoto, I don't see how that relates to this.
  • Makoto
    Makoto about 10 years
    It would. If you declared public static void main(String[] args) and public static String main(String[] args), that wouldn't compile since both methods would have the same signature. It's not a complete answer until it addresses that point.
  • kviiri
    kviiri about 10 years
    @Makoto, yes, but I don't see there being a question about having both versions of main in the same class. Only about whether main can return a String instead of void, which I already answered.
  • Jus12
    Jus12 almost 8 years
    A bit incorrect. You cannot run the class using the usual way. But you can write a custom loader that executes the modified main.