我无法让apache commons cli工作。
我有一个最简单的开始: 这是唯一的课程。 使用maven(命令行)添加资源。
import org.apache.commons.cli.*;
public class App {
public static void main(String[] args) {
// create Options object
Options options = new Options();
CommandLineParser parser = new DefaultParser();
// add t option
options.addOption("t", false, "display current time");
try{
CommandLine cmd = parser.parse( options, args);
}catch(ParseExeption ex){
}
if(cmd.hasOption("t")) {
// print the date and time
}else {
// print the date
}
}
}
无论我尝试过什么。我得到"找不到符号"。 这是错误的最后一部分:
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /Users/peter/Code/java/using_archetypes/using_cli_1/src/main
/java/com/mkyong/core/utils/App.java:[27,8] cannot find symbol
symbol: class ParseExeption
location: class com.mkyong.core.utils.App
[ERROR] /Users/peter/Code/java/using_archetypes/using_cli_1/src/main
/java/com/mkyong/core/utils/App.java:[31,4] cannot find symbol
symbol: variable cmd
location: class com.mkyong.core.utils.App
[INFO] 2 errors
[INFO] -------------------------------------------------------------
[INFO]
-------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO]
-------------------------------------------------------------------
[INFO] Total time: 0.842 s
[INFO] Finished at: 2016-10-31T12:17:29+01:00
[INFO] Final Memory: 15M/309M
[INFO]
---------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-
compiler-plugin:3.1:compile (default-compile) on project
dateUtils2: Compilation failure: Compilation failure:
[ERROR] /Users/peter/Code/java/using_archetypes/using_cli_1/src/main
/java/com/mkyong/core/utils/App.java:[27,8] cannot find symbol
[ERROR] symbol: class ParseExeption
[ERROR] location: class com.mkyong.core.utils.App
[ERROR] /Users/peter/Code/java/using_archetypes/using_cli_1/src/main
/java/com/mkyong/core/utils/App.java:[31,4] cannot find symbol
[ERROR] symbol: variable cmd
[ERROR] location: class com.mkyong.core.utils.App
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven
with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug
logging.
[ERROR]
[ERROR] For more information about the errors and possible
solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN
/MojoFailureException
请帮我从commons-cli开始。 这是使用maven编译器编译的。 谢谢。
答案 0 :(得分:1)
只需将ParseExeption
替换为ParseException
,然后将if / else块移动到与您定义变量cmd
相同的代码块中,否则它将无法显示,因为例如下一个:
public static void main(String[] args) throws ParseException{
// create Options object
Options options = new Options();
CommandLineParser parser = new DefaultParser();
// add t option
options.addOption("t", false, "display current time");
CommandLine cmd = parser.parse( options, args);
if(cmd.hasOption("t")) {
// print the date and time
}else {
// print the date
}
}