K
Kamil
Hello,
I am trying to run simple EchoClient program on my linux machine that
connects to the port 7 of the host. It works perfectly from the
command line. However when I try to run it from Ant I get
NullPointerException. It seems that Ant has control over input and
output stream. Question is: Is there any way that Ant can hand over
the control over streams to the program that is currently running?
Here are the files I am using
build.xml
<?xml version="1.0" encoding="UTF-8"?>
<project name="MyProject" default="run-client" basedir=".">
<description>
This is my main build.xml file
</description>
<property name="classes" value="${basedir}/classes"/>
<property name="data" value="${basedir}/data"/>
<property name="docs" value="${basedir}/docs"/>
<property name="packages" value="${basedir}/packages"/>
<property name="src" value="${basedir}/src"/>
<property name="version" value="0.95"/>
<target name="init">
<tstamp/>
<mkdir dir="${packages}"/>
<mkdir dir="${classes}"/>
<mkdir dir="${data}"/>
<mkdir dir="${docs}"/>
</target>
<target name="compileClient" depends="init,docs">
<javac srcdir="${src}" destdir="${classes}"
includes="*Client*"
excludes="*Server*"/>
</target>
<target name="docs" depends="init">
<javadoc sourcepath="${src}" destdir="${docs}">
<fileset dir="${src}" defaultexcludes="yes">
<include name="*" />
</fileset>
</javadoc>
</target>
<target name="makeClientJar" depends="compileClient">
<jar jarfile="${packages}/client.${TODAY}.jar"
basedir="${classes}"
includes="**/*Client*"
excludes="**/*Server*">
<manifest>
<attribute name="Class-Path" value="${classes}.src"/>
<attribute name="Main-Class" value="src.EchoClient"/>
<attribute name="Build-By" value="${user.name}"/>
<section name="common">
<attribute name="Specification-Title" value="My
Manifest File"/>
<attribute name="Specification-Version"
value="${version}"/>
<attribute name="Specification-Vendor"
value="Kamil Corp."/>
<attribute name="Implementation-Title"
value="common"/>
<attribute name="Implementation-Version"
value="${version} ${TODAY}"/>
<attribute name="Implementation-Vendor"
value="Kamil Corp."/>
</section>
<section name="common/class1.class">
<attribute name="Sealed" value="false"/>
</section>
</manifest>
</jar>
</target>
<target name="run-client" depends="makeClientJar">
<java classname="src.EchoClient"
classpath="${packages}/client.${TODAY}.jar"
fork="true">
</java>
</target>
</project>
src/EchoClient.java
import java.io.*;
import java.net.*;
import java.util.*;
public class EchoClient {
public static void main(String[] args) throws IOException {
//declare variables used in the program
Socket echoSocket = null;
PrintWriter out = null;
BufferedReader in = null;
String host = "localhost";
System.out.println("Connecting...");
try {
//create Socket object and connect it to port 7 of the
host
echoSocket = new Socket(host, 7);
//create output stream for the created Socket
out = new PrintWriter(echoSocket.getOutputStream(),true);
//create input stream for the created Socket
in = new BufferedReader(new
InputStreamReader(echoSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host: "+ host);
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for "
+ "the connection to: "+ host);
System.exit(1);
}
System.out.println("Connected !");
BufferedReader stdIn = new BufferedReader(new
InputStreamReader(System.in));
String target = "quit";
System.out.println("Please enter phrase (quit ends session):
");
String userInput = stdIn.readLine();
//continue echoing messages until user inputs quit
while (!userInput.equals(target)) {
long timeStarted = System.currentTimeMillis();
out.println(userInput);
System.out.println("echo: " + in.readLine());
long timeFinished =
System.currentTimeMillis()-timeStarted;
System.out.println("It took "+timeFinished+" ms for the
message to come back");
userInput=stdIn.readLine();
}
out.close();
in.close();
stdIn.close();
echoSocket.close();
}
}
Thanks for help.
I am trying to run simple EchoClient program on my linux machine that
connects to the port 7 of the host. It works perfectly from the
command line. However when I try to run it from Ant I get
NullPointerException. It seems that Ant has control over input and
output stream. Question is: Is there any way that Ant can hand over
the control over streams to the program that is currently running?
Here are the files I am using
build.xml
<?xml version="1.0" encoding="UTF-8"?>
<project name="MyProject" default="run-client" basedir=".">
<description>
This is my main build.xml file
</description>
<property name="classes" value="${basedir}/classes"/>
<property name="data" value="${basedir}/data"/>
<property name="docs" value="${basedir}/docs"/>
<property name="packages" value="${basedir}/packages"/>
<property name="src" value="${basedir}/src"/>
<property name="version" value="0.95"/>
<target name="init">
<tstamp/>
<mkdir dir="${packages}"/>
<mkdir dir="${classes}"/>
<mkdir dir="${data}"/>
<mkdir dir="${docs}"/>
</target>
<target name="compileClient" depends="init,docs">
<javac srcdir="${src}" destdir="${classes}"
includes="*Client*"
excludes="*Server*"/>
</target>
<target name="docs" depends="init">
<javadoc sourcepath="${src}" destdir="${docs}">
<fileset dir="${src}" defaultexcludes="yes">
<include name="*" />
</fileset>
</javadoc>
</target>
<target name="makeClientJar" depends="compileClient">
<jar jarfile="${packages}/client.${TODAY}.jar"
basedir="${classes}"
includes="**/*Client*"
excludes="**/*Server*">
<manifest>
<attribute name="Class-Path" value="${classes}.src"/>
<attribute name="Main-Class" value="src.EchoClient"/>
<attribute name="Build-By" value="${user.name}"/>
<section name="common">
<attribute name="Specification-Title" value="My
Manifest File"/>
<attribute name="Specification-Version"
value="${version}"/>
<attribute name="Specification-Vendor"
value="Kamil Corp."/>
<attribute name="Implementation-Title"
value="common"/>
<attribute name="Implementation-Version"
value="${version} ${TODAY}"/>
<attribute name="Implementation-Vendor"
value="Kamil Corp."/>
</section>
<section name="common/class1.class">
<attribute name="Sealed" value="false"/>
</section>
</manifest>
</jar>
</target>
<target name="run-client" depends="makeClientJar">
<java classname="src.EchoClient"
classpath="${packages}/client.${TODAY}.jar"
fork="true">
</java>
</target>
</project>
src/EchoClient.java
import java.io.*;
import java.net.*;
import java.util.*;
public class EchoClient {
public static void main(String[] args) throws IOException {
//declare variables used in the program
Socket echoSocket = null;
PrintWriter out = null;
BufferedReader in = null;
String host = "localhost";
System.out.println("Connecting...");
try {
//create Socket object and connect it to port 7 of the
host
echoSocket = new Socket(host, 7);
//create output stream for the created Socket
out = new PrintWriter(echoSocket.getOutputStream(),true);
//create input stream for the created Socket
in = new BufferedReader(new
InputStreamReader(echoSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host: "+ host);
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for "
+ "the connection to: "+ host);
System.exit(1);
}
System.out.println("Connected !");
BufferedReader stdIn = new BufferedReader(new
InputStreamReader(System.in));
String target = "quit";
System.out.println("Please enter phrase (quit ends session):
");
String userInput = stdIn.readLine();
//continue echoing messages until user inputs quit
while (!userInput.equals(target)) {
long timeStarted = System.currentTimeMillis();
out.println(userInput);
System.out.println("echo: " + in.readLine());
long timeFinished =
System.currentTimeMillis()-timeStarted;
System.out.println("It took "+timeFinished+" ms for the
message to come back");
userInput=stdIn.readLine();
}
out.close();
in.close();
stdIn.close();
echoSocket.close();
}
}
Thanks for help.