有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

为什么如果我从NetBeans运行一个java项目,它可以工作,但jar文件却不能工作?

我的问题是:为什么如果我从NetBeans运行一个Java项目,它工作得很好,但是如果我为同一个项目创建一个jar文件,有些函数就不工作了? 特别是,下面的代码应该执行一个bat文件(在项目本身的src文件夹中),并将视频从一个目录移动到另一个目录。 实现这一点的java代码是:

String pathMoveBat = new java.io.File("src\\move.bat").getAbsolutePath();
Process move = Runtime.getRuntime().exec(pathMoveBat+" "+username+" "+dateFormat.format(currentDate)+" "+i+"");

还有文件移动。蝙蝠是

cd C:\Users\%1\Videos\LogitechWebcam
FORFILES /M *.wmv /C "cmd /c move @file C:\Users\%1\Desktop\%2\videos\video%3"

共 (1) 个答案

  1. # 1 楼答案

    这个过程可能会启动,但几乎会立即阻塞,除非您关心它的stderr和stdout流。检查文档

    https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Process.html

    By default, the created process does not have its own terminal or console. All its standard I/O (i.e. 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(). 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.

    另一件事是,src文件夹通常不会添加到zip中,正如其他人所说,即使是这样,您也无法仅从命令行访问其中的内容