开发者

How to correctly run a .bat file from java under a windows service?

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

    private void posAppRunner(final String path[], final Class targetClass) {
    new Thread(new Runna开发者_如何转开发ble() {
        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:

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.

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!

Could you expain me, how to overcome this situation?


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.

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

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