我们如何在Shell脚本中设置Java类路径?

时间:2018-12-10 11:30:48

标签: java shell unix classpath java-home

我需要有关此概念的帮助。我在这个论坛中搜索了多个主题,但没有一个匹配我的问题。

我写了两个Java类和一个Shell脚本。我将它们压缩到一个文件夹中,然后发送给不懂Java的其他人。他只是解压缩并运行Shell脚本。

此人将在其机器的任何位置提取代码。因此,现在,shell脚本应该执行以下操作。

  1. 检查Java是否正确安装
  2. 如果是这样,请将Java类路径设置为存在Java类的文件夹。
  3. 检查Java版本。这是在脚本中实现的。所以没问题
  4. 编译并运行。

我的zip文件将包含App.java,Message.java,MyCodeExecution.sh。

    #!/bin/bash

    export CLASSPATH=$CLASSPATH:.

    echo $CLASSPATH

    VER=`java -version 2>&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

这是我的Message和App.java代码

  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 <= 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;
        }

    }

1 个答案:

答案 0 :(得分:0)

您可以在shell脚本中将类路径设置为当前文件夹,如下所示:
export CLASSPATH = $ CLASSPATH :.
这样,就可以在同一文件夹中找到Player类。