Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to redirect input from file to stdin in Netbeans?

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 
like image 609
denys Avatar asked Mar 13 '12 14:03

denys


4 Answers

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.

like image 153
khmarbaise Avatar answered Nov 17 '22 07:11

khmarbaise


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.

like image 7
ZeroTau Avatar answered Nov 17 '22 09:11

ZeroTau


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.

like image 1
AlexR Avatar answered Nov 17 '22 07:11

AlexR


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

like image 1
Paul Avatar answered Nov 17 '22 08:11

Paul