Unable to run class in a separate thread/window











up vote
1
down vote

favorite












I am trying to run a multi-user chat client java programme as part of another java programme.



How do I implement it in such a way that I can open up the chat client from the main programme? I have attempted to start it using ProcessBuilder but it causes the whole programme to crash.



The class to start the chat client and the client client itself is shown below respectively



--------------------- Class to start chat client ---------------------



import java.io.File;
import java.io.IOException;
import java.io.InputStream;

public class ChatCommand extends Command {

public static final String COMMAND_WORD = "chat";
public static final String MESSAGE_USAGE = COMMAND_WORD + ":n" + "Opens up a separate chat programment"
+ "Example: " + COMMAND_WORD;

public static final String MESSAGE_SUCCESS = "Initialising chat!";

public static void main(String args) {
new ChatCommand();
}

public ChatCommand() {
try {
int result = compile("seedu.addressbook.communications.ChatClient");
System.out.println("javac returned " + result);
result = run("seedu.addressbook.communications.ChatClient");
} catch (IOException | InterruptedException ex) {
ex.printStackTrace();
}
}

public int run(String clazz) throws IOException, InterruptedException {
ProcessBuilder pb = new ProcessBuilder("java", clazz);
pb.redirectError();
pb.directory(new File("src"));
Process p = pb.start();
InputStreamConsumer consumer = new InputStreamConsumer(p.getInputStream());
consumer.start();

int result = p.waitFor();

consumer.join();

System.out.println(consumer.getOutput());

return result;
}

public int compile(String file) throws IOException, InterruptedException {
ProcessBuilder pb = new ProcessBuilder("javac", file);
pb.redirectError();
pb.directory(new File("src"));
Process p = pb.start();
InputStreamConsumer consumer = new InputStreamConsumer(p.getInputStream());
consumer.start();

int result = p.waitFor();

consumer.join();

System.out.println(consumer.getOutput());

return result;
}

public class InputStreamConsumer extends Thread {

private InputStream is;
private IOException exp;
private StringBuilder output;

public InputStreamConsumer(InputStream is) {
this.is = is;
}

@Override
public void run() {
int in = -1;
output = new StringBuilder(64);
try {
while ((in = is.read()) != -1) {
output.append((char) in);
}
} catch (IOException ex) {
ex.printStackTrace();
exp = ex;
}
}

public StringBuilder getOutput() {
return output;
}

public IOException getException() {
return exp;
}
}

public CommandResult execute() {
ChatClient cc = new ChatClient();
try {
cc.main(new String{"a", "b"});
} catch (Exception e) {
System.out.println("aaa");
}
commandHistory.addHistory(COMMAND_WORD);
return new CommandResult(MESSAGE_SUCCESS);
}
}


--------------------------- Chat Client --------------------------



import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/* A simple Swing-based client for the chat server. Graphically
* it is a frame with a text field for entering messages and a
* textarea to see the whole dialog.
*
* The client follows the Chat Protocol which is as follows.
* When the server sends "SUBMITNAME" the client replies with the
* desired screen name. The server will keep sending "SUBMITNAME"
* requests as long as the client submits screen names that are
* already in use. When the server sends a line beginning
* with "NAMEACCEPTED" the client is now allowed to start
* sending the server arbitrary strings to be broadcast to all
* chatters connected to the server. When the server sends a
* line beginning with "MESSAGE " then all characters following
* this string should be displayed in its message area.
*/

