Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

#ActualShippou

Posted 28 June 2012 - 08:43 AM

I am having trouble with Eclipse creating runnable Jar files.

I made a video about it, trying to cover all the unhelpful " answers " I have found on Google related to this error.


#3Shippou

Posted 28 June 2012 - 04:47 AM

****
Edit
****
I rebooted Eclipse, and it let me compile the executable jars. I have no clue why it didn't let me when I first created the files, but it works now.


Client Jar:
[source lang="java"]package Example_Client;import java.net.*;import java.io.*;import javax.swing.JOptionPane;public class Client { ObjectInputStream Sinput; // to read the socker ObjectOutputStream Soutput; // towrite on the socket Socket socket; // Constructor connection receiving a socket number Client(int port) { // we use "localhost" as host name, the server is on the same machine // but you can put the "real" server name or IP address try { socket = new Socket("localhost", port); } catch(Exception e) { System.out.println("Error connectiong to server:" + e); return; } String St = "Connection accepted " + socket.getInetAddress() + ":" + socket.getPort(); JOptionPane.showMessageDialog(null,St); System.out.println(St); /* Creating both Data Stream */ try { Sinput  = new ObjectInputStream(socket.getInputStream()); Soutput = new ObjectOutputStream(socket.getOutputStream()); } catch (IOException e) { System.out.println("Exception creating new Input/output Streams: " + e); return; } // now that I have my connection String test = "aBcDeFgHiJkLmNoPqRsTuVwXyZ"; String st3 = "Client sending \"" + test + "\" to serveur"; JOptionPane.showMessageDialog(null,st3); // send the string to the server System.out.println(st3); try { Soutput.writeObject(test); Soutput.flush(); } catch(IOException e) { System.out.println("Error writting to the socket: " + e); return; } // read back the answer from the server String response; try { response = (String) Sinput.readObject(); String st4 = "Read back from server: " + response; JOptionPane.showMessageDialog(null,st4); System.out.println(st4); } catch(Exception e) { System.out.println("Problem reading back from server: " + e); } try{ Sinput.close(); Soutput.close(); } catch(Exception e) {} }   public static void main(String[] arg) { new Client(1500); }}[/source]

Server Jar:
[source lang="java"]package Example_Server;//The server code Server.java:import java.io.*;import java.net.*;import javax.swing.JOptionPane;/*** This is to help people to write Client server application*  I tried to make it as simple as possible... the client connect to the server*  the client send a String to the server the server returns it in UPPERCASE thats all*/public class Server {// you must "run" server to have the server run as a console application public static void main(String[] arg) { // start server on port 1500 new Server(1500); } // the socket used by the server private ServerSocket serverSocket; // server constructor Server(int port) { /* create socket server and wait for connection requests */ try { serverSocket = new ServerSocket(port); String st2 = "Server waiting for client on port " + serverSocket.getLocalPort(); JOptionPane.showMessageDialog(null,st2); System.out.println(st2); while(true) { Socket socket = serverSocket.accept();  // accept connection String st1 = " New client asked for a connection"; JOptionPane.showMessageDialog(null,st1); System.out.println(st1); TcpThread t = new TcpThread(socket); // make a thread of it String st3 = "Starting a thread for a new Client"; JOptionPane.showMessageDialog(null,st3); System.out.println(st3); t.start(); } } catch (IOException e) { System.out.println("Exception on new ServerSocket: " + e); } } /** One instance of this thread will run for each client */ class TcpThread extends Thread { // the socket where to listen/talk Socket socket; ObjectInputStream Sinput; ObjectOutputStream Soutput; TcpThread(Socket socket) { this.socket = socket; } public void run() { /* Creating both Data Stream */ String st="Thread trying to create Object Input/Output Streams"; JOptionPane.showMessageDialog(null,st); System.out.println(st); try { // create output first Soutput = new ObjectOutputStream(socket.getOutputStream()); Soutput.flush(); Sinput  = new ObjectInputStream(socket.getInputStream()); } catch (IOException e) { System.out.println("Exception creating new Input/output Streams: " + e); return; } String st4="Thread waiting for a String from the Client"; JOptionPane.showMessageDialog(null,st4); System.out.println(st4); // read a String (which is an object) try { String str = (String) Sinput.readObject(); str = str.toUpperCase(); Soutput.writeObject(str); Soutput.flush(); } catch (IOException e) { System.out.println("Exception reading/writing  Streams: " + e); return; } // will surely not happen with a String catch (ClassNotFoundException o) { } finally { try { Soutput.close(); Sinput.close(); } catch (Exception e) { } } } }}[/source]

#2Shippou

Posted 28 June 2012 - 04:44 AM

****
Edit
****
I rebooted Eclipse, and it let me compile an executable Jar. I have no clue why it didn't let me when I first created the files, but it works now.


Client Jar:
[source lang="java"]package Example_Client;import java.net.*;import java.io.*;import javax.swing.JOptionPane;public class Client { ObjectInputStream Sinput; // to read the socker ObjectOutputStream Soutput; // towrite on the socket Socket socket; // Constructor connection receiving a socket number Client(int port) { // we use "localhost" as host name, the server is on the same machine // but you can put the "real" server name or IP address try { socket = new Socket("localhost", port); } catch(Exception e) { System.out.println("Error connectiong to server:" + e); return; } String St = "Connection accepted " + socket.getInetAddress() + ":" + socket.getPort(); JOptionPane.showMessageDialog(null,St); System.out.println(St); /* Creating both Data Stream */ try { Sinput  = new ObjectInputStream(socket.getInputStream()); Soutput = new ObjectOutputStream(socket.getOutputStream()); } catch (IOException e) { System.out.println("Exception creating new Input/output Streams: " + e); return; } // now that I have my connection String test = "aBcDeFgHiJkLmNoPqRsTuVwXyZ"; String st3 = "Client sending \"" + test + "\" to serveur"; JOptionPane.showMessageDialog(null,st3); // send the string to the server System.out.println(st3); try { Soutput.writeObject(test); Soutput.flush(); } catch(IOException e) { System.out.println("Error writting to the socket: " + e); return; } // read back the answer from the server String response; try { response = (String) Sinput.readObject(); String st4 = "Read back from server: " + response; JOptionPane.showMessageDialog(null,st4); System.out.println(st4); } catch(Exception e) { System.out.println("Problem reading back from server: " + e); } try{ Sinput.close(); Soutput.close(); } catch(Exception e) {} }   public static void main(String[] arg) { new Client(1500); }}[/source]

Server Jar:
[source lang="java"]package Example_Server;//The server code Server.java:import java.io.*;import java.net.*;import javax.swing.JOptionPane;/*** This is to help people to write Client server application*  I tried to make it as simple as possible... the client connect to the server*  the client send a String to the server the server returns it in UPPERCASE thats all*/public class Server {// you must "run" server to have the server run as a console application public static void main(String[] arg) { // start server on port 1500 new Server(1500); } // the socket used by the server private ServerSocket serverSocket; // server constructor Server(int port) { /* create socket server and wait for connection requests */ try { serverSocket = new ServerSocket(port); String st2 = "Server waiting for client on port " + serverSocket.getLocalPort(); JOptionPane.showMessageDialog(null,st2); System.out.println(st2); while(true) { Socket socket = serverSocket.accept();  // accept connection String st1 = " New client asked for a connection"; JOptionPane.showMessageDialog(null,st1); System.out.println(st1); TcpThread t = new TcpThread(socket); // make a thread of it String st3 = "Starting a thread for a new Client"; JOptionPane.showMessageDialog(null,st3); System.out.println(st3); t.start(); } } catch (IOException e) { System.out.println("Exception on new ServerSocket: " + e); } } /** One instance of this thread will run for each client */ class TcpThread extends Thread { // the socket where to listen/talk Socket socket; ObjectInputStream Sinput; ObjectOutputStream Soutput; TcpThread(Socket socket) { this.socket = socket; } public void run() { /* Creating both Data Stream */ String st="Thread trying to create Object Input/Output Streams"; JOptionPane.showMessageDialog(null,st); System.out.println(st); try { // create output first Soutput = new ObjectOutputStream(socket.getOutputStream()); Soutput.flush(); Sinput  = new ObjectInputStream(socket.getInputStream()); } catch (IOException e) { System.out.println("Exception creating new Input/output Streams: " + e); return; } String st4="Thread waiting for a String from the Client"; JOptionPane.showMessageDialog(null,st4); System.out.println(st4); // read a String (which is an object) try { String str = (String) Sinput.readObject(); str = str.toUpperCase(); Soutput.writeObject(str); Soutput.flush(); } catch (IOException e) { System.out.println("Exception reading/writing  Streams: " + e); return; } // will surely not happen with a String catch (ClassNotFoundException o) { } finally { try { Soutput.close(); Sinput.close(); } catch (Exception e) { } } } }}[/source]

#1Shippou

Posted 28 June 2012 - 04:18 AM

I have not had issues with this in the past, but for some reason I am unable to compile these Client / Server examples as executable jars, even though they both have a main method. (( And yes, they are separate java projects ))
( I can run them both from Eclipse )

Client Jar:
[source lang="java"]package Example_Client;import java.net.*;import java.io.*;import javax.swing.JOptionPane;public class Client { ObjectInputStream Sinput; // to read the socker ObjectOutputStream Soutput; // towrite on the socket Socket socket; // Constructor connection receiving a socket number Client(int port) { // we use "localhost" as host name, the server is on the same machine // but you can put the "real" server name or IP address try { socket = new Socket("localhost", port); } catch(Exception e) { System.out.println("Error connectiong to server:" + e); return; } String St = "Connection accepted " + socket.getInetAddress() + ":" + socket.getPort(); JOptionPane.showMessageDialog(null,St);        System.out.println(St); /* Creating both Data Stream */ try { Sinput  = new ObjectInputStream(socket.getInputStream()); Soutput = new ObjectOutputStream(socket.getOutputStream()); } catch (IOException e) { System.out.println("Exception creating new Input/output Streams: " + e); return; } // now that I have my connection String test = "aBcDeFgHiJkLmNoPqRsTuVwXyZ"; String st3 = "Client sending \"" + test + "\" to serveur"; JOptionPane.showMessageDialog(null,st3); // send the string to the server System.out.println(st3); try { Soutput.writeObject(test); Soutput.flush(); } catch(IOException e) { System.out.println("Error writting to the socket: " + e); return; } // read back the answer from the server String response; try { response = (String) Sinput.readObject(); String st4 = "Read back from server: " + response; JOptionPane.showMessageDialog(null,st4); System.out.println(st4); } catch(Exception e) { System.out.println("Problem reading back from server: " + e); } try{ Sinput.close(); Soutput.close(); } catch(Exception e) {} }   public static void main(String[] arg) { new Client(1500); }}[/source]

Server Jar:
[source lang="java"]package Example_Server;//The server code Server.java:import java.io.*;import java.net.*;import javax.swing.JOptionPane;/** * This is to help people to write Client server application *  I tried to make it as simple as possible... the client connect to the server *  the client send a String to the server the server returns it in UPPERCASE thats all */public class Server {// you must "run" server to have the server run as a console application public static void main(String[] arg) { // start server on port 1500 new Server(1500); } // the socket used by the server private ServerSocket serverSocket; // server constructor Server(int port) { /* create socket server and wait for connection requests */ try { serverSocket = new ServerSocket(port); String st2 = "Server waiting for client on port " + serverSocket.getLocalPort(); JOptionPane.showMessageDialog(null,st2); System.out.println(st2); while(true) { Socket socket = serverSocket.accept();  // accept connection String st1 = " New client asked for a connection"; JOptionPane.showMessageDialog(null,st1); System.out.println(st1); TcpThread t = new TcpThread(socket);    // make a thread of it String st3 = "Starting a thread for a new Client"; JOptionPane.showMessageDialog(null,st3); System.out.println(st3); t.start(); } } catch (IOException e) { System.out.println("Exception on new ServerSocket: " + e); } } /** One instance of this thread will run for each client */ class TcpThread extends Thread { // the socket where to listen/talk Socket socket; ObjectInputStream Sinput; ObjectOutputStream Soutput; TcpThread(Socket socket) { this.socket = socket; } public void run() { /* Creating both Data Stream */ String st="Thread trying to create Object Input/Output Streams"; JOptionPane.showMessageDialog(null,st); System.out.println(st); try { // create output first Soutput = new ObjectOutputStream(socket.getOutputStream()); Soutput.flush(); Sinput  = new ObjectInputStream(socket.getInputStream()); } catch (IOException e) { System.out.println("Exception creating new Input/output Streams: " + e); return; } String st4="Thread waiting for a String from the Client"; JOptionPane.showMessageDialog(null,st4); System.out.println(st4); // read a String (which is an object) try { String str = (String) Sinput.readObject(); str = str.toUpperCase(); Soutput.writeObject(str); Soutput.flush(); } catch (IOException e) { System.out.println("Exception reading/writing  Streams: " + e); return; } // will surely not happen with a String catch (ClassNotFoundException o) { } finally { try { Soutput.close(); Sinput.close(); } catch (Exception e) { } } } }}[/source]

PARTNERS