gpt4 book ai didi

java - 在java中使用cmd命令打印文件

转载 作者:行者123 更新时间:2023-12-02 05:37:55 25 4
gpt4 key购买 nike

我正在尝试从我的 java swing 应用程序打印记事本文件。但是我似乎无法让它工作。

当我在命令提示符中输入此命令时,它会打印给定的记事本文件。 start /min notepad /p C:\score-programma\boem.txt

但是,当我尝试在 java 中执行此操作时,它说找不到该文件。

java.io.IOException: Cannot run program "start": CreateProcess error=2, Het systeem kan het opgegeven bestand niet vinden
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at nl.daalhuisen.model.Model.print(Model.java:184)
at Apl$1$1.actionPerformed(Apl.java:86)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.io.IOException: CreateProcess error=2, Het systeem kan het opgegeven bestand niet vinden
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
... 41 more

这是我运行的java代码:

String[] args = {"start", "/min", "notepad", "/p", "C:\score-programma\boem.txt"};
Runtime.getRuntime().exec(args);

我也尝试过 <br/>
String[] args = {"cmd.exe", "start", "/min", "notepad", "/p", "C:\score-programma\boem.txt"};

这不会产生相同的错误,但什么也没有发生。我在这里缺少什么?

最佳答案

startcmd 的内置命令。它不是可执行文件。你必须运行:

cmd /c start notepad /p c:\etc...

/c 标志是告诉 cmd 您正在尝试运行其他程序所必需的。如果您不传入该值,则 startnotepad 等将被视为 cmd 本身的参数。

关于java - 在java中使用cmd命令打印文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24806928/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com