Tag: exec

用Java运行bat文件并等待

你会认为从Java启动一个bat文件是一件容易的事,但是没有…我有一个bat文件,它为一个从文本文件中读取的值循环执行一些sql命令。 它或多或少是这样的: FOR /F %%x in (%CD%\listOfThings.txt) do sqlcmd -Slocalhost\MSSQL %1 %2 -d %3 -i %CD%\SQLScripts\\%%x exit 不要担心它们不重要的细节。 我想要的是简单地从Java中运行这个bat文件,让它等到执行完成。 显然这并不容易。 到目前为止我所拥有的是: Runtime.getRuntime().exec(“cmd /K start SQLScriptsToRun.bat” +” -U”+getUser() +” -P”+getPass() +” ” + projectName); return true; 问题是exec()方法立即返回。 bat文件运行2-3分钟。 我尝试删除开始但无济于事。 我尝试了很多变化,但它让我无处可去。 关于如何完成这个简单任务的任何想法?

为什么ant.bat在以编程方式运行时不会返回错误状态?

当我从命令行运行ant时,如果我遇到故障,我会得到一个非零退出状态(在UNIX上是$ ?,在Windows上是%ERRORLEVEL%)。 但是我们有一个运行ant的Java程序(通过ProcessBuilder),当ant失败时,在Windows上我们无法获得退出状态。 我刚用这个简单的ant测试文件validation了这个: 在UNIX上,运行ant会打印失败消息,并回显$? 之后打印1.在Windows上,运行ant或ant.bat打印失败消息,然后回显%ERRORLEVEL%打印1。 现在,使用下面的测试程序:在UNIX上,java Run ant打印失败消息,并回显$? 后续打印1.在Windows上,java Run ant无法找到名为ant的程序运行,但java Run ant.bat打印出一条失败消息,然后回显%ERRORLEVEL%后打印0 。 是什么赋予了? 我们依靠能够在运行ant之后检查退出状态。 无论如何,我们是。 为什么我们不能以编程方式依赖于此? 测试程序: import java.io.*; public class Run { public static void main(String[] args) throws IOException, InterruptedException { ProcessBuilder pb = new ProcessBuilder(args); Process p = pb.start(); ProcThread stdout = new ProcThread(p.getInputStream(), System.out); ProcThread stderr = new ProcThread(p.getErrorStream(), […]

如何让java getRuntime()。exec()运行带参数的命令行程序?

我一直在尝试编写一个使用Runtime.getRuntime().exec()方法的java程序来使用命令行来运行程序“tesseract”的实例。 一些背景,Tesseract是一个免费的开源程序,用于在图片上执行OCR(光学字符识别)。 它接收图片文件并输出文本文档。 它是一个使用此命令运行的命令行程序 (从命令提示符shell中) tesseract imageFilePath outFilePath [optional arguments] 例: tesseract “C:\Program Files (x86)\Tesseract-OCR\doc\eurotext.tif” “C:\Users\Dreadnought\Documents\TestingFolder\out” 第一个参数调用tesseract程序,第二个参数是图像文件的绝对路径,最后一个参数是输出文件应该是什么的路径和名称。 Tesseract只需要输出文件的名称,它不需要扩展名。 在命令提示符下工作,这非常有效。 但是,我想从java程序运行它并遇到一些错误。 我发现这个代码作为一个起点非常有用 public class Main { public static void main(String args[]) { try { Runtime rt = Runtime.getRuntime(); String cmdString = “cmd /c dir”; System.out.println(cmdString); Process pr = rt.exec(cmdString); BufferedReader input = new BufferedReader(new InputStreamReader( pr.getInputStream())); […]

Android:ffmpeg,文件名包含空格

我想从Android应用程序执行ffmpeg,非常如下所述: 将FFmpeg与Android-NDK配合使用 。 执行以下命令可以正常工作: Process p = Runtime.getRuntime().exec(“/data/data/yourpackagename/ffmpeg -i infile.mp4 outfile.mp4”); 要么 Process p = Runtime.getRuntime().exec(new String[]{“/data/data/yourpackagename/ffmpeg”, “-i”, “infile.mp4”, “outfile.mp4”); 但是当输入或输出文件名包含空格时,ffmpeg会因“找不到文件”错误而失败: Process p = Runtime.getRuntime().exec(“/data/data/yourpackagename/ffmpeg -i \”in space file.mp4\” outfile.mp4″); // fails Process p = Runtime.getRuntime().exec(“/data/data/yourpackagename/ffmpeg -i ‘in space file.mp4’ outfile.mp4”); // fails Process p = Runtime.getRuntime().exec(new String[]{“/data/data/yourpackagename/ffmpeg”, “-i”, “\”in space file.mp4\””, “outfile.mp4”); //fails 对于没有空格但引用的文件,FFMPEG也会失败: […]

从Java执行CURL – 什么是正确的方法?

我通过以下代码执行curl: // execute process Process pr = null; Runtime run = Runtime.getRuntime(); try { pr = run.exec(cmdline.split(” “)); A ret = ff(pr); pr.waitFor(); return ret; } catch (Exception ex) { throw new RuntimeException(“Executing ” + cmdline, ex); } finally { try { // close all those bloody streams pr.getErrorStream().close(); pr.getInputStream().close(); pr.getOutputStream().close(); } catch (IOException ex) […]

如何杀死runtime.exec?

我在我的jframe中使用下面的代码,以便通过java程序执行shell脚本,但是当我实现其他方法来停止运行过程时,此方法被第一个阻止,我无法停止我的程序。 所以我要杀死新进程,问题是如何获取进程的pid以及如何将其置于后台,以便杀死它。 public static void execShellCmd(String cmd) { try { testPath = getPath(); System.out.println(cmd); Runtime runtime = Runtime.getRuntime(); process = runtime.exec(new String[] { “sh”, testPath + “/install.sh”, cmd, “&” }); int exitValue = process.waitFor(); value = exitValue; System.out.println(“exit value: ” + exitValue); BufferedReader buf = new BufferedReader(new InputStreamReader( process.getInputStream())); String line = “”; BufferedWriter bufferedWriter […]

Maven Exec插件:如何配置工作目录

我正在使用带有以下命令的Exec Maven插件: mvn exec:java 我没有设法用这种执行模式设置工作目录 。 我想使用mainClass(在特定的包中),我希望我的执行的根文件夹在$ {basedir}之外的另一个目录中。 感谢您的帮助。 我的pom.xml目标对我不起作用: org.codehaus.mojo exec-maven-plugin 1.3.2 ${project.build.directory}\classes com.package.MyMainClass true 使用-X选项的结果 [DEBUG] Configuring mojo org.codehaus.mojo:exec-maven-plugin:1.3.2:java from plugin realm ClassRealm[plugin>org.codehaus.mojo:exec-maven-plugin:1.3.2,parent: sun.misc.Launcher$AppClassLoader@11b86e7] [DEBUG] Configuring mojo ‘org.codehaus.mojo:exec-maven-plugin:1.3.2:java’ with basic configurator –> [DEBUG] (f) arguments = [] [DEBUG] (f) classpathScope = runtime [DEBUG] (f) cleanupDaemonThreads = true [DEBUG] (f) daemonThreadJoinTimeout = 15000 [DEBUG] […]

从Java运行PHP

我试图从java文件中运行以下命令。 我正在尝试显示php版本,稍后将更改为运行php文件。 Process p = Runtime.getRuntime().exec(“cmd /C PHP/php.exe -v”); BufferedReader in = new BufferedReader(new InputStreamReader( p.getInputStream())); String line = null; while ((line = in.readLine()) != null) { System.out.println(line); } 当我运行程序时,没有错误。 但控制台没有显示任何东西:( 谢谢。

Java:等待exec进程直到它退出

我在Windows中运行一个java程序,从Windows事件中收集日志。 创建一个.csv文件,在该文件上执行某些操作。 命令被执行和管道。 如何让我的Java程序等到进程完成? 这是我正在使用的代码片段: Runtime commandPrompt = Runtime.getRuntime(); try { Process powershell = commandPrompt.exec(“powershell -Command \”get-winevent -FilterHashTable @{ logname = ‘Microsoft-Windows-PrintService/Operational’;StartTime = ‘”+givenDate+” 12:00:01 AM’; EndTime = ‘”+beforeDay+” 23:59:59 ‘; ID = 307 ;} | ConvertTo-csv| Out-file “+ file +”\””); //I have tried waitFor() here but that does not seem to work, required command […]

从Java执行命令并等待命令完成

在我的Java程序中,我创建了一个执行命令来运行批处理文件的进程,如下所示: try { File tempFile = new File(“C:/Users/Public/temp.cmd”); tempFile.createNewFile(); tempFile.deleteOnExit(); setContents(tempFile, recipe.getText()); //Writes some user input to file String cmd = “cmd /c start ” + tempFile.getPath(); Process p = Runtime.getRuntime().exec(cmd); int exitVal = p.waitFor(); refreshActionPerformed(evt); } catch (InterruptedException ex) { Logger.getLogger(mainFrame.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(mainFrame.class.getName()).log(Level.SEVERE, null, ex); } 现在,我想要发生的是命令 refreshActionPerformed(evt); […]