Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Help wiith input/output of jar

1,301 views
Skip to first unread message

bH

unread,
Aug 6, 2009, 4:33:25 PM8/6/09
to
Hi All,
I want to make a program into a jar that makes the input
and output visible when executing the jar.
I have Windows XP.
I have a program that runs successfully,but not in the jar
form, using these lines of code:
.....
BufferedReader userIn = new BufferedReader(
new InputStreamReader(System.in));
out = new PrintWriter(theSocket.getOutputStream());
System.out.println("Connected to echo server");
......
When I use the jar form, execute it, the page on which
to write the input and output are not visible.
Requesting advice, please.
TIA,
bH

John B. Matthews

unread,
Aug 6, 2009, 4:49:37 PM8/6/09
to
In article
<4c1b3fc4-16d8-45e1...@r36g2000vbn.googlegroups.com>,
bH <bher...@hotmail.com> wrote:

Maybe you can use something like this. I haven't tried it on Windows,
but I'd appreciate hearing your results. I think XP has a telnet client,
but PuTTY should work, too:

<code>
package net;

import java.awt.*;
import java.awt.event.*;
import java.io.PrintWriter;
import java.net.*;
import java.util.Scanner;
import javax.swing.*;

public class EchoServer implements ActionListener, Runnable {

private static final int PORT = 12000;
private final JTextField tf = new JTextField(25);;
private final JTextArea ta = new JTextArea(15, 25);;
private final JButton send = new JButton("Send");
private volatile PrintWriter out;
private Scanner in;
private Thread listener;

public EchoServer() {
JFrame f = new JFrame("Echo Server");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getRootPane().setDefaultButton(send);
f.add(tf, BorderLayout.NORTH);
f.add(new JScrollPane(ta), BorderLayout.CENTER);
f.add(send, BorderLayout.SOUTH);
f.setLocation(300, 300);
f.pack();
f.setVisible(true);
send.addActionListener(this);
ta.setLineWrap(true);
ta.setWrapStyleWord(true);
display("Please telnet to port " + PORT);
listener = new Thread(this, "Listener");
}

public void start() {
listener.start();
}

@Override
public void actionPerformed(ActionEvent ae) {
String s = tf.getText();
if (out != null) out.println(s);
display(s);
tf.setText("");
}

@Override
public void run() {
try {
ServerSocket ss = new ServerSocket(PORT);
Socket socket = ss.accept();
in = new Scanner(socket.getInputStream());
out = new PrintWriter(socket.getOutputStream(), true);
while (true) {
display(in.nextLine());
}
} catch (Exception e) {
e.printStackTrace();
}
}

private void display(String s) {
ta.append(s + "\u23CE\n");
ta.setCaretPosition(ta.getDocument().getLength());
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new EchoServer().start();
}
});
}
}
</code>

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>

Roedy Green

unread,
Aug 7, 2009, 11:46:09 PM8/7/09
to
On Thu, 6 Aug 2009 13:33:25 -0700 (PDT), bH <bher...@hotmail.com>
wrote, quoted or indirectly quoted someone who said :

The executable jars are created with tools like jar.exe and ANT
genjar.

See
http://mindprod.com/jgloss/jarexe.html
http://mindprod.com/jgloss/jar.html
http://mindprod.com/jgloss/genjar.html

You can read members with getResource
see http://mindprod.com/jgloss/resource.html
--
Roedy Green Canadian Mind Products
http://mindprod.com

"We must be very careful when we give advice to younger people: sometimes
they follow it!"
~ Edsger Wybe Dijkstra, born: 1930-05-11 died: 2002-08-06 at age: 72

bH

unread,
Aug 10, 2009, 10:31:59 PM8/10/09
to
On Aug 7, 11:46 pm, Roedy Green <see_webs...@mindprod.com.invalid>
wrote:
> On Thu, 6 Aug 2009 13:33:25 -0700 (PDT), bH <bherbs...@hotmail.com>

