简体   繁体   English

带日志文件但不能用java程序执行的bat文件不生成

[英]Bat file with log file but not able to execute with java program log file is not generation

I created one bat file in that I am trying to copy folder and generate its log into myLog.txt file Now I am trying to run the bat file through the java program it's getting executed but it's not able to generate a log file please help me我创建了一个 bat 文件,我试图复制文件夹并将其日志生成到 myLog.txt 文件中 现在我试图通过它正在执行的 java 程序运行 bat 文件,但它无法生成日志文件

public static void allBatch()
    {
        
         try {
                // Process p =  Runtime.getRuntime().exec("cmd /c stopTomact.bat", null, new File("C:\\Users\\Ajay\\Documents"));
                 Process process =Runtime.getRuntime().exec("cmd /C start C:\\Users\\Ajay\\Documents\\batchFile.bat");
                   System.out.println("Stopped"); 
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
        
        
    }

@echo off @回声关闭

@echo off @回声关闭

call :main >myLog.txt 2>&1调用 :main >myLog.txt 2>&1

:main if exist "C:\\Users\\Ajay\\Documents\\old Tomcat\\Controller*.*" ( :main 如果存在 "C:\\Users\\Ajay\\Documents\\old Tomcat\\Controller*.*" (

xcopy /e "C:\\Users\\Ajay\\Documents\\old Tomcat\\Controller*.*" "C:\\Users\\Ajay\\Documents\\newTomcat\\Controller*" /y echo %date%-----Copy Successful------%time% xcopy /e "C:\\Users\\Ajay\\Documents\\old Tomcat\\Controller*.*" "C:\\Users\\Ajay\\Documents\\newTomcat\\Controller*" /y echo %date%-----复制成功- - - -%时间%

echo --------------------------------------------------- net stop Tomcat8 sc query Tomcat8 echo %date%-----Stop Successful------%time%回声 ------------------------------------------------- -- net stop Tomcat8 sc query Tomcat8 echo %date%-----停止成功------%time%

rem start notepad "C:\\Users\\Ajay\\Documents" rem 启动记事本“C:\\Users\\Ajay\\Documents”

)else echo %date%------- sorry Failed TO Copy -----%time% )else echo %date%-------抱歉复制失败 -----%time%

timeout 5 > nul exit超时 5 > 空退出

You invoke the Runtime.exec() method.您调用 Runtime.exec() 方法。 The method returns a Process instance, and in it's documentation you can read该方法返回一个 Process 实例,您可以在它的文档中阅读

By default, the created process does not have its own terminal or console.默认情况下,创建的进程没有自己的终端或控制台。 All its standard I/O (ie stdin, stdout, stderr) operations will be redirected to the parent process, where they can be accessed via the streams obtained using the methods getOutputStream(), getInputStream(), and getErrorStream().它的所有标准 I/O(即 stdin、stdout、stderr)操作将被重定向到父进程,在那里可以通过使用 getOutputStream()、getInputStream() 和 getErrorStream() 方法获得的流访问它们。 The parent process uses these streams to feed input to and get output from the process.父进程使用这些流向进程提供输入并从进程中获取输出。 Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the process may cause the process to block, or even deadlock.由于一些原生平台只为标准输入输出流提供有限的缓冲区大小,如果不能及时写入进程的输入流或读取输出流,可能会导致进程阻塞,甚至死锁。 https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Process.html https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Process.html

So it is likely your process is started by the OS but gets blocked due to I/O restrictions.因此,您的进程很可能是由操作系统启动的,但由于 I/O 限制而被阻止。 Get around that by reading the STDOUT and STDERR streams until your process finishes.通过读取 STDOUT 和 STDERR 流来解决这个问题,直到您的过程完成。 One good programming model is visible at https://www.baeldung.com/run-shell-command-in-javahttps://www.baeldung.com/run-shell-command-in-java上可以看到一种好的编程模型

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

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