从批处理文件中的Java应用程序获取退出代码 [英] Get exit code from a java application in batch file

查看:114
本文介绍了从批处理文件中的Java应用程序获取退出代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在努力为我们的一个Java应用程序创建测试用例. 在我的代码中,我的Java应用程序调用了一个批处理文件,该文件又启动了一个单独的Java进程,该进程返回了我需要从调用Java应用程序中使用的错误代码. 我正在执行以下操作来调用我的批处理文件:

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();

批处理文件如下:

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

现在有了上面的代码和批处理文件,我的JUnit框架就挂在这个特定的测试用例上,好像在等待它结束一样.现在,当JUnit挂在测试用例上时,转到任务管理器,并结束java.exe进程,将使JUnit框架可以继续处理其他用例.

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.

双击运行.bat文件,它将正常运行Java应用程序.

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

在批处理文件中的java命令之前添加START批处理命令似乎可以解决挂起的问题,但是我似乎无法从Java应用程序中获取正确的退出代码,因为它始终为0.( Java应用程序使用System.exit(INTEGER_VALUE)退出并显示错误代码.我假设%errorlevel%值被开始"命令自己的退出值覆盖.

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?

谢谢.

P.S:如果有什么不同,我正在使用JDK 5和Netbeans 5.5.1.

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

推荐答案

请勿在出口处使用/B .这是我编写脚本的方法:

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%

注意:另外,您可以通过3种不同的方式执行Java程序:

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

这非常关键,因为您的Java命令可能会以退出代码退出,并且为了将退出代码正确传递给ERRORLEVEL var,您需要使用上面不确定的正确方法.

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.

这篇关于从批处理文件中的Java应用程序获取退出代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