> wrote, quoted or indirectly quoted someone who said :
>
>
>
>
>
> >Hi All,
> >I want to make a program into a jar that makes the input
> >and output visible when executing the jar.
> >I have Windows XP.
> >I have a program that runs successfully,but not in the jar
> >form, using these lines of code:
> >.....
> >BufferedReader userIn = new BufferedReader(
> >       new InputStreamReader(System.in));
> >      out = new PrintWriter(theSocket.getOutputStream());
> >      System.out.println("Connected to echo server");
> >......
> >When I use the jar form, execute it, the page on which
> >to write the input and output are not visible.
> >Requesting advice, please.
> >TIA,
> >bH
>
> The executable jars are created with tools like jar.exe and ANT
> genjar.
>
> Seehttp://mindprod.com/jgloss/jarexe.htmlhttp://mindprod.com/jgloss/jar.htmlhttp://mindprod.com/jgloss/genjar.html

>
> You can read members with getResource
> seehttp://mindprod.com/jgloss/resource.html
> --
> Roedy Green Canadian Mind Productshttp://mindprod.com

>
> "We must be very careful when we give advice to younger people: sometimes
> they follow it!"
> ~ Edsger Wybe Dijkstra, born: 1930-05-11 died: 2002-08-06 at age: 72- Hide quoted text -
>
> - Show quoted text -

Hi John and Roedy,
Thanks for your responses.
John, I have tried to use your program
as written to no avail. I am not settled on using
threads, especially something like this. Yet, I was
able to use certain parts of your "gift" to modify
programs that I saw elsewhere for each of the
EchoServer and EchoClient. These found elsewhere
had only a public static void main( String [] args ),
and so on and used "System.in..." and "System.out..."
I also have attempted to rework your "gift" above, I am
not there yet. Finally, you wanted
information about telnet and port in a Windows XP.
My internet wanderings suggest that it
is but with specifics to ports, security and
firewalls.
When I finally got the programs to run with frames,
I placed them in jars. And now each can be used
as a click on it to demonstrate EchoServer
and EchoClient.
Roedy, you are one extraordinary person to write
all that you have set out there for me to read. I
admit that on the topic of jars, I really had
no clue about the System.out.println was not
evident when in jar form. That is why I raised the
Question in my posting. But now I
know that a jar can't provide a Terminal
to report when it reads " System.out.println".
As for making a jar, I use BlueJ IDE, which has a
menu option to make a jar.
Thanks again to both of you.
bH

John B. Matthews

unread,
Aug 10, 2009, 11:01:27 PM8/10/09
to
In article
<077030bb-eb70-49b5...@z31g2000yqd.googlegroups.com>,
bH <bher...@hotmail.com> wrote:
[...]

> Finally, you wanted information about telnet and port in
> a Windows XP. My internet wanderings suggest that it is
> but with specifics to ports, security and firewalls.

In the interim, I stumbled across a machine running Vista, and I
thought I'd give it a try. Enabling telnet turned out to be an
obscure, tedious administrative task. Moreover, it proved easier to
comandeer an open port that to finesse the firewall.

> When I finally got the programs to run with frames, I
> placed them in jars. And now each can be used as a click
> on it to demonstrate EchoServer and EchoClient.

Capital idea! You probably finished before me. :-)

[...]

bH

unread,
Aug 12, 2009, 6:20:14 PM8/12/09
to
On Aug 10, 11:01 pm, "John B. Matthews" <nos...@nospam.invalid> wrote:
> In article

Hi John,
The following is my effort to revise your code to
my EchoServer. I do not understand how to write a
thread in a similar manner that you did. Yet I did
use some of lines that you have written.
It is necessary in this situation to have the
EchoServer to be running when the EchoClient
is started.
Thanks again for your efforts.
bH

import java.awt.*;
import java.io.PrintWriter;
import java.net.*;
import javax.swing.*;
import java.nio.*;
import java.nio.channels.*;
import java.util.*;
import java.io.IOException;

// This is a hands off server:
// I left out any refernce to a button
// I didn't need it, in my estimation.