public class ChatClient {

private BufferedReader in;
private PrintWriter out;
private JFrame frame = new JFrame("MediChat");
private JTextField textField = new JTextField(40);
private JTextArea messageArea = new JTextArea(8, 40);

/* Constructs the client by laying out the GUI and registering a
* listener with the textfield so that pressing Return in the
* listener sends the textfield contents to the server. Note
* however that the textfield is initially NOT editable, and
* only becomes editable AFTER the client receives the NAMEACCEPTED
* message from the server.
*/
public ChatClient() {

// Layout GUI
textField.setEditable(false);
messageArea.setEditable(false);
frame.getContentPane().add(textField, "North");
frame.getContentPane().add(new JScrollPane(messageArea), "Center");
frame.pack();

// Add Listeners
textField.addActionListener(new ActionListener() {
/* Responds to pressing the enter key in the textfield by sending
* the contents of the text field to the server. Then clear
* the text area in preparation for the next message.
*/
public void actionPerformed(ActionEvent e) {
out.println(textField.getText());
textField.setText("");
}
});
}

/* Prompt for and return the address of the server.
*/
private String getServerAddress() {
return JOptionPane.showInputDialog(
frame,
"Enter IP Address of the Server:",
"Welcome to MediChat!",
JOptionPane.QUESTION_MESSAGE);
}

/* Prompt for and return the desired screen name.
*/
private String getName() {
return JOptionPane.showInputDialog(
frame,
"Choose a screen name:",
"Screen name selection",
JOptionPane.PLAIN_MESSAGE);
}

/* Connects to the server then enters the processing loop.
*/
private void run() throws IOException {

// Make connection and initialize streams
String serverAddress = getServerAddress();
Socket socket = new Socket(serverAddress, 9001);
in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);

// Process all messages from server, according to the protocol.
while (true) {
String line = in.readLine();
if (line.startsWith("SUBMITNAME")) {
out.println(getName());
} else if (line.startsWith("NAMEACCEPTED")) {
textField.setEditable(true);
} else if (line.startsWith("MESSAGE")) {
messageArea.append(line.substring(8) + "n");
}
}
}

/**
* Runs the client as an application with a closeable frame.
*/
public static void main(String args) throws Exception {
ChatClient client = new ChatClient();
client.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
client.frame.setVisible(true);
client.run();
}
}









share|improve this question







New contributor




K. Tan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • I appreciate the quick accept!
    – GhostCat
    yesterday















up vote
1
down vote

favorite












I am trying to run a multi-user chat client java programme as part of another java programme.



How do I implement it in such a way that I can open up the chat client from the main programme? I have attempted to start it using ProcessBuilder but it causes the whole programme to crash.



The class to start the chat client and the client client itself is shown below respectively



--------------------- Class to start chat client ---------------------



import java.io.File;
import java.io.IOException;
import java.io.InputStream;

public class ChatCommand extends Command {

public static final String COMMAND_WORD = "chat";
public static final String MESSAGE_USAGE = COMMAND_WORD + ":n" + "Opens up a separate chat programment"
+ "Example: " + COMMAND_WORD;

public static final String MESSAGE_SUCCESS = "Initialising chat!";

public static void main(String args) {
new ChatCommand();
}

public ChatCommand() {
try {
int result = compile("seedu.addressbook.communications.ChatClient");
System.out.println("javac returned " + result);
result = run("seedu.addressbook.communications.ChatClient");
} catch (IOException | InterruptedException ex) {
ex.printStackTrace();
}
}

public int run(String clazz) throws IOException, InterruptedException {
ProcessBuilder pb = new ProcessBuilder("java", clazz);
pb.redirectError();
pb.directory(new File("src"));
Process p = pb.start();
InputStreamConsumer consumer = new InputStreamConsumer(p.getInputStream());
consumer.start();

int result = p.waitFor();

consumer.join();

System.out.println(consumer.getOutput());

return result;
}

public int compile(String file) throws IOException, InterruptedException {
ProcessBuilder pb = new ProcessBuilder("javac", file);
pb.redirectError();
pb.directory(new File("src"));
Process p = pb.start();
InputStreamConsumer consumer = new InputStreamConsumer(p.getInputStream());
consumer.start();

int result = p.waitFor();

consumer.join();

System.out.println(consumer.getOutput());

return result;
}

public class InputStreamConsumer extends Thread {

private InputStream is;
private IOException exp;
private StringBuilder output;

public InputStreamConsumer(InputStream is) {
this.is = is;
}

@Override
public void run() {
int in = -1;
output = new StringBuilder(64);
try {
while ((in = is.read()) != -1) {
output.append((char) in);
}
} catch (IOException ex) {
ex.printStackTrace();
exp = ex;
}
}

public StringBuilder getOutput() {
return output;
}

public IOException getException() {
return exp;
}
}

public CommandResult execute() {
ChatClient cc = new ChatClient();
try {
cc.main(new String{"a", "b"});
} catch (Exception e) {
System.out.println("aaa");
}
commandHistory.addHistory(COMMAND_WORD);
return new CommandResult(MESSAGE_SUCCESS);
}
}


