-4

I am new in how to start a program inside another java program! and i don't know how to call it. The 1st question is that i must do it inside a new thread? the second question is how to call the new program. The program i want to call is "wondershaper". I use ubuntu 12.04 for running this programm and in command line i write. "sudo wondershaper eth0 10000 1000". How i can write it inside a general program? I have a server that i want to handle the rate of it! thats why i use it. So i have a multithread server and the code is

class Client extends Thread {
    private Socket connectionSocket;


    public Client(Socket c) throws IOException {
        connectionSocket = c;
    }

    public void run() {

        String path = "C:/pao/new2/";

        File folder = new File(path);
        File[] listOfFiles = folder.listFiles();


        try {

            String fileToSendStr = readFile();
            File fileToSend = null;
            for (File f : listOfFiles)

            {               
                if (f.getName().equals(fileToSendStr)) {
                    fileToSend = f;
                    break;
                }
            }
            //System.out.println("Connescting to Client to recieve the part " +fileToSendStr);
            if (fileToSend == null) {

            }
            System.out.println("Sending the chunk to Client " + fileToSendStr + "to the client: " +connectionSocket.getRemoteSocketAddress().toString());
            java.util.Date date= new java.util.Date();
             System.out.println(new Timestamp(date.getTime()));
            long length = fileToSend.length();
            byte [] longBytes = new byte[8];
            ByteBuffer bbuffer = ByteBuffer.wrap(longBytes);
            bbuffer.putLong(length);
            connectionSocket.getOutputStream().write(longBytes);

            BufferedOutputStream bout = new BufferedOutputStream(connectionSocket.getOutputStream());
            BufferedInputStream bain = new BufferedInputStream(new FileInputStream(fileToSend));

            byte buffer [] = new byte [1024];
            int i = 0;
            while((i = bain.read(buffer, 0, 1024)) >= 0){

                bout.write(buffer, 0, i);


            }



            System.out.println("chunk sended");
            java.util.Date date1= new java.util.Date();
             System.out.println(new Timestamp(date1.getTime()));
            bout.close();
            bain.close();


        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private String readFile() throws IOException {

        BufferedReader r = new BufferedReader(new InputStreamReader(
                connectionSocket.getInputStream()));
        return r.readLine();

    }
}

so when i read the readFile what client send to me. After if it is the rate string to start the "wondershaper" and put the rate inside the "sudo wondershaper eth0 10000 rate" and start the program

Chris Bour
  • 25
  • 1
  • 7

1 Answers1

1

Process aProcess = Runtime.getRuntime().exec("cmd"); //you can pass any process here

you can also read the output of this program.

InputStream is = aProcess.getInputStream();

Ps: You can pass any process, along with the arguments, but you can't pass things like >>, 2> or | or wild cards like *

-- from the comments

Ankit
  • 6,554
  • 6
  • 49
  • 71