I have implemented a chat server client. I have written the following shell script to dynamically take in the server port number: server.sh
javac -classpath . com/chat/ChatConstants.java
javac -classpath . com/chat/ChatServer.java
echo Enter server port number
read $1
java com.chat.ChatServer $1
This is the java main method I am trying to pass the argument to :
public static void main(String args[]) {
// The default port number.
int portNumber = 8888;
if (args.length < 1) {
System.out.println("Chat server is up on " + portNumber);
} else {
portNumber = Integer.valueOf(args[0]).intValue();
System.out.println("Chat server is up on " + portNumber);
}
}
However, the port number printed is always the default port ie 8888. When I run the java program as follows
java com.chat.ChatServer 2727
The cmd line args are taken properly and server port is set to 2727.
I seem to be doing something wrong in the shell script. I even tried passing the arguments with quotes as follows:
java com.chat.ChatServer "$1"
The command prompt closes promptly.
Please help
Your script should be:
read PORT
java com.chat.ChatServer $PORT
$1
is a special variable which holds the first commandline argument passed to the shell script.
You need to read
your input into a different variable.
Edit: Just a sidenote, you can print a prompt without using echo
by using read -p "Enter server port number" P
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.