I'm working with sockets and I have two java files (client.java and server.java) that I want to use two shell scripts to compile and execute. I want the user to enter the port number and host name as command line arguments, and I am unsure how to do this using the shell script instead of just straight executing the java files themselves. I've been trying to figure it out with a combination of the code below, but with no luck. Any tips would be thoroughly appreciated.
我正在使用套接字,我有两个java文件(client.java和server.java),我想使用两个shell脚本来编译和执行。我希望用户输入端口号和主机名作为命令行参数,我不确定如何使用shell脚本而不是直接执行java文件本身。我一直试图通过以下代码的组合来解决这个问题,但没有运气。任何提示都将非常感激。
#!/bin/bash
javac server.java $@
hostName = $1
portNumber = $2
** To clarify, I want two shell scripts (one for client and one for server) that will compile and execute these two java classes, as well as handle the command line arguments. The server script will take in a port number and try to connect to that port, showing an error message if unsuccessful. The client script will do the same with a host name and a port.
**为了澄清,我想要两个shell脚本(一个用于客户端,一个用于服务器),它们将编译和执行这两个java类,以及处理命令行参数。服务器脚本将接收端口号并尝试连接到该端口,如果不成功则显示错误消息。客户端脚本将对主机名和端口执行相同操作。
1 个解决方案
#1
1
A Java application can accept any number of arguments from the command line. This allows the user to specify configuration information when the application is launched.
Java应用程序可以从命令行接受任意数量的参数。这允许用户在启动应用程序时指定配置信息。
The user enters command-line arguments when invoking the application and specifies them after the name of the class to be run. For example, suppose a Java application called Sort sorts lines in a file. To sort the data in a file named friends.txt, a user would enter:
用户在调用应用程序时输入命令行参数,并在要运行的类的名称后指定它们。例如,假设一个名为Sort的Java应用程序对文件中的行进行排序。要对名为friends.txt的文件中的数据进行排序,用户将输入:
java Sort friends.txt When an application is launched, the runtime system passes the command-line arguments to the application's main method via an array of Strings. In the previous example, the command-line arguments passed to the Sort application in an array that contains a single String: "friends.txt".
java Sort friends.txt启动应用程序时,运行时系统通过字符串数组将命令行参数传递给应用程序的main方法。在前面的示例中,命令行参数在包含单个String的数组中传递给Sort应用程序:“friends.txt”。
Echoing Command-Line Arguments
回应命令行参数
The Echo example displays each of its command-line arguments on a line by itself:
Echo示例单独在一行上显示其每个命令行参数:
public class Echo {
public static void main (String[] args) {
for (String s: args) {
System.out.println(s);
}
}
}
The following example shows how a user might run Echo. User input is in italics.
以下示例显示用户如何运行Echo。用户输入以斜体显示。
java Echo Drink Hot Java Drink Hot Java Note that the application displays each word — Drink, Hot, and Java — on a line by itself. This is because the space character separates command-line arguments. To have Drink, Hot, and Java interpreted as a single argument, the user would join them by enclosing them within quotation marks.
java Echo Drink Hot Java Drink Hot Java请注意,应用程序单独显示每个单词 - Drink,Hot和Java。这是因为空格字符分隔了命令行参数。要将Drink,Hot和Java解释为单个参数,用户可以通过将它们括在引号内来加入它们。
java Echo "Drink Hot Java" Drink Hot Java Parsing Numeric Command-Line Arguments
java Echo“喝热Java”喝热Java解析数字命令行参数
If an application needs to support a numeric command-line argument, it must convert a String argument that represents a number, such as "34", to a numeric value. Here is a code snippet that converts a command-line argument to an int:
如果应用程序需要支持数字命令行参数,则它必须将表示数字的String参数(例如“34”)转换为数字值。这是一个将命令行参数转换为int的代码片段:
int firstArg;
if (args.length > 0) {
try {
firstArg = Integer.parseInt(args[0]);
} catch (NumberFormatException e) {
System.err.println("Argument" + args[0] + " must be an integer.");
System.exit(1);
}
}
parseInt throws a NumberFormatException if the format of args[0] isn't valid. All of the Number classes — Integer, Float, Double, and so on — have parseXXX methods that convert a String representing a number to an object of their type. Source: docs.oracle.com
如果args [0]的格式无效,则parseInt会抛出NumberFormatException。所有Number类 - Integer,Float,Double等 - 都有parseXXX方法,它们将表示数字的String转换为其类型的对象。资料来源:docs.oracle.com
#1
1
A Java application can accept any number of arguments from the command line. This allows the user to specify configuration information when the application is launched.
Java应用程序可以从命令行接受任意数量的参数。这允许用户在启动应用程序时指定配置信息。
The user enters command-line arguments when invoking the application and specifies them after the name of the class to be run. For example, suppose a Java application called Sort sorts lines in a file. To sort the data in a file named friends.txt, a user would enter:
用户在调用应用程序时输入命令行参数,并在要运行的类的名称后指定它们。例如,假设一个名为Sort的Java应用程序对文件中的行进行排序。要对名为friends.txt的文件中的数据进行排序,用户将输入:
java Sort friends.txt When an application is launched, the runtime system passes the command-line arguments to the application's main method via an array of Strings. In the previous example, the command-line arguments passed to the Sort application in an array that contains a single String: "friends.txt".
java Sort friends.txt启动应用程序时,运行时系统通过字符串数组将命令行参数传递给应用程序的main方法。在前面的示例中,命令行参数在包含单个String的数组中传递给Sort应用程序:“friends.txt”。
Echoing Command-Line Arguments
回应命令行参数
The Echo example displays each of its command-line arguments on a line by itself:
Echo示例单独在一行上显示其每个命令行参数:
public class Echo {
public static void main (String[] args) {
for (String s: args) {
System.out.println(s);
}
}
}
The following example shows how a user might run Echo. User input is in italics.
以下示例显示用户如何运行Echo。用户输入以斜体显示。
java Echo Drink Hot Java Drink Hot Java Note that the application displays each word — Drink, Hot, and Java — on a line by itself. This is because the space character separates command-line arguments. To have Drink, Hot, and Java interpreted as a single argument, the user would join them by enclosing them within quotation marks.
java Echo Drink Hot Java Drink Hot Java请注意,应用程序单独显示每个单词 - Drink,Hot和Java。这是因为空格字符分隔了命令行参数。要将Drink,Hot和Java解释为单个参数,用户可以通过将它们括在引号内来加入它们。
java Echo "Drink Hot Java" Drink Hot Java Parsing Numeric Command-Line Arguments
java Echo“喝热Java”喝热Java解析数字命令行参数
If an application needs to support a numeric command-line argument, it must convert a String argument that represents a number, such as "34", to a numeric value. Here is a code snippet that converts a command-line argument to an int:
如果应用程序需要支持数字命令行参数,则它必须将表示数字的String参数(例如“34”)转换为数字值。这是一个将命令行参数转换为int的代码片段:
int firstArg;
if (args.length > 0) {
try {
firstArg = Integer.parseInt(args[0]);
} catch (NumberFormatException e) {
System.err.println("Argument" + args[0] + " must be an integer.");
System.exit(1);
}
}
parseInt throws a NumberFormatException if the format of args[0] isn't valid. All of the Number classes — Integer, Float, Double, and so on — have parseXXX methods that convert a String representing a number to an object of their type. Source: docs.oracle.com
如果args [0]的格式无效,则parseInt会抛出NumberFormatException。所有Number类 - Integer,Float,Double等 - 都有parseXXX方法,它们将表示数字的String转换为其类型的对象。资料来源:docs.oracle.com