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.
I need the same as:
$ java MyProgram < data.txt
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.
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 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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With