6 Replies Latest reply on Jun 6, 2017 8:34 PM by delphinus818

    EAP 6.1 Accessing CLI programmatically

    Eric Ko Newbie

      Hey all,

       

      I need to access the CLI from my business logic. Following the tutorial in https://community.jboss.org/wiki/JBossAS7Command-linePublicAPI as well as the native management API documentation https://docs.jboss.org/author/display/AS71/The+native+management+API, I imported the dependency org.jboss.as:jboss-as-cli:7.2.0.Final into my project and wrote the following method:

       

      private void runSomeCliCommands() {

             

              ModelControllerClient client = null;

             

              try {

                  CommandContext ctx = CommandContextFactory.getInstance().newCommandContext();

                  ctx.connectController();

       

                  client = ctx.getModelControllerClient();

       

                  // Create some operations and execute them...

       

              } catch (CommandLineException e) {

                  logger.warn("Error when getting connecting to controller", e);

              } catch (IOException e) {

                  logger.warn("Error when executing command", e);

              }

              finally {

                  if (client != null) {

                      try {

                          client.close();

                      } catch (IOException e) {

                          logger.warn("Error when closing client", e);

                      }

                  }

              }

          }

       

      However, when I run CommandContextFactory.getInstance().newCommandContext(), it throws the exception org.jboss.as.cli.CliInitializationException: Failed to parse /usr/local/jboss-eap-6.1/bin/jboss-cli.xml with the root cause:

       

      Caused by: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[6,1]

      Message: Unexpected element '{urn:jboss:cli:1.2}jboss-cli'

       

      I found a similar issue in Jira https://issues.jboss.org/browse/JBASMP-47 and suspected that it might be a dependency issue but it looks like I'm using the correct version of jboss-as-cli.

       

      Anyone have any ideas?


        • 1. Re: EAP 6.1 Accessing CLI programmatically
          jaikiran pai Master

          Eric, welcome to the forums.

           

          CLI (Command Line Interface) is a tool to facilitate invoking management operations against a server. It's an alternative to the web console. When you say you want to use the CLI in your application, do you really mean you want to use that CLI (the command line interface) or do you mean that you want to invoke management operations against the server from your application, just like the CLI does?

          • 2. Re: EAP 6.1 Accessing CLI programmatically
            Eric Ko Newbie

            Hi, sorry for the confusion. I meant that I want to invoke management operations against the server from my application.

            • 3. Re: EAP 6.1 Accessing CLI programmatically
              Eric Ko Newbie

              Digging down into the source code of jboss-as-cli-7.2.0.Final.jar, I see that there are two Namespace enums defined CLI_1_0("urn:jboss:cli:1.0") and CLI_1_1("urn:jboss:cli:1.1"). However, the namespace version in the actual jboss-cli.xml document is urn:jboss:cli:1.2. jboss-as-cli-7.2.0.Final.jar, which I fetch from the maven repository, is not exactly the same version that is running in the appserver, which is jboss-as-cli-7.2.0.Final-redhat-8.jar and contains the correct namespace enum.

               

              So what's up with the discrepency?

              • 4. Re: EAP 6.1 Accessing CLI programmatically
                Jan Martiska Newbie

                jboss-as-cli-7.2.0.Final.jar is a jar from JBoss AS 7.2.0.Final.

                jboss-as-cli-7.2.0.Final-redhat-8.jar is a jar from the enterprise edition - JBoss EAP 6.1.0, which is way newer than JBoss AS 7.2.0.Final and contains a newer version of the XML schema (urn:jboss:cli:1.2). And you're trying to use the old JAR with the new schema.

                 

                I suppose you are running that code from within JBoss EAP? If yes, you can just add a dependency to the org.jboss.as.cli module for your deployment (either through MANIFEST.MF or jboss-deployment-structure.xml).. then, the jboss-as-cli-7.2.0.Final-redhat-8.jar will be on the classpath for your application and you don't need to add any dependency added through maven or whatever else you are using. If you want to add the CLI dependency through maven, you need to depend on jboss-as-cli-7.2.0.Final-redhat-8.jar (which is not available in public repositories, so you need to use the zipped repo distributed with EAP).

                • 5. Re: EAP 6.1 Accessing CLI programmatically
                  Eric Ko Newbie

                  Yep, realized that I was using 7.2.0.Final. Changed the version to 7.2.0.Final-redhat-8 and now it works. Thanks.

                  • 6. Re: EAP 6.1 Accessing CLI programmatically
                    delphinus818 Newbie

                    I tried your suggestion of using jboss-deployment-structure.xml to specify org.jboss.as.cli as a module and exclude all dependencies.  I got the following error, do you know why?  I'm using Wildfly 10.0.0.

                     

                    CliInitializationException : org.jboss.as.cli.CliInitializationException: Failed to load org.jboss.as.cli.impl.CommandContextFactoryImpl

                    at org.jboss.as.cli.CommandContextFactory.getInstance(CommandContextFactory.java:43)