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
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>
.
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
then connect to this process from NetBeans.
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.
<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.
This can be done by adding your own run target to your project's build.xml file. For example:
<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.
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.
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 "-Dexec.args=-classpath %classpath com.mycompany.test" -Dexec.executable=/Downloads/jdk1.7/bin/java exec-maven-plugin:1.2.1:exec < /tmp/Input