--------------------------- Chat Client --------------------------



import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/* A simple Swing-based client for the chat server. Graphically
* it is a frame with a text field for entering messages and a
* textarea to see the whole dialog.
*
* The client follows the Chat Protocol which is as follows.
* When the server sends "SUBMITNAME" the client replies with the
* desired screen name. The server will keep sending "SUBMITNAME"
* requests as long as the client submits screen names that are
* already in use. When the server sends a line beginning
* with "NAMEACCEPTED" the client is now allowed to start
* sending the server arbitrary strings to be broadcast to all
* chatters connected to the server. When the server sends a
* line beginning with "MESSAGE " then all characters following
* this string should be displayed in its message area.
*/

public class ChatClient {

private BufferedReader in;
private PrintWriter out;
private JFrame frame = new JFrame("MediChat");
private JTextField textField = new JTextField(40);
private JTextArea messageArea = new JTextArea(8, 40);

/* Constructs the client by laying out the GUI and registering a
* listener with the textfield so that pressing Return in the
* listener sends the textfield contents to the server. Note
* however that the textfield is initially NOT editable, and
* only becomes editable AFTER the client receives the NAMEACCEPTED
* message from the server.
*/
public ChatClient() {

// Layout GUI
textField.setEditable(false);
messageArea.setEditable(false);
frame.getContentPane().add(textField, "North");
frame.getContentPane().add(new JScrollPane(messageArea), "Center");
frame.pack();

// Add Listeners
textField.addActionListener(new ActionListener() {
/* Responds to pressing the enter key in the textfield by sending
* the contents of the text field to the server. Then clear
* the text area in preparation for the next message.
*/
public void actionPerformed(ActionEvent e) {
out.println(textField.getText());
textField.setText("");
}
});
}

/* Prompt for and return the address of the server.
*/
private String getServerAddress() {
return JOptionPane.showInputDialog(
frame,
"Enter IP Address of the Server:",
"Welcome to MediChat!",
JOptionPane.QUESTION_MESSAGE);
}

/* Prompt for and return the desired screen name.
*/
private String getName() {
return JOptionPane.showInputDialog(
frame,
"Choose a screen name:",
"Screen name selection",
JOptionPane.PLAIN_MESSAGE);
}

/* Connects to the server then enters the processing loop.
*/
private void run() throws IOException {

// Make connection and initialize streams
String serverAddress = getServerAddress();
Socket socket = new Socket(serverAddress, 9001);
in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);

// Process all messages from server, according to the protocol.
while (true) {
String line = in.readLine();
if (line.startsWith("SUBMITNAME")) {
out.println(getName());
} else if (line.startsWith("NAMEACCEPTED")) {
textField.setEditable(true);
} else if (line.startsWith("MESSAGE")) {
messageArea.append(line.substring(8) + "n");
}
}
}

/**
* Runs the client as an application with a closeable frame.
*/
public static void main(String args) throws Exception {
ChatClient client = new ChatClient();
client.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
client.frame.setVisible(true);
client.run();
}
}









share|improve this question







New contributor




K. Tan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • I appreciate the quick accept!
    – GhostCat
    yesterday













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I am trying to run a multi-user chat client java programme as part of another java programme.



How do I implement it in such a way that I can open up the chat client from the main programme? I have attempted to start it using ProcessBuilder but it causes the whole programme to crash.



The class to start the chat client and the client client itself is shown below respectively



--------------------- Class to start chat client ---------------------



import java.io.File;
import java.io.IOException;
import java.io.InputStream;

