K
Kamil Muzylo
Hello,
I am trying to run a simple echo server on my linux machine using Ant.
I opened port 7 and the program works fine from command line. When I try to
run it using Ant program throws NullPointerException. I looked at Java and
Ant manuals with no results. I am new to Java and Ant and I am probalby
missing some very small detail. I would be greatful for any tips.
km
EchoClient.java (This file is located in ../src directory):
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): ");
//continue echoing messages until user inputs quit
while (!(userInput=stdIn.readLine()).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");
}
out.close();
in.close();
stdIn.close();
echoSocket.close();
}
}
build.xml looks like this:
<?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>
I am trying to run a simple echo server on my linux machine using Ant.
I opened port 7 and the program works fine from command line. When I try to
run it using Ant program throws NullPointerException. I looked at Java and
Ant manuals with no results. I am new to Java and Ant and I am probalby
missing some very small detail. I would be greatful for any tips.
km
EchoClient.java (This file is located in ../src directory):
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): ");
//continue echoing messages until user inputs quit
while (!(userInput=stdIn.readLine()).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");
}
out.close();
in.close();
stdIn.close();
echoSocket.close();
}
}
build.xml looks like this:
<?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>