5 Replies Latest reply: May 1, 2014 5:20 AM by jwenting RSS

    Command Line Apps

    gdillen

      I need several command line applications (running app with entering parameters at the command prompt). Is it correct to have 1 package (e.g. com.xyz.apps) and within this package several classes with a main method (besides of course other classes for supporting the main-method classes)? When building my classes in the package this way (using NetBeans) I get 1 jar file. I suppose this way I can't provide command line parameters to the different  classes that have a main method? How can I achieve this?

       

      Many thanks.

        • 1. Re: Command Line Apps
          rp0428
          I need several command line applications (running app with entering parameters at the command prompt). Is it correct to have 1 package (e.g. com.xyz.apps) and within this package several classes with a main method (besides of course other classes for supporting the main-method classes)? When building my classes in the package this way (using NetBeans) I get 1 jar file. I suppose this way I can't provide command line parameters to the different  classes that have a main method? How can I achieve this?

          I'm not sure exactly what your question is,

           

          Can you have several classes in 1 package? Yes

           

          Can each of those classes have their own 'main' method? Yes

           

          Can I execute each of those classes from the command line? Yes

           

          But with only one jar file you can NOT execute more than one of those classes by just executing the jar file. The jar file manifest can specify ONE main class. That is the class that will get executed if you 'execute' the jar file itself.

           

          You can specify any of the classes explicitly to run.

          • 2. Re: Command Line Apps
            gdillen

            Thanks.

             

            I suppose (after some Googling) running a specific class in the jar file that contains a main method as follows:

            "java -cp myjarfile.jar com.xyz.abc.ClassThatContainsMain" where com.xyz.abc is the package name?

             

            One additional question: I also suppose as a best practice that the name of the jar file doesn't contain the package name:

            so instead of "com.xyz.abc.myjarfile.jar" using myjarfile.jar?

             

            Thanks.

            • 3. Re: Command Line Apps
              rp0428

              One additional question: I also suppose as a best practice that the name of the jar file doesn't contain the package name:

              so instead of "com.xyz.abc.myjarfile.jar" using myjarfile.jar?

              Ok - but then what if your wife wants to create a jar? She might name it 'myjarfile.jar' since it belongs to her.

               

              The naming convention you use for your jars is entirely up to you. If you have several apps that need to be distributed separately you may not want to include them all in one jar file anyway. But your distribution strategy is also up to you.

              • 4. Re: Command Line Apps
                gimbal2

                rp0428 wrote:

                 

                One additional question: I also suppose as a best practice that the name of the jar file doesn't contain the package name:

                so instead of "com.xyz.abc.myjarfile.jar" using myjarfile.jar?

                Ok - but then what if your wife wants to create a jar? She might name it 'myjarfile.jar' since it belongs to her.

                 

                Yeah but you can rename it to mywifesjarfile.jar

                • 5. Re: Command Line Apps
                  jwenting

                  or just change the password to your computer so your wife can't mess around with it