Problem with Java program run from Ant

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>
 
B

Berlin Brown

Kamil said:
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");
}

I am pretty sure with your build.xml you cant do Command-Line input
through ant. Ant probably has control over stdin,stdout,stderr, I would
run the program the normal way. I am too lazy to look up your
solution, but I got the problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Problem with Ant 1
ant script, problem with classpath 4
compile with ant 1
problem creating WAR file with ant 4
ant taskdef problem 0
Ant problem with unjar 0
Ant question 1
ant keeps compiling 3

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top