public class ChatCommand extends Command {

public static final String COMMAND_WORD = "chat";
public static final String MESSAGE_USAGE = COMMAND_WORD + ":n" + "Opens up a separate chat programment"
+ "Example: " + COMMAND_WORD;

public static final String MESSAGE_SUCCESS = "Initialising chat!";

public static void main(String args) {
new ChatCommand();
}

public ChatCommand() {
try {
int result = compile("seedu.addressbook.communications.ChatClient");
System.out.println("javac returned " + result);
result = run("seedu.addressbook.communications.ChatClient");
} catch (IOException | InterruptedException ex) {
ex.printStackTrace();
}
}

public int run(String clazz) throws IOException, InterruptedException {
ProcessBuilder pb = new ProcessBuilder("java", clazz);
pb.redirectError();
pb.directory(new File("src"));
Process p = pb.start();
InputStreamConsumer consumer = new InputStreamConsumer(p.getInputStream());
consumer.start();

int result = p.waitFor();

consumer.join();

System.out.println(consumer.getOutput());

return result;
}

public int compile(String file) throws IOException, InterruptedException {
ProcessBuilder pb = new ProcessBuilder("javac", file);
pb.redirectError();
pb.directory(new File("src"));
Process p = pb.start();
InputStreamConsumer consumer = new InputStreamConsumer(p.getInputStream());
consumer.start();

int result = p.waitFor();

consumer.join();

System.out.println(consumer.getOutput());

return result;
}

public class InputStreamConsumer extends Thread {

private InputStream is;
private IOException exp;
private StringBuilder output;

public InputStreamConsumer(InputStream is) {
this.is = is;
}

@Override
public void run() {
int in = -1;
output = new StringBuilder(64);
try {
while ((in = is.read()) != -1) {
output.append((char) in);
}
} catch (IOException ex) {
ex.printStackTrace();
exp = ex;
}
}

public StringBuilder getOutput() {
return output;
}

public IOException getException() {
return exp;
}
}

public CommandResult execute() {
ChatClient cc = new ChatClient();
try {
cc.main(new String{"a", "b"});
} catch (Exception e) {
System.out.println("aaa");
}
commandHistory.addHistory(COMMAND_WORD);
return new CommandResult(MESSAGE_SUCCESS);
}
}


--------------------------- Chat Client --------------------------



import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/* A simple Swing-based client for the chat server. Graphically
* it is a frame with a text field for entering messages and a
* textarea to see the whole dialog.
*
* The client follows the Chat Protocol which is as follows.
* When the server sends "SUBMITNAME" the client replies with the
* desired screen name. The server will keep sending "SUBMITNAME"
* requests as long as the client submits screen names that are
* already in use. When the server sends a line beginning
* with "NAMEACCEPTED" the client is now allowed to start
* sending the server arbitrary strings to be broadcast to all
* chatters connected to the server. When the server sends a
* line beginning with "MESSAGE " then all characters following
* this string should be displayed in its message area.
*/

public class ChatClient {

private BufferedReader in;
private PrintWriter out;
private JFrame frame = new JFrame("MediChat");
private JTextField textField = new JTextField(40);
private JTextArea messageArea = new JTextArea(8, 40);

/* Constructs the client by laying out the GUI and registering a
* listener with the textfield so that pressing Return in the
* listener sends the textfield contents to the server. Note
* however that the textfield is initially NOT editable, and
* only becomes editable AFTER the client receives the NAMEACCEPTED
* message from the server.
*/
public ChatClient() {

// Layout GUI
textField.setEditable(false);
messageArea.setEditable(false);
frame.getContentPane().add(textField, "North");
frame.getContentPane().add(new JScrollPane(messageArea), "Center");
frame.pack();

// Add Listeners
textField.addActionListener(new ActionListener() {
/* Responds to pressing the enter key in the textfield by sending
* the contents of the text field to the server. Then clear
* the text area in preparation for the next message.
*/
public void actionPerformed(ActionEvent e) {
out.println(textField.getText());
textField.setText("");
}
});
}

/* Prompt for and return the address of the server.
*/
private String getServerAddress() {
return JOptionPane.showInputDialog(
frame,
"Enter IP Address of the Server:",
"Welcome to MediChat!",
JOptionPane.QUESTION_MESSAGE);
}

/* Prompt for and return the desired screen name.
*/
private String getName() {
return JOptionPane.showInputDialog(
frame,
"Choose a screen name:",
"Screen name selection",
JOptionPane.PLAIN_MESSAGE);
}

/* Connects to the server then enters the processing loop.
*/
private void run() throws IOException {

// Make connection and initialize streams
String serverAddress = getServerAddress();
Socket socket = new Socket(serverAddress, 9001);
in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);

// Process all messages from server, according to the protocol.
while (true) {
String line = in.readLine();
if (line.startsWith("SUBMITNAME")) {
out.println(getName());
} else if (line.startsWith("NAMEACCEPTED")) {
textField.setEditable(true);
} else if (line.startsWith("MESSAGE")) {
messageArea.append(line.substring(8) + "n");
}
}
}

