1

Is it possible to run a java class in a batch file with a main method inside, changing the return type of the method from "void" to another type, and get the return value in batch file?

I mean something similar to the following:

Java class:

public class Main{

    public static String main(String[] args) {
        public String returnValue;
        
        // code to execute...
        
        return returnValue;
    }
}

Batch file:

...
set RETURNVALUE = ""
//call "java com.test.Main" and store return value in RETURNVALUE variable
...

I need to make some checks in the java class, and then execute the subsequent instructions in batch file according to the result given by the java class.

Any help will be appreciated

bisi
  • 31
  • 3
  • Maybe you can pass a temp file as an argument and store the result there? – Barracuda Nov 22 '22 at 08:30
  • `main(String...)` can't return any value if it's to be a real main method. You'd need to write the output somewhere else, e.g. to stdout (via `System.out`) and have your batch read it. – Thomas Nov 22 '22 at 08:33
  • Would you be able to solve the problem if you were just running an existing executable program? Because that is effectively what happens here. Or is the problem "how do I make the Java program have an exit status code?" Or just what? "Tried many ways to execute the code and store the value" **Why are you telling us this**? If it's because you think "I need to claim that I am not lazy in order to get an answer", that is the wrong mindset - we want to **see** what you tried *in order to fix those attempts*. – Karl Knechtel Nov 22 '22 at 08:58
  • @KarlKnechtel I'm quite new to this portal, so when I've been taken to the "Explain you attempts" section **I thought it was mandatory**. So I wrote that statement because I honestly didn't remember all my attempts (**I have no need to demonstrate I'm not lazy here**). But now the question is.. Is the problem just that sentence? What would have happened if I had left that section blank? Is it possible on this portal to make a question without first doing several attempts on your own? If so, maybe I have the wrong mindset.. Now that I know, I've immediately delete that sentence. – bisi Nov 22 '22 at 11:52
  • You could send stdout into [a variable](https://stackoverflow.com/questions/2033338/get-stdout-into-a-variable) but this is not really a Java question at all (because it would be the same with the stdout of any process) but a question for the Windows/DOS topic – g00se Nov 22 '22 at 12:02
  • "so when I've been taken to the "Explain you attempts" section I thought it was mandatory." Please note carefully: telling us "I tried many ways to do this" **is not explaining anything**. – Karl Knechtel Nov 23 '22 at 16:52

1 Answers1

3

You can end the main method with System.exit() instead of return:

public class Main{
    public static void main(String[] args) {
        int returnValue;

        // code to execute...
        returnValue = 123;
        
        System.exit(returnValue);
    }
}

Then in the script:

#!/bin/bash

java ./Main.java
RETURNVALUE=$?

echo "The return value was $RETURNVALUE"

which gives you:

$ ./script.sh  
The return value was 123

The exit code can't be a string, only a number from 0 to 255. If you want to return a string instead, consider printing to stdout instead and saving that to a variable.

haaks
  • 31
  • 2
  • It's a *batch file* not a Bash script – g00se Nov 22 '22 at 09:34
  • @g00se+ in batch (i.e. CMD) use `%errorlevel%`. But it must be on a subsequent line, unlike Unix shells where you could do one line like `java blah; RETURN=$?` – dave_thompson_085 Nov 22 '22 at 09:57
  • @dave_thompson_085 Yes, I know but that's not what the OP wants ;) afaics It's a string that's required – g00se Nov 22 '22 at 10:03
  • @g00se: the Q gives String as an example but text only says 'another type [than void]'. And this A is specifically for a number (in Unix only a _small_ number). – dave_thompson_085 Nov 23 '22 at 07:59