Java 有没有办法将输入从文件重定向到 Netbeans 中的 stdin?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9686060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a way to redirect input from file to stdin in Netbeans?
提问by denys
I'm developing application with Netbeans and Maven. My application should obtain data from stdin. But I could not understand how to test it. Putting < data.txt
into args list does not work.
我正在使用 Netbeans 和 Maven 开发应用程序。我的应用程序应该从标准输入获取数据。但我无法理解如何测试它。把< data.txt
进入的args列表不起作用。
I need the same as:
我需要与:
$ java MyProgram < data.txt
采纳答案by ZeroTau
This can be done by adding your own run target to your project's build.xml file. For example:
这可以通过将您自己的运行目标添加到项目的 build.xml 文件中来完成。例如:
<target name="run" depends="jar">
<exec dir="${work.dir}" executable="java" input="${work.dir}/inputfile.txt">
<arg value="-jar"/>
<arg file="${dist.jar}"/>
</exec>
</target>
Note that commands such as Run, Debug, and Test only use your custom build.xml if the Compile on Save feature is turned off for the project. So you will need to ensure that Compile on Save is turned off in your project's properties.
请注意,如果项目的“保存时编译”功能已关闭,则运行、调试和测试等命令仅使用您的自定义 build.xml。因此,您需要确保在项目的属性中关闭“保存时编译”。
回答by khmarbaise
I assume you have a thing like:
我假设你有这样的事情:
public static void main(String[] args) {
...
}
This can used as an entry point to your application and before that you change the input channel via:
这可以用作应用程序的入口点,在此之前,您可以通过以下方式更改输入通道:
FileInputStream is = new FileInputStream(new File("test.data"));
System.setIn(is);
The above can be used within a unit/integration test.
以上可以在单元/集成测试中使用。
回答by AlexR
I am not sure how it is in NetBeans but in eclipse you can write something to console and it is redirected as STDIN to running application. I believe the same should work in NetBeans too. So, just run your application, then copy/paste content of data.txt
to console and probably press <ENTER>
.
我不确定它在 NetBeans 中的情况,但在 eclipse 中,您可以向控制台写入一些内容,并将其作为 STDIN 重定向到正在运行的应用程序。我相信 NetBeans 也应该如此。因此,只需运行您的应用程序,然后将 的内容复制/粘贴data.txt
到控制台并可能按<ENTER>
。
If nothing help use remote debugging, i.e. run your program from command prompt as following:
如果没有任何帮助,请使用远程调试,即从命令提示符运行您的程序,如下所示:
java -Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=y MyProgram < data.txt
java -Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=y MyProgram < data.txt
then connect to this process from NetBeans.
然后从 NetBeans 连接到此进程。
回答by Paul
Old school, but it's what I knew. One caveat is that the mvn command does not return to the cli when done, but for some purposes this is acceptable. Note you need to be in the project root directory
老派,但这是我所知道的。一个警告是 mvn 命令在完成后不会返回到 cli,但出于某些目的,这是可以接受的。注意你需要在项目根目录下
mvn "-Dexec.args=-classpath %classpath com.mycompany.test" -Dexec.executable=/Downloads/jdk1.7/bin/java exec-maven-plugin:1.2.1:exec < /tmp/Input
mvn "-Dexec.args=-classpath %classpath com.mycompany.test" -Dexec.executable=/Downloads/jdk1.7/bin/java exec-maven-plugin:1.2.1:exec < /tmp/Input
回答by Nimar
Add a new target "run-input" under Files->build.xml with the following text. Note: this version uses the correct location of the java runtime. Also, it assumes that you have made a directory called inputs with the file input1.
使用以下文本在 Files->build.xml 下添加一个新目标“run-input”。注意:此版本使用 java 运行时的正确位置。此外,它还假设您已经使用文件 input1 创建了一个名为输入的目录。
<target name="run-input" depends="jar">
<exec dir="${work.dir}" executable="${java.home}/bin/java" input="${src.dir}/inputs/input1">
<arg value="-jar"/>
<arg file="${dist.jar}"/>
</exec>
</target>
You can always create a shortcut for this new target.
您始终可以为此新目标创建快捷方式。