1

My task is to implement Distance Vector Multicast Routing Protocol (DVMRP) using files (No sockets or threads). I will have three java programs

  1. Host.java
  2. Router.java
  3. Controller.java

The procedure for testing is:

  1. Run the test1.sh script file on a new terminal window

test1.sh looks like:

rm -f lan?  hout?  hin?
rm -f rout?
router 0 0 1 &
router 1 1 2 &
router 2 2 3 &
router 3 3 0 &
controller host router 0 1 2 3 lan 0 1 2 3&

Here, the line containing

router 0 0 1 &

means that a process is created from the executable file "router" (basically Router.java) and runs it in the background with input 0 0 1, & is I think to say that its in bash mode.

Similarly the next line

router 1 1 2 &

means that another process is created from the executable file "router" (basically Router.java) and runs in the background with input 1 1 2.

Similarly, the line:

controller ...

means that another process is created from the executable file "controller" (basically Controller.java) and runs in the background with the corresponding input.

How to do this? Sorry for earlier confusion :)

4
  • What's the problem? You can do: java Router 0 0 1 & Commented Dec 5, 2013 at 16:53
  • OP wants to know how to call the sh script from Java with the appropriate arguments Commented Dec 5, 2013 at 16:58
  • By the last line in the script "controller host router 0 1 2 3 lan 0 1 2 3&", do you mean to invoke the Controller.java program with the parameters "host router 0 1 2 3 lan 0 1 2 3", or do you intend to have "router 0 1 2 3" be replaced by running this is a command? What is the output of the router command? Commented Dec 5, 2013 at 17:30
  • The last line is meant to execute the controller process (executed once only) and the input is: router 0...9 host 0...9 lan 0...9 This input tells the controller process that there are routers and their router-id which ranges from 0 to 9, hosts and their host-id which ranges from 0 to 9, lans and their lab-id which ranges from 0 to 9 Commented Dec 5, 2013 at 17:37

2 Answers 2

4

First you compile your java program:

javac Router.java

You should invoke java like this:

java Router 0 0 1 &

You can find a Hello World example here

Edit: I am still not entirely sure I understand what you are trying to do, but you might try to change the above test1.sh to this:

rm -f lan?  hout?  hin?
rm -f rout?
java Router 0 0 1 &
java Router 1 1 2 &
java Router 2 2 3 &
java Router 3 3 0 &
java Controller host router 0 1 2 3 lan 0 1 2 3&

This will spawn 4 instances of Router and one instance of Controller. This assumes that you have compiled the programs first:

javac Router.java Controller.java Host.java
Sign up to request clarification or add additional context in comments.

8 Comments

I have three programs Host.java, Router.java & Controller.java (implementing DVMRP using files). So first Host.java is run from a terminal window, then Router.java from another terminal window and then Controller.java from yet another terminal window. Then the shell script file is executed on yet another terminal.
I do not follow what you mean. .java files are compiled to binary(-ish) .class files which are invoked with "java <classname>". Can you rephrase your question to elaborate what you are looking to accomplish?
I am editing the question with details, please wait.
Question explained in detail. Please have a look.
I just updated the proposed solution. Is this what you are trying do to?
|
0

If I understand correctly, you have a Java program called Router.java that takes the parameters, and you want to run it in the background.

If my assumption is correct, then you need to do some things:

  1. Compile the java program
  2. Run the program in the background

To compile your program:

javac Router.java

To run your program:

java Router 0 0 1 &

After reading (and reading again) your question and your comments, I think your problem is that you don't know how to pass parameters to your Java program from the command line. If that is your problem, you should remember that the main function has an args parameter, which is a way to pass values to the program from the command line. The args variable is an array of String objects that you can use in your code:

public class Router {
    /*
     * Your methods go here
     */
    public static void main(String[] args) {
        /*
         * The 'args' variable holds the arguments entered in the command line
         */
        if(args[0].equals("a")) {
            /*
             * This is an example of how to read the arguments supplied in the command line.
             */
        }
    }
}

7 Comments

The scenario is I compile Router.java by: java Router.java Then I run it by: java Router Similarly, I run two other programs Host.java & Controller.java on separate Terminals. Now I need to open up another terminal and execute the shell script file namely test1.sh which contains: rm -f lan? hout? hin? rm -f rout? router 0 0 1 & router 1 1 2 & router 2 2 3 & router 3 3 0 & controller host router 0 1 2 3 lan 0 1 2 3& The Host, Router and Controller processes take inputs related to them from this script file execution.
@user2961121 I don't understand what you are trying to do. Are your java programs already written and you need to call them from a shell script? Or you want to call a shell script from your java programs?
I have the java programs. Steps: 1. Run each java program from separate terminals 2. On a new terminal run the script file which provides input for all three java programs.
@user2961121 and do the programs already run individually? have you tested them? have you compiled them?
Please have a look at the detailed question.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.