public class EchoServerJMv1 implements Runnable {
private static final int PORT = 4444;


private final JTextField tf = new JTextField(25);;
private final JTextArea ta = new JTextArea(15, 25);;

private volatile PrintWriter out;
private Scanner in;
private Thread listener;

public static int DEFAULT_PORT = 4444;
public static int port = DEFAULT_PORT;

public EchoServerJMv1() {


JFrame f = new JFrame("Echo Server");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.add(new JScrollPane(ta), BorderLayout.CENTER);
f.setLocation(300, 300);
f.pack();
f.setVisible(true);
ta.setLineWrap(true);
ta.setWrapStyleWord(true);
display("Using LocalHost and port " + PORT);
display("Listening for connections");


listener = new Thread(this, "Listener");
}

public void start() {
display("Came thru 'start()' to just print a 'hello'");
listener.start();
}

@Override
public void run() {
ServerSocketChannel serverChannel;
Selector selector;
try {
serverChannel = ServerSocketChannel.open();
ServerSocket ss = serverChannel.socket();
InetSocketAddress address = new InetSocketAddress(port);
ss.bind(address);
serverChannel.configureBlocking(false);
selector = Selector.open();
serverChannel.register(selector, SelectionKey.OP_ACCEPT);
}
catch (IOException ex) {
ex.printStackTrace();
return;
}

while (true) {
try {
selector.select();
}
catch (IOException ex) {
ex.printStackTrace();
break;
}

Set readyKeys = selector.selectedKeys();
Iterator iterator = readyKeys.iterator();
while (iterator.hasNext()) {

SelectionKey key = (SelectionKey) iterator.next();
iterator.remove();
try {
if (key.isAcceptable()) {
ServerSocketChannel server =
(ServerSocketChannel ) key.channel();
SocketChannel client = server.accept();
display("Accepted connection from " + client);
client.configureBlocking(false);
SelectionKey clientKey = client.register(selector,
SelectionKey.OP_WRITE | SelectionKey.OP_READ);
ByteBuffer buffer = ByteBuffer.allocate(100);
clientKey.attach(buffer);
}
if (key.isReadable()) {
SocketChannel client = (SocketChannel) key.channel();
ByteBuffer output = (ByteBuffer) key.attachment();
client.read(output);
}
if (key.isWritable()) {
SocketChannel client =
(SocketChannel) key.channel();
ByteBuffer output = (ByteBuffer) key.attachment();
output.flip();
client.write(output);
output.compact();
}
}
catch (IOException ex) {
key.cancel();
try {
key.channel().close();
}
catch (IOException cex) {}
}
}
}
}

private void display(String s) {
ta.append(s + "\u23CE\n");
ta.setCaretPosition(ta.getDocument().getLength());
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {

new EchoServerJMv1().start();
}
});
}
}

bH

unread,
Aug 12, 2009, 6:23:21 PM8/12/09
to
> }- Hide quoted text -
>
> - Show quoted text -- Hide quoted text -

>
> - Show quoted text -

Hi John,


The following is my effort to revise your code to

my EchoClient. I do not understand how to write a


thread in a similar manner that you did. Yet I did
use some of lines that you have written.
It is necessary in this situation to have the
EchoServer to be running when the EchoClient
is started.
Thanks again for your efforts.
bH

import java.awt.*;
import java.awt.event.*;


import java.io.PrintWriter;
import java.net.*;
import javax.swing.*;

