-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

2
  • I'm not 100% sure, but I guess you're looking for Java's Process class. Commented Jul 23, 2013 at 19:37
  • Related question: stackoverflow.com/questions/3643939/… Commented Jul 23, 2013 at 19:41

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

7 Comments

You can pass any process, but you can't pass things like >>, 2> or |
Yes, the Runtime class is quite important to learn, and very useful in many cases dealing with console scenarios
@VitalijZadneprovskij you can pass, if it is supported by shell.
but except wild cards chars
@ay89 I have tried on a Linux environment and it did not work. They were considered as arguments to pass to the process to call. Maybe in that case you have to use bash cmd arg1 arg2 argN >> output.txt
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.