Peter-TI Peter-TI - 1 month ago 11
Java Question

Error cannot find symbol after try-catch block

I wasn't able to get apache commons cli to work.

apache commons-cli

I have the most simple start:
This is the only class.
Resources are added with maven(commandline).

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


No matter what I tried. I get the "cannot find symbol".
This is the last part of the error:

[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


Please help me to start with the commons-cli.
This is compiled with the maven compiler.
thank you.

Answer

Simply replace ParseExeption with ParseExceptionand move the if/else block in the same code block as where you defined your variable cmd otherwise it won't be visible, for example as next:

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
    }

}