title: How can we set Java classpath in shell script?
body: <p>I need a help with this concept. I searched several threads in this forum but none matched my problem. </p>
  
  <p>I write two java classes and a shell script. I zip them into a folder and send it to another person who does not know java. He would just unpack and run the shell script.</p>
  
  <p>The person will extract the code anywhere in his machine. So now the shell script should do the following.</p>
  
  <ol>
  <li>Check if Java is installed properly</li>
  <li>If so, set the java class path to the folder where the java classes are present.</li>
  <li>Check java version. This is achieved in the script. So no problem</li>
  <li>Compile and run.</li>
  </ol>
  
  <p>My zip file would contain App.java, Message.java, MyCodeExecution.sh.  </p>
  
  <pre><code>    #!/bin/bash
  
      export CLASSPATH=$CLASSPATH:.
  
      echo $CLASSPATH
  
      VER=`java -version 2&gt;&amp;1 | grep "java version" | awk '{print $3}' 
      | tr   -d \" | awk '{split($0, array, ".")} END{print array[2]}'`
      if [[ $VER -gt 6 ]]; then
          echo "Java version is greater than 1.6."
      else
          echo "Java version is lower than 1.6."
      fi
  
      javac App.java
      javac Message.java
      java App
  </code></pre>
  
  <p>This is my code for Message and App.java</p>
  
  <pre><code>  package com.demo;
  
      import java.io.BufferedReader;
      import java.io.IOException;
      import java.io.InputStreamReader;
  
      /**
       * This class creates two instances of same class and performs message
       * transmission.
       * 
       */
      public class App {
          final static String initTurn = "initiator";
          final static String receiverTurn = "receiver";
  
  
  
          public static void main(String[] args) {
              App app = new App();
              InputStreamReader r = new InputStreamReader(System.in); // Input reader for messages
              InputStreamReader totalMessages = new InputStreamReader(System.in); // Input reader to get total number of
                                                                                  // messages
              BufferedReader br = null;
              Message initiator = new Message(); /* Instance for initiator who initiates the message */
              Message receiver = new Message(); /* Instance for receiver who receives and re-sends the message. */
  
              String initMessage = null; /* Message got from the initiator */
              String recvMessage = null; /* Message to be sent back to initiator by receiver */
              String turn = initTurn; /* Turn to send message by initiator or receiver. */
              try {
  
                  /*
                   * This snippet is implemented to give option of sending n number of messages
                   * and receive it. User can choose to transmit total number of messages
                   */
                  int c = 0;
                   System.out.println("How many messages do you wish to transmit? ");
  
                  br = new BufferedReader(totalMessages);
                  c = Integer.parseInt(br.readLine());
  
                  /* br object is reset for receiving messages from user. */
                  br = null;
                  br = new BufferedReader(r);
                  for (int counter = 1; counter &lt;= c;) { /*
                                                           * Message transmission stops when initiator reach the transmission
                                                           * limit
                                                           */
                      if (initTurn.equals(turn)) {
                          System.out.println("\nInitiator... Please type your message:\n ");
                          if (counter == c) {
                               System.out.println("This would be the last message which you may sent and receive.\n");
  
                          }
                          /* Receiving message as input by initiator. */
                          initMessage = br.readLine();
                          turn = receiverTurn; /*
                                                   * turn is set to receiver that is transmission turn is now given to
                                                   * receiver.
                                                   */
                          initiator.setMessage(initMessage);
                      } else if (receiverTurn.equals(turn)) {
                          /* Receiver gets message from Initiator */
                          receiver.setMessage(initiator.getMessage());
                          recvMessage = receiver.getMessage();
                          /* Receiver re-sends the message to the initiator with the message counter. */
                           System.out.println("\nMessage received by Initiator from Receiver is: ' " +
                           recvMessage
                           + "'. Message counter " + counter);
  
                          if (counter == c) {
                              System.out.println("\nMessage transmission limit over. See you again.");
                          }
                          turn = initTurn; /*
                                               * turn is set to receiver that is transmission turn is now given to initiator.
                                               */
                          counter++;
                      }
                  }
  
              } catch (IOException e) {
                  e.printStackTrace();
              } finally {
                  if (br != null) {
                      try {
                          br.close();
                      } catch (IOException ie) {
                          ie.printStackTrace();
                      }
                  }
                  if (totalMessages != null) {
                      try {
                          totalMessages.close();
                      } catch (IOException ie) {
                          ie.printStackTrace();
                      }
                  }
                  if (r != null) {
                      try {
                          r.close();
                      } catch (IOException ie) {
                          ie.printStackTrace();
                      }
                  }
              }
  
          }
      }
  
      class Message {
  
          String message;
  
          public String getMessage() {
              return message;
          }
  
          public void setMessage(String message) {
              this.message = message;
          }
  
      }
  </code></pre>