/**
* Runs the client as an application with a closeable frame.
*/
public static void main(String args) throws Exception {
ChatClient client = new ChatClient();
client.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
client.frame.setVisible(true);
client.run();
}
}









share|improve this question







New contributor




K. Tan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I am trying to run a multi-user chat client java programme as part of another java programme.



How do I implement it in such a way that I can open up the chat client from the main programme? I have attempted to start it using ProcessBuilder but it causes the whole programme to crash.



The class to start the chat client and the client client itself is shown below respectively



--------------------- Class to start chat client ---------------------



import java.io.File;
import java.io.IOException;
import java.io.InputStream;

public class ChatCommand extends Command {

public static final String COMMAND_WORD = "chat";
public static final String MESSAGE_USAGE = COMMAND_WORD + ":n" + "Opens up a separate chat programment"
+ "Example: " + COMMAND_WORD;

public static final String MESSAGE_SUCCESS = "Initialising chat!";

public static void main(String args) {
new ChatCommand();
}

public ChatCommand() {
try {
int result = compile("seedu.addressbook.communications.ChatClient");
System.out.println("javac returned " + result);
result = run("seedu.addressbook.communications.ChatClient");
} catch (IOException | InterruptedException ex) {
ex.printStackTrace();
}
}

public int run(String clazz) throws IOException, InterruptedException {
ProcessBuilder pb = new ProcessBuilder("java", clazz);
pb.redirectError();
pb.directory(new File("src"));
Process p = pb.start();
InputStreamConsumer consumer = new InputStreamConsumer(p.getInputStream());
consumer.start();

int result = p.waitFor();

consumer.join();

System.out.println(consumer.getOutput());

return result;
}

public int compile(String file) throws IOException, InterruptedException {
ProcessBuilder pb = new ProcessBuilder("javac", file);
pb.redirectError();
pb.directory(new File("src"));
Process p = pb.start();
InputStreamConsumer consumer = new InputStreamConsumer(p.getInputStream());
consumer.start();

int result = p.waitFor();

consumer.join();

System.out.println(consumer.getOutput());

return result;
}

public class InputStreamConsumer extends Thread {

private InputStream is;
private IOException exp;
private StringBuilder output;

public InputStreamConsumer(InputStream is) {
this.is = is;
}

@Override
public void run() {
int in = -1;
output = new StringBuilder(64);
try {
while ((in = is.read()) != -1) {
output.append((char) in);
}
} catch (IOException ex) {
ex.printStackTrace();
exp = ex;
}
}

public StringBuilder getOutput() {
return output;
}

public IOException getException() {
return exp;
}
}

public CommandResult execute() {
ChatClient cc = new ChatClient();
try {
cc.main(new String{"a", "b"});
} catch (Exception e) {
System.out.println("aaa");
}
commandHistory.addHistory(COMMAND_WORD);
return new CommandResult(MESSAGE_SUCCESS);
}
}


--------------------------- Chat Client --------------------------



import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/* A simple Swing-based client for the chat server. Graphically
* it is a frame with a text field for entering messages and a
* textarea to see the whole dialog.
*
* The client follows the Chat Protocol which is as follows.
* When the server sends "SUBMITNAME" the client replies with the
* desired screen name. The server will keep sending "SUBMITNAME"
* requests as long as the client submits screen names that are
* already in use. When the server sends a line beginning
* with "NAMEACCEPTED" the client is now allowed to start
* sending the server arbitrary strings to be broadcast to all
* chatters connected to the server. When the server sends a
* line beginning with "MESSAGE " then all characters following
* this string should be displayed in its message area.
*/

