如何在Windows服务下从java正确运行.bat文件?

时间:2022-01-29 16:02:05

From my java project I want to run an external .bat file in another thread. For this purpose I use the following method:

从我的java项目我想在另一个线程中运行外部.bat文件。为此,我使用以下方法:

    private void posAppRunner(final String path[], final Class targetClass) {
    new Thread(new Runnable() {
        public void run() {
            try {
                String line;
                Process p = Runtime.getRuntime().exec(path);
                BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
                while ((line = input.readLine()) != null) {
                    System.out.println(line);
                }
                input.close();
            } catch (IOException e) {
                LogFactory.getLog(targetClass).warn("Error when starting a PosApplication: " + e.getMessage());
            }
        }

    }).start();

I run the following .bat file:

我运行以下.bat文件:

call chdir %~dp0

start java <_some_arguments>

So when I do it locally from IntelliJ IDEA it works correct - a cmd process appears, after that a java process appears and after that the cmd process disappears.

因此,当我从IntelliJ IDEA本地执行它时它工作正确 - 出现一个cmd进程,之后出现一个java进程,然后cmd进程消失。

But when I run my java project with this method through ANT under TeamCity windows service, only cmd process appears and nothing happens after. Java process that must be started from the bat file doesn't appear. It looks like I don't read the process output but I do!

但是当我在TeamCity Windows服务下通过ANT使用此方法运行我的java项目时,只显示cmd进程,之后没有任何反应。不会出现必须从bat文件启动的Java进程。看起来我没有阅读过程输出,但我做到了!

Could you expain me, how to overcome this situation?

你能告诉我,如何克服这种情况?

1 个解决方案

#1


0  

I believe that the problem is in current working directory. I am not so familiar with bat files and do not remember by heart what does %~dp0 mean. Anyway as the first attempt try to modify your batch file to contain the hard coded path. I believe that this will work.

我相信问题出现在当前的工作目录中。我对蝙蝠文件不太熟悉,不记得%~dp0是什么意思。无论如何,作为第一次尝试尝试修改您的批处理文件以包含硬编码路径。我相信这会奏效。

In this case decide what is better for you: discover the path in java code and then pass it to batch file, generate batch file on the fly, so that it contains all parameters hard coded or debug the script. for example you can remove the start java <_some_arguments> and put

在这种情况下,决定什么是更好的:在java代码中发现路径,然后将其传递给批处理文件,动态生成批处理文件,以便它包含硬编码的所有参数或调试脚本。例如,你可以删除start java <_some_arguments>并放入

echo %~dp0 > c:\temp\batlog.log

echo%~dp0> c:\ temp \ batlog.log

this will print the parameter to log file. Now run it as service and see what does the log file contain. Probably you will see the problem immediately.

这会将参数打印到日志文件中。现在将其作为服务运行,并查看日志文件包含的内容。可能你会立即看到问题。

#1


0  

I believe that the problem is in current working directory. I am not so familiar with bat files and do not remember by heart what does %~dp0 mean. Anyway as the first attempt try to modify your batch file to contain the hard coded path. I believe that this will work.

我相信问题出现在当前的工作目录中。我对蝙蝠文件不太熟悉,不记得%~dp0是什么意思。无论如何,作为第一次尝试尝试修改您的批处理文件以包含硬编码路径。我相信这会奏效。

In this case decide what is better for you: discover the path in java code and then pass it to batch file, generate batch file on the fly, so that it contains all parameters hard coded or debug the script. for example you can remove the start java <_some_arguments> and put

在这种情况下,决定什么是更好的:在java代码中发现路径,然后将其传递给批处理文件,动态生成批处理文件,以便它包含硬编码的所有参数或调试脚本。例如,你可以删除start java <_some_arguments>并放入

echo %~dp0 > c:\temp\batlog.log

echo%~dp0> c:\ temp \ batlog.log

this will print the parameter to log file. Now run it as service and see what does the log file contain. Probably you will see the problem immediately.

这会将参数打印到日志文件中。现在将其作为服务运行,并查看日志文件包含的内容。可能你会立即看到问题。