Runtime.getRuntime().exec(command) - I get nothing
Hi all. This is a beginner speaking.
When I try to use Runtime.getRuntime().exec(command) I get no errors, but nothing happens. I am in the correct directory, and have tried with complete filepaths also. When I run the command ( cat file.txt > file2.txt) in the terminal it works as expected. If you could help me sort this out it would be great, and very much appreaciated!
EDIT: It turns out that the command runs fine, except for the copying to another file part: > file2.txt
How come?
Heres the code: (As I said, I'm a beginner, so forgive me for the messy formating.)
Quote:
public class server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;
String clientCommand, capitalizedSentence;
Integer port = 0;
port = Integer.parseInt(args[0]);
try {
serverSocket = new ServerSocket(port); //1234
} catch (IOException e) {
System.err.println("Could not listen on port: 1234.");
System.exit(1);
}
Socket clientSocket = null;
try {
clientSocket = serverSocket.accept();
} catch (IOException e) {
System.err.println("Accept failed.");
System.exit(1);
}
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(clientSocket.getOutputStream());
clientCommand = inFromClient.readLine();
System.out.println("The Command is: " + clientCommand); // The correct command is printed.
Process runCommand = Runtime.getRuntime().exec(clientCommand);
outToClient.close();
inFromClient.close();
clientSocket.close();
serverSocket.close();
}
}