public class ChatClient {

private BufferedReader in;
private PrintWriter out;
private JFrame frame = new JFrame("MediChat");
private JTextField textField = new JTextField(40);
private JTextArea messageArea = new JTextArea(8, 40);

/* Constructs the client by laying out the GUI and registering a
* listener with the textfield so that pressing Return in the
* listener sends the textfield contents to the server. Note
* however that the textfield is initially NOT editable, and
* only becomes editable AFTER the client receives the NAMEACCEPTED
* message from the server.
*/
public ChatClient() {

// Layout GUI
textField.setEditable(false);
messageArea.setEditable(false);
frame.getContentPane().add(textField, "North");
frame.getContentPane().add(new JScrollPane(messageArea), "Center");
frame.pack();

// Add Listeners
textField.addActionListener(new ActionListener() {
/* Responds to pressing the enter key in the textfield by sending
* the contents of the text field to the server. Then clear
* the text area in preparation for the next message.
*/
public void actionPerformed(ActionEvent e) {
out.println(textField.getText());
textField.setText("");
}
});
}

/* Prompt for and return the address of the server.
*/
private String getServerAddress() {
return JOptionPane.showInputDialog(
frame,
"Enter IP Address of the Server:",
"Welcome to MediChat!",
JOptionPane.QUESTION_MESSAGE);
}

/* Prompt for and return the desired screen name.
*/
private String getName() {
return JOptionPane.showInputDialog(
frame,
"Choose a screen name:",
"Screen name selection",
JOptionPane.PLAIN_MESSAGE);
}

/* Connects to the server then enters the processing loop.
*/
private void run() throws IOException {

// Make connection and initialize streams
String serverAddress = getServerAddress();
Socket socket = new Socket(serverAddress, 9001);
in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);

// Process all messages from server, according to the protocol.
while (true) {
String line = in.readLine();
if (line.startsWith("SUBMITNAME")) {
out.println(getName());
} else if (line.startsWith("NAMEACCEPTED")) {
textField.setEditable(true);
} else if (line.startsWith("MESSAGE")) {
messageArea.append(line.substring(8) + "n");
}
}
}

/**
* Runs the client as an application with a closeable frame.
*/
public static void main(String args) throws Exception {
ChatClient client = new ChatClient();
client.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
client.frame.setVisible(true);
client.run();
}
}






java






share|improve this question







New contributor




K. Tan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




K. Tan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




K. Tan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked yesterday









K. Tan

82




82




New contributor




K. Tan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





K. Tan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






K. Tan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • I appreciate the quick accept!
    – GhostCat
    yesterday


















  • I appreciate the quick accept!
    – GhostCat
    yesterday
















I appreciate the quick accept!
– GhostCat
yesterday




I appreciate the quick accept!
– GhostCat
yesterday












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










You are seriously overcomplicating things:




  • don't make compiling your client code part of any Java class. Define a project setup in your IDE, or on the command line using gradle for example. Then use that to separately compile your classes whenever you change something. Running javac in your classes manually is seriously wrong!

  • and then, just make sure that all compiled class files are available on the classpath of your jvm. Don't bother to use reflection or anything else that is based on class names as raw strings.

  • most importantly: you use other classes by instantiating objects directly. The main method should only be used when you want to run a class standalone from the command line!

  • so instead of using class names as string, simply normally import the classes to use, and use new to create objects of them.

  • beyond that, separate your concerns. The client is the client, the server is the server. It is absolutely not a good idea to have the server start client instances. Meaning: rather create a third class, maybe called SetupTestEnvironment that first starts the server and a few clients for testing purposes.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });






    K. Tan is a new contributor. Be nice, and check out our Code of Conduct.










     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238511%2funable-to-run-class-in-a-separate-thread-window%23new-answer', 'question_page');
    }
    );

    Post as a guest
































    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    You are seriously overcomplicating things:




    • don't make compiling your client code part of any Java class. Define a project setup in your IDE, or on the command line using gradle for example. Then use that to separately compile your classes whenever you change something. Running javac in your classes manually is seriously wrong!

    • and then, just make sure that all compiled class files are available on the classpath of your jvm. Don't bother to use reflection or anything else that is based on class names as raw strings.

    • most importantly: you use other classes by instantiating objects directly. The main method should only be used when you want to run a class standalone from the command line!

    • so instead of using class names as string, simply normally import the classes to use, and use new to create objects of them.

    • beyond that, separate your concerns. The client is the client, the server is the server. It is absolutely not a good idea to have the server start client instances. Meaning: rather create a third class, maybe called SetupTestEnvironment that first starts the server and a few clients for testing purposes.






    share|improve this answer



























      up vote
      1
      down vote



      accepted










      You are seriously overcomplicating things:




      • don't make compiling your client code part of any Java class. Define a project setup in your IDE, or on the command line using gradle for example. Then use that to separately compile your classes whenever you change something. Running javac in your classes manually is seriously wrong!

      • and then, just make sure that all compiled class files are available on the classpath of your jvm. Don't bother to use reflection or anything else that is based on class names as raw strings.

      • most importantly: you use other classes by instantiating objects directly. The main method should only be used when you want to run a class standalone from the command line!

      • so instead of using class names as string, simply normally import the classes to use, and use new to create objects of them.

      • beyond that, separate your concerns. The client is the client, the server is the server. It is absolutely not a good idea to have the server start client instances. Meaning: rather create a third class, maybe called SetupTestEnvironment that first starts the server and a few clients for testing purposes.






      share|improve this answer

























        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        You are seriously overcomplicating things:




        • don't make compiling your client code part of any Java class. Define a project setup in your IDE, or on the command line using gradle for example. Then use that to separately compile your classes whenever you change something. Running javac in your classes manually is seriously wrong!

        • and then, just make sure that all compiled class files are available on the classpath of your jvm. Don't bother to use reflection or anything else that is based on class names as raw strings.

        • most importantly: you use other classes by instantiating objects directly. The main method should only be used when you want to run a class standalone from the command line!

        • so instead of using class names as string, simply normally import the classes to use, and use new to create objects of them.

        • beyond that, separate your concerns. The client is the client, the server is the server. It is absolutely not a good idea to have the server start client instances. Meaning: rather create a third class, maybe called SetupTestEnvironment that first starts the server and a few clients for testing purposes.






        share|improve this answer














        You are seriously overcomplicating things:




        • don't make compiling your client code part of any Java class. Define a project setup in your IDE, or on the command line using gradle for example. Then use that to separately compile your classes whenever you change something. Running javac in your classes manually is seriously wrong!

        • and then, just make sure that all compiled class files are available on the classpath of your jvm. Don't bother to use reflection or anything else that is based on class names as raw strings.

        • most importantly: you use other classes by instantiating objects directly. The main method should only be used when you want to run a class standalone from the command line!

        • so instead of using class names as string, simply normally import the classes to use, and use new to create objects of them.

        • beyond that, separate your concerns. The client is the client, the server is the server. It is absolutely not a good idea to have the server start client instances. Meaning: rather create a third class, maybe called SetupTestEnvironment that first starts the server and a few clients for testing purposes.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited yesterday

























        answered yesterday









        GhostCat

        85.2k1680139




        85.2k1680139






















            K. Tan is a new contributor. Be nice, and check out our Code of Conduct.










             

            draft saved


            draft discarded


















            K. Tan is a new contributor. Be nice, and check out our Code of Conduct.













            K. Tan is a new contributor. Be nice, and check out our Code of Conduct.












            K. Tan is a new contributor. Be nice, and check out our Code of Conduct.















             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238511%2funable-to-run-class-in-a-separate-thread-window%23new-answer', 'question_page');
            }
            );

            Post as a guest




















































































            Popular posts from this blog

            Bressuire

            Vorschmack

            Quarantine