0

I have a small problem with my batch file, here is my code:

Java -jar ****name.jar -commands****

So incase the user does have java installed it should give proper error code and if it has java installed and the java does not find the file its trying to run it should give proper error code and so on.

If "%Errorlevel%" NEQ "0" (
    For /F "Tokens=*" %%C In ('Net Helpmsg %Errorlevel%') Do (
    Echo Error Level: [Return Code #%Errorlevel% - %%C]
    )
)

I was hoping it would get me correct error codes but it doesn't. Like for example if the user does not have java installed it would say something like this "java is not recognized.." and it should give an correct error code beneath it but unfortunately, it doesn't.

Here is what i have tried:

  • Removed the quotation around Errorlevel so its does not treat it like a string.

I was hoping anyone could point out my mistake?

7
  • 1
    Read this first: SET variable doesn't work in block Commented Aug 3, 2016 at 13:22
  • @jeb That doesn't appear to be a duplicate - the OP isn't setting anything in the IF/FOR. The code seems to do what the OP wants if ERRORLEVEL has a value before hand: the problem is more likely that either the (unshown) commands aren't setting errorlevel or it's being clearead before testing. Commented Aug 3, 2016 at 13:24
  • @TripeHound That's the cause why I removed my duplicate marker. I didn't read the question carefully enough Commented Aug 3, 2016 at 13:26
  • 1
    You need to show the lines causing the error. And if there is a block surrounding all these commands Commented Aug 3, 2016 at 13:27
  • Sorry for the lack of info. Updated the post. Commented Aug 3, 2016 at 17:02

1 Answer 1

1

The error numbers used by net helpmsg and ErrorLevel are totally different things; net helpmsg only understands error numbers returned by the net command.

What you are trying to accomplish is the following, I think (take a look at the explanatory remarks):

rem /* Test whether `java` is installed and can be found: */

rem // simply try to run the `java` executable:
java -version 2> nul
if ErrorLevel 1 echo ErrorLevel is set to %ErrorLevel% [9009 if not found, 0 otherwise].

rem // or use the `where` command to find the `java` executable:
where java > nul 2> nul
if ErrorLevel 1 echo ErrorLevel is set to %ErrorLevel% [1 if not found, 0 otherwise].

if ErrorLevel 1 goto :EOF

rem /* Thest whether the Java script `name.jar` exists: */

rem // simply try to run the Java script:
java name.jar -commands 2> nul
if ErrorLevel 1 echo ErrorLevel is set to %ErrorLevel% [1 if not found, or, if the script exists, its `ErrorLevel`].

rem // or use the `where` command to find the `java` executable:
where name.jar > nul 2> nul
if ErrorLevel 1 echo ErrorLevel is set to %ErrorLevel% [1 if not found, 0 otherwise].

rem /* (the `> nul` suffix suppresses output messages,
rem     the `2> nul` suffix suppresses error messages) */


rem /* To capture the error message returned by any command, do this: */
for /F delims^=^ eol^= %%E in ('
    command 2^>^&1 ^> nul
') do (
    echo This is the error message: "%%E"
)

Since the question post has been updated and clarified, the following does not cover the problem. The true solution can be found above. I keep the former answer here for reference.
This is for the case you want to extract the error number returned by the net command, which is specific to this command and has got nothing to to with the ErrorLevel:

The error number of the net command and the ErrorLevel are totally different things.

Supposing you try to execute net view, it might fail with this error message, for instance:

System error 6118 has occurred.

The list of servers for this workgroup is not currently available

The resulting ErrorLevel is 2 though.
So executing net helpmsg %ErrorLevel% does not make any sense.

In contrast, net helpmsg 6118 does, which returns the following string:

The list of servers for this workgroup is not currently available

To capture the error number of the net command, use a for /F loop and take the line of the first iteration only by an if defined condition (using command net view in this example):

set "ERRNUM=" & set "ERRTXT="
for /F "delims=" %%M in ('2^>^&1 ^> nul net view') do (
    rem Note that the `token` option is language-dependent:
    for /F "tokens=3" %%N in ("%%M") do (
        if not defined ERRNUM set "ERRNUM=%%N"
    )
    set "ERRTXT=%%M"
)

This would capture the error number 6118 of the above example and store it in variable ERRNUM. The last line of the error output (that I consider as the error message) is stored in variable ERRTXT.

To retrieve the error message later from a given error number stored in ERRNUM, you could use this:

set /A "ERRNUM+=0"
if %ERRNUM% NEQ 0 (
    for /F "delims=" %%M in ('net helpmsg %ERRNUM%') do (
        set "ERRTXT=%%M"
    )
)

Side Note: Yes, I would remove the quotes if %ErrorLevel% NEQ 0 to do a numeric comparison.

Sign up to request clarification or add additional context in comments.

6 Comments

I am sorry for a late reply, I was trying to understand the code but, i cant really. If you could compile it with my code that be much more helpful as I could understand more about this.
Please show the code portion that is returning the error you mentioned in your question...
'If "%Errorlevel%" NEQ "0" ( set "ERRNUM=" & set "ERRTXT=" for /F "delims=" %%M in ('2^>^&1 ^> num net view') do ( rem Note that the token option is language-dependent: for /F "tokens=3" %%N in ("%%M") do ( if not defined ERRNUM set "ERRNUM=%%N" ) set "ERRTXT=%%M" )) echo %ERRNUM% %ERRTXT% pause'
No, I meant the original code that causes the error (Java is not recognized), by editing your question; code in comments is hardly readable...
Sorry about that, updated the post. Let me know if you need any more info.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.