import java.io.*;
import javax.swing.*;
public class EchoClientJMv1 implements ActionListener,
Runnable {

private static final int PORT = 4444;
private final JTextField tf = new JTextField(25);;
private final JTextArea ta = new JTextArea(15, 25);;

private final JButton send = new JButton("Send");

private volatile PrintWriter out;
private Thread listener;
private String s = "";


public EchoClientJMv1() {


JFrame f = new JFrame("Echo Server");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.getRootPane().setDefaultButton(send);
f.add(tf, BorderLayout.NORTH);
f.add(new JScrollPane(ta), BorderLayout.CENTER);
f.add(send, BorderLayout.SOUTH);
f.setLocation(300, 300);
f.pack();
f.setVisible(true);
send.addActionListener(this);
ta.setLineWrap(true);
ta.setWrapStyleWord(true);

display("Please use LocalHost to port " + PORT);


listener = new Thread(this, "Listener");
}


public void start() {
display("Came thru 'start()' to just print a 'hello'");
listener.start();
}

@Override
public void actionPerformed(ActionEvent ae) {
try {
String host = "localhost";
Socket t = new Socket(host, PORT);

BufferedReader in
= new BufferedReader(new
InputStreamReader(t.getInputStream()));
PrintWriter out
= new PrintWriter(new
OutputStreamWriter(t.getOutputStream()));

String s = tf.getText();

display("Sent: " + s);
out.println(s); //to server
out.flush();

tf.setText("");// clear the input tf

String str = in.readLine();
if (str != null) {
display("Received: "+ str + " \n"); // from server
}
} catch (Exception e) {
display("Error: " + e);
}
}


@Override
public void run() {
display("Came thru 'run()' to just print a 'hello'");
}


private void display(String s) {
ta.append(s + "\u23CE\n");
ta.setCaretPosition(ta.getDocument().getLength());
}


public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {

new EchoClientJMv1().start();
}
});
}
}

John B. Matthews

unread,
Aug 12, 2009, 6:38:36 PM8/12/09
to
In article
<2424f942-de6f-4ad6...@k19g2000yqn.googlegroups.com>,
bH <bher...@hotmail.com> wrote:

> On Aug 10, 11:01 pm, "John B. Matthews" <nos...@nospam.invalid> wrote:
> > In article
> > <077030bb-eb70-49b5-a33f-905904ed0...@z31g2000yqd.googlegroups.com>, bH
> > <bherbs...@hotmail.com> wrote:
> >
> > [...]
> >
> > > Finally, you wanted information about telnet and port in a
> > > Windows XP. My internet wanderings suggest that it is but with
> > > specifics to ports, security and firewalls.
> >
> > In the interim, I stumbled across a machine running Vista, and I
> > thought I'd give it a try. Enabling telnet turned out to be an
> > obscure, tedious administrative task. Moreover, it proved easier to

> > com[m]andeer an open port that to finesse the firewall.


> >
> > > When I finally got the programs to run with frames, I placed them
> > > in jars. And now each can be used as a click on it to demonstrate
> > > EchoServer and EchoClient.
> >
> > Capital idea! You probably finished before me. :-)
> >
> > [...]
>

> Hi John,
> The following is my effort to revise your code to
> my EchoServer. I do not understand how to write a
> thread in a similar manner that you did. Yet I did
> use some of lines that you have written.
> It is necessary in this situation to have the
> EchoServer to be running when the EchoClient
> is started.
> Thanks again for your efforts.

It looks like your EchoClient is blocking the EDT in actionPerformed().
As client and server are largely symmetric, here's an example that does
both:

<code>
package net;

import java.awt.*;
import java.awt.event.*;
import java.io.*;


import java.net.*;
import java.util.Scanner;
import javax.swing.*;

