简体   繁体   English

在计划模式下执行bat文件(java程序)后,命令提示符不会退出

[英]command prompt is not exiting after executing the bat file (java program) in scheduled mode

The following code in a batch file will exit properly if I execute it manually. 如果我手动执行它,批处理文件中的以下代码将正确退出。 However, if I run the batch file using a scheduler app, it is not terminating on Windows Server 2008 machine. 但是,如果我使用调度程序应用程序运行批处理文件,它不会终止在Windows Server 2008计算机上。 Application is exiting however cmd is not. 应用程序正在退出,但cmd不是。

Any help will be very much appreciated. 任何帮助将非常感谢。

java -Xms1024M -Xmx1024M -jar myAPP.jar -o true
EXIT;

One mistake is using EXIT; 一个错误是使用EXIT; instead of just exit . 而不是exit

Although on reaching end of batch file the command process automatically terminates and therefore exit at end of a batch file is usually not needed. 虽然在到达批处理文件的末尾时,命令进程会自动终止,因此通常不需要在批处理文件的末尾exit The exception is the batch file is called from another batch file and under certain conditions the entire command process executing both batch files should be exited immediately without returning back to parent batch file. 例外是从另一个批处理文件调用批处理文件,并且在某些条件下,应立即退出执行两个批处理文件的整个命令进程,而不返回父批处理文件。

The command EXIT without parameter /B always results in exiting command process making it also very difficult to debug a batch file from within a command prompt window as just exit results in closing also the command prompt window. 没有参数/B的命令EXIT总是导致退出命令进程,使得从命令提示符窗口调试批处理文件也很困难,因为exit导致关闭命令提示符窗口。 So in general it is always better to using exit /B when needed at all. 因此,一般情况下,在需要时使用exit /B总是更好。

But most likely java.exe does not exit and therefore the batch processing does not end. 但很可能java.exe不会退出,因此批处理不会结束。

There are several information missing in question. 有几个信息缺失。

  1. Why using a batch file at all when running only java.exe with several parameters? 为什么在仅运行带有多个参数的java.exe时完全使用批处理文件?
    Why not creating a scheduled task with that command line? 为什么不使用该命令行创建计划任务?

  2. What is the environment for java.exe on running this command line as scheduled task? 运行此命令行时java.exe作为计划任务的环境是什么?
    Is the scheduled task configured to run with system account or with your user account? 计划任务是否配置为使用系统帐户或用户帐户运行?
    The environment variables defined for system account are surely different to the environment variables of your user account. 为系统帐户定义的环境变量肯定与您的用户帐户的环境变量不同。 Java usually depends on several environment variables, except the data predefined with environment variables are specified on command line with using the appropriate Java command line options . Java通常依赖于几个环境变量,除了使用适当的Java命令行选项在命令行上指定使用环境变量预定义的数据。

  3. Why not using javaw.exe (no console window) instead of java.exe (with console window) which would result in not opening a console window at all? 为什么不使用javaw.exe (没有控制台窗口)而不是java.exe (带控制台窗口),这会导致根本没有打开控制台窗口?

  4. It is always helpful on running an application (or batch file) as scheduled task to be independent on the environment variables PATHEXT and PATH by specifying each application to run with full path and with file extension enclosed in double quotes. 通过将每个应用程序指定为以完整路径运行并且文件扩展名用双引号括起来,将应用程序(或批处理文件)作为计划任务运行以独立于环境变量PATHEXTPATH始终是有用的。
    Using just java requires that Windows command processor searches in current directory and next in all directories according to PATH defined for the currently used account for a java.* file having a file extension listed in PATHEXT defined for the currently used account. 仅使用java要求Windows命令处理器根据为当前使用的帐户定义的PATH在当前目录和下一个目录中搜索,该文件具有在为当前使用的帐户定义的PATHEXT列出的文件扩展名的java.*文件。

  5. Double clicking on a batch file results in execution of the batch file by cmd.exe with the directory of the batch file as current directory. 双击批处理文件会导致cmd.exe执行批处理文件,并将批处理文件的目录作为当前目录。 But running a batch file with Run as administrator or as scheduled task results in executing the batch file with %SystemRoot%\\System32 as current directory. 但是, Run as administrator身份Run as administrator或作为计划任务运行批处理文件会导致将%SystemRoot%\\System32作为当前目录执行批处理文件。
    So is it a good idea to use just myAPP.jar on command line without full path to this Java package with enclosing the name of the file with path in double quotes in case of a space or another character listed on last paragraph on last help page output by running in a command prompt window cmd /? 因此,在命令行上仅使用myAPP.jar是一个好主意,没有这个Java包的完整路径,如果在上一个帮助页面的最后一个段落中列出空格或其他字符,请用双引号括起文件名通过在命令提示符窗口cmd /?运行输出cmd /? is in file path? 在文件路径?
    I think, this is not a good idea. 我想,这不是一个好主意。 I'm quite sure that myAPP.jar can't be found in the directory %SystemRoot%\\System32 being the current directory on running the batch file as scheduled task. 我非常确定在%SystemRoot%\\System32目录中找不到myAPP.jar ,因为当前目录是将批处理文件作为计划任务运行的。

It is a common mistake made by many users that they think the environment (current directory, environment variables) on running a batch file as scheduled task is the same as on double clicking on the batch file in their user environment. 许多用户常常错误地认为,将批处理文件作为计划任务运行的环境(当前目录,环境变量)与双击其用户环境中的批处理文件相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM