3

I'm currently making an effort to create test cases for one of our java applications. In my code, my java application calls a batch file, which in turn starts a separate java process, that returns an error code that I need to consume from the calling java application. I'm doing the following to invoke my batch file:

Process process = runTime.exec(new String[]{"cmd.exe","/c",scriptPath});
exitValue = process.waitFor();

The batch file is as follows:

@echo off
cd %~dp0
java -cp  frames.FrameDriver
SET exitcode=%errorlevel%
exit /B %exitcode%

Now with the above code and batch file, my JUnit framework just hangs on this particular test case, as if it's waiting for it to end. Now when JUnit is hanging on the test case, going to the Task Manager, and ending the java.exe process would allow the JUnit framework to continue with the other cases.

Running the .bat file by double clicking it runs the Java application normally.

Adding the START batch command before the java command in my batch file seems to fix the hanging problem, but I can't seem to get the correct exit code from my Java application as it's always 0. (The Java application exits with an error code using System.exit(INTEGER_VALUE)). I'm assuming that the %errorlevel% value is being overwritten by the "start" command's own exit value.

Can anyone please tell me how to solve this problem?

Thanks.

P.S: If it makes any difference, I'm using JDK 5 and Netbeans 5.5.1.

7
  • 4
    Is there a particular reason why you're doing this via a batch file rather than simply running the target java command directly via Runtime.exec or ProcessBuilder? Commented Feb 28, 2013 at 13:27
  • 2
    Mainly it is because this .bat file of mine is replicating a similar shell script that runs on Linux, and I wouldn't want to change the design of it just now. In production our application runs on Linux machines. Our development environment however is Windows. Commented Feb 28, 2013 at 13:40
  • Try replacing START with CALL. START is creating a new, external process that is no longer connected or responding to your batch file. Of course, the same "hang" might well occur with CALL but it's worth a try. Also try @CALL (note the at symbol). Commented Mar 1, 2013 at 7:19
  • The hang problem is no longer there when I use "START". When using "START", I guess the wrong exit code from my java application. Commented Mar 3, 2013 at 11:55
  • @MouhammedSoueidane How do you know when your frames.FrameDriver exits? Commented Mar 4, 2013 at 11:15

1 Answer 1

3

Don't use the /B on your exit. Here is how I would do a script:

@ECHO off
ECHO Running %~nx0 in %~dp0
CALL :myfunction World
java.exe -cp  frames.FrameDriver
IF NOT ERRORLEVEL 0 (
  SET exitcode=1
) ELSE (
  SET exitcode=0
)
GOTO :END
:myfunction
ECHO Hello %~1
EXIT /B 0
:END
EXIT %exitcode%

NOTE: Also, you can execute java program in 3 different ways:

  java.exe -cp  frames.FrameDriver
  CALL java.exe -cp  frames.FrameDriver
  cmd.exe /c java.exe -cp  frames.FrameDriver

This is very critical since, your Java command may exit with a exit code and in order to pass the exit code correctly to the ERRORLEVEL var, you need to use the correct method above, which I am unsure about.

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

Comments

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.