Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: Runtime.getRuntime().exec(command) - I get nothing

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.)


    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();
    }
    }
    Last edited by andersbk; October 7th, 2010 at 11:30 PM.

Similar Threads

  1. Runtime Error running in UVA
    By mathfxr in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 17th, 2010, 03:06 PM
  2. Is it possible to load classes at runtime that are discovered from a directory
    By mydarkpassenger in forum Java Theory & Questions
    Replies: 11
    Last Post: June 10th, 2010, 11:33 AM
  3. Create buttons at runtime
    By rtumatt in forum AWT / Java Swing
    Replies: 2
    Last Post: May 24th, 2010, 07:42 AM
  4. How to Navigate to a URL with Runtime.getRuntime().exec()
    By Flash in forum Java SE API Tutorials
    Replies: 4
    Last Post: October 5th, 2009, 08:18 PM
  5. Getting Null Pointer Exception in runtime
    By RoadRunner in forum Exceptions
    Replies: 1
    Last Post: April 26th, 2009, 02:21 PM