/**
* A simple network client-server pair
* @author John B. Matthews
*/
public class Echo implements ActionListener, Runnable {

private static final String HOST = "127.0.0.1";
private static final int PORT = 12345;


private final JTextField tf = new JTextField(25);
private final JTextArea ta = new JTextArea(15, 25);

private final JButton send = new JButton("Send");

private volatile PrintWriter out;
private Scanner in;

private Thread client;
private Kind kind;

public static enum Kind {
Client(100, "Trying"), Server(500, "Awaiting");
private int offset;
private String activity;
private Kind(int offset, String activity) {
this.offset = offset;
this.activity = activity;
}
}

public Echo(Kind kind) {
this.kind = kind;
JFrame f = new JFrame("Echo " + kind);


f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getRootPane().setDefaultButton(send);
f.add(tf, BorderLayout.NORTH);
f.add(new JScrollPane(ta), BorderLayout.CENTER);
f.add(send, BorderLayout.SOUTH);

f.setLocation(kind.offset, 300);


f.pack();
f.setVisible(true);
send.addActionListener(this);
ta.setLineWrap(true);
ta.setWrapStyleWord(true);

display(kind.activity + HOST + " on port " + PORT);
client = new Thread(this, "Client");
}

public void start() {
client.start();
}

@Override
public void actionPerformed(ActionEvent ae) {

String s = tf.getText();
if (out != null) {
out.println(s);
}
display(s);
tf.setText("");
}

@Override
public void run() {
try {

Socket socket;
if (kind == Kind.Client) {
socket = new Socket(HOST, PORT);
} else {


ServerSocket ss = new ServerSocket(PORT);

socket = ss.accept();
}
in = new Scanner(socket.getInputStream());
out = new PrintWriter(socket.getOutputStream(), true);

display("Connected");


while (true) {
display(in.nextLine());
}
} catch (Exception e) {

display(e.getMessage());
e.printStackTrace();
}
}

private void display(String s) {
ta.append(s + "\u23CE\n");
ta.setCaretPosition(ta.getDocument().getLength());
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {

new Echo(Kind.Server).start();
new Echo(Kind.Client).start();
}
});
}
}
</code>

bH

unread,
Aug 12, 2009, 11:08:36 PM8/12/09
to
On Aug 12, 6:38 pm, "John B. Matthews" <nos...@nospam.invalid> wrote:
> In article
> <2424f942-de6f-4ad6-a155-25fd5c158...@k19g2000yqn.googlegroups.com>,
> <http://sites.google.com/site/drjohnbmatthews>- Hide quoted text -

>
> - Show quoted text -

Hi John,
I read this "info/discussion" at:
<http://java.sun.com/docs/books/tutorial/networking/sockets/
readingWriting.html>
However, I have difficulty aligning your gift
as the "info/discussion" suggests. Your Echo will also
switch roles: what the server does and what the client
does, in spite of the difference of headers :JFrame("Echo " + kind);
In practice this would not be so, would it? Normally the
server is not pressing a button "send" anything and
not getting a response to what it sent. But I am
not a guru on that topic by any means.
I think that I have moved this discussion too
far off original target.
Thanks for your efforts.

bH

John B. Matthews

unread,
Aug 12, 2009, 11:56:59 PM8/12/09
to
In article
<0f94c002-35d2-4e00...@t13g2000yqt.googlegroups.com>,
bH <bher...@hotmail.com> wrote:

[...]


> Hi John,
> I read this "info/discussion" at:
> <http://java.sun.com/docs/books/tutorial/networking/sockets/
readingWriting.html>
> However, I have difficulty aligning your gift
> as the "info/discussion" suggests. Your Echo will also
> switch roles: what the server does and what the client
> does, in spite of the difference of headers :JFrame("Echo " + kind);
> In practice this would not be so, would it? Normally the
> server is not pressing a button "send" anything and
> not getting a response to what it sent. But I am
> not a guru on that topic by any means.
> I think that I have moved this discussion too
> far off original target.
> Thanks for your efforts.

The big difference is that the tutorial displays results on the console,
which is synchronized as a convenience. In contrast, my example is
organized to display results in a GUI widget without blocking the EDT.
Of course, both are mere demonstrations, and both accept only a single
client connection.

As an exercise, the tutorial suggests extending the server to accept
multiple client connections. Goetz, et al. examine this problem in
chapter 6 of _Java Concurrency in Practice_:

<http://www.javaconcurrencyinpractice.com/>

bH

unread,
Aug 13, 2009, 12:19:33 AM8/13/09
to
On Aug 12, 11:56 pm, "John B. Matthews" <nos...@nospam.invalid> wrote:
> In article
> <0f94c002-35d2-4e00-a18c-b0e83c60b...@t13g2000yqt.googlegroups.com>,

Thanks again,
bH

0 new messages