简体   繁体   English

如何运行包含对批处理文件调用的 jar 文件?

[英]How run a jar file which contains a call to a batch file?

I have made a java application that sometimes should call a batch file in order to move a video from a folder to another.我制作了一个 java 应用程序,有时应该调用一个批处理文件,以便将视频从一个文件夹移动到另一个文件夹。 The batch file is in the same directory of the main class of the project (ProjectName\\src\\move.bat).批处理文件位于项目主类的同一目录中(ProjectName\\src\\move.bat)。

I got the path of the batch file through the instruction:我通过指令得到了批处理文件的路径:

String pathMoveBat = new java.io.File("src\\move.bat").getAbsolutePath();

and I use the code below (which is called by pressing a button in the application) to call that file:我使用下面的代码(通过按下应用程序中的按钮调用)来调用该文件:

Process move = Runtime.getRuntime().exec(pathMoveBat+" "+username+" "+dateFormat.format(currentDate)+" "+i+"");

Basically, when I click on the button nothing happen and seems that Windows cannot find the file move.bat.基本上,当我点击按钮时什么也没有发生,似乎 Windows 找不到文件 move.bat。

Is there any other way to call that file from a jar?有没有其他方法可以从 jar 中调用该文件?

There are some points you need to understand before make it to work在使其工作之前,您需要了解一些要点

  1. In order to execute a batch file you need an external program like command prompt(cmd).为了执行批处理文件,您需要一个外部程序,如命令提示符(cmd)。
  2. You need to know the location of batch file.您需要知道批处理文件的位置。 Here in your case you are placing it under the src folder which will not available when you generate the jar.在您的情况下,您将它放在 src 文件夹下,当您生成 jar 时该文件夹将不可用。 Since the src is a source folder so the batch should be copied to bin/dist/classes folder (output as per the IDE or project settings).由于 src 是源文件夹,因此应将批处理复制到 bin/dist/classes 文件夹(根据 IDE 或项目设置输出)。
  3. While executing the process using Runtime make sure to provide relevant argument separated with spaces.在使用 Runtime 执行过程时,请确保提供用空格分隔的相关参数。 Here you are passing username and current date and i.在这里,您正在传递用户名和当前日期以及 i。
  4. Once you have executed the batch file using Runtime.exec get the Process.getInputStream to catch the output if successfully executed.使用 Runtime.exec 执行批处理文件后,如果成功执行,请获取 Process.getInputStream 以捕获输出。 You will also need Process.getErrorStream to check if you have any error while executing the batch file.您还需要 Process.getErrorStream 来检查在执行批处理文件时是否有任何错误。
  5. Last but not least close the streams, Process.getInputStream().close();最后但并非最不重要的是关闭流, Process.getInputStream().close(); Process.getOutputStream().close(); Process.getOutputStream().close(); Process.getErrorStream().close(); Process.getErrorStream().close();

Runtime.getRuntime().exec("cmd /c move.bat "+username+" "+dateFormat.format(currentDate)+" "+i+""); Runtime.getRuntime().exec("cmd /c move.bat "+username+" "+dateFormat.format(currentDate)+" "+i+"");

Switch to ProcessBuilder if you can, its better : https://stackoverflow.com/a/10723391/1129313如果可以,切换到 ProcessBuilder,它更好: https : //stackoverflow.com/a/10723391/1129313

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

相关问题 如何运行包含Clojure和Java代码编译的类文件的jar文件。 Java文件包含主要方法 - How to Run a jar file which contains clojure and java code compiled class files. java file contains main method 在 Python 中运行包含 java 命令的批处理文件 - Run a batch file in Python which contains java command 如何在另一个jar文件的类路径中的jar文件中运行类 - how to run a class in the jar file which is in the classpath of another jar file 如何从同一目录中存在的JAR文件运行或调用exe文件? - How to run or call an exe file from a JAR file which is present in the same directory? 如何创建批处理文件以运行系统任何位置的jar文件 - How to create a batch file to run a jar file that is in any location of the system 如何配置install4j以运行包含其他jar文件的java可执行jar文件? - How do I configure install4j to run a java executable jar file which contains other jar files? 使用批处理文件运行jar文件 - Using batch file to run a jar file 运行带有参数的 jar 文件的批处理文件 - batch file to run jar file with parameters 如何运行依赖于其他jar文件的ant的jar - How to run a jar from ant which dependes on other jar file 为应用程序创建jar文件,该文件又包含外部jar文件 - creating jar file for application which in turn contains external jar file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM