How to create a Jar file with execution options? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Data science time! April 2019 and salary with experience Should we burninate the [wrap] tag? The Ask Question Wizard is Live!Create ArrayList from arrayHow To: Execute command line in C#, get STD OUT resultsHow can I pretty-print JSON in a shell script?Can I add jars to maven 2 build classpath without installing them?How can I create an executable JAR with dependencies using Maven?How to symlink a file in Linux?How to create a .jar file or export jar on IntelliJ (like eclipse java archive export)Creating a memory leak with JavaCan't execute jar- file: “no main manifest attribute”How do I find all files containing specific text on Linux?

Resolving to minmaj7

Denied boarding although I have proper visa and documentation. To whom should I make a complaint?

What does the "x" in "x86" represent?

Withdrew £2800, but only £2000 shows as withdrawn on online banking; what are my obligations?

How widely used is the term Treppenwitz? Is it something that most Germans know?

What is Arya's weapon design?

Using audio cues to encourage good posture

Why did the rest of the Eastern Bloc not invade Yugoslavia?

The logistics of corpse disposal

Output the ŋarâþ crîþ alphabet song without using (m)any letters

Why do people hide their license plates in the EU?

What does an IRS interview request entail when called in to verify expenses for a sole proprietor small business?

Why was the term "discrete" used in discrete logarithm?

Why light coming from distant stars is not discreet?

How to answer "Have you ever been terminated?"

Seeking colloquialism for “just because”

Short Story with Cinderella as a Voo-doo Witch

Why are there no cargo aircraft with "flying wing" design?

Is it fair for a professor to grade us on the possession of past papers?

Why am I getting the error "non-boolean type specified in a context where a condition is expected" for this request?

How can I make names more distinctive without making them longer?

51k Euros annually for a family of 4 in Berlin: Is it enough?

Why are Kinder Surprise Eggs illegal in the USA?

Identifying polygons that intersect with another layer using QGIS?



How to create a Jar file with execution options?



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
Data science time! April 2019 and salary with experience
Should we burninate the [wrap] tag?
The Ask Question Wizard is Live!Create ArrayList from arrayHow To: Execute command line in C#, get STD OUT resultsHow can I pretty-print JSON in a shell script?Can I add jars to maven 2 build classpath without installing them?How can I create an executable JAR with dependencies using Maven?How to symlink a file in Linux?How to create a .jar file or export jar on IntelliJ (like eclipse java archive export)Creating a memory leak with JavaCan't execute jar- file: “no main manifest attribute”How do I find all files containing specific text on Linux?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















How can I create a jar file with execution options, for example, the idea is execute the command:



java -jar MyProgram.jar -M someFile.txt


or



java -jar MyProgram.jar -cp someFile.txt


So the -M option defines a particular method to process the file someFile.txt and with -cp defines another behavior for the code.



With this in mine, how can I get this result from my code, is there something I need to write in the Main class, or how can I define such behavior?










share|improve this question

















  • 1





    Have you thought about using the args in public static void main(String[] args) for that?

    – deHaar
    Mar 8 at 17:44











  • For that case, I should define two arguments for the code, one for the function I need to choose cp or M and the input file, and catch them using an if statement on the main class?

    – EEsparaquia
    Mar 8 at 17:48






  • 1





    Yes, exactly... That should be the choice in your case, I think.

    – deHaar
    Mar 8 at 17:49

















0















How can I create a jar file with execution options, for example, the idea is execute the command:



java -jar MyProgram.jar -M someFile.txt


or



java -jar MyProgram.jar -cp someFile.txt


So the -M option defines a particular method to process the file someFile.txt and with -cp defines another behavior for the code.



With this in mine, how can I get this result from my code, is there something I need to write in the Main class, or how can I define such behavior?










share|improve this question

















  • 1





    Have you thought about using the args in public static void main(String[] args) for that?

    – deHaar
    Mar 8 at 17:44











  • For that case, I should define two arguments for the code, one for the function I need to choose cp or M and the input file, and catch them using an if statement on the main class?

    – EEsparaquia
    Mar 8 at 17:48






  • 1





    Yes, exactly... That should be the choice in your case, I think.

    – deHaar
    Mar 8 at 17:49













0












0








0








How can I create a jar file with execution options, for example, the idea is execute the command:



java -jar MyProgram.jar -M someFile.txt


or



java -jar MyProgram.jar -cp someFile.txt


So the -M option defines a particular method to process the file someFile.txt and with -cp defines another behavior for the code.



With this in mine, how can I get this result from my code, is there something I need to write in the Main class, or how can I define such behavior?










share|improve this question














How can I create a jar file with execution options, for example, the idea is execute the command:



java -jar MyProgram.jar -M someFile.txt


or



java -jar MyProgram.jar -cp someFile.txt


So the -M option defines a particular method to process the file someFile.txt and with -cp defines another behavior for the code.



With this in mine, how can I get this result from my code, is there something I need to write in the Main class, or how can I define such behavior?







java linux command-line jar executable-jar






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 8 at 17:42









EEsparaquiaEEsparaquia

386




386







  • 1





    Have you thought about using the args in public static void main(String[] args) for that?

    – deHaar
    Mar 8 at 17:44











  • For that case, I should define two arguments for the code, one for the function I need to choose cp or M and the input file, and catch them using an if statement on the main class?

    – EEsparaquia
    Mar 8 at 17:48






  • 1





    Yes, exactly... That should be the choice in your case, I think.

    – deHaar
    Mar 8 at 17:49












  • 1





    Have you thought about using the args in public static void main(String[] args) for that?

    – deHaar
    Mar 8 at 17:44











  • For that case, I should define two arguments for the code, one for the function I need to choose cp or M and the input file, and catch them using an if statement on the main class?

    – EEsparaquia
    Mar 8 at 17:48






  • 1





    Yes, exactly... That should be the choice in your case, I think.

    – deHaar
    Mar 8 at 17:49







1




1





Have you thought about using the args in public static void main(String[] args) for that?

– deHaar
Mar 8 at 17:44





Have you thought about using the args in public static void main(String[] args) for that?

– deHaar
Mar 8 at 17:44













For that case, I should define two arguments for the code, one for the function I need to choose cp or M and the input file, and catch them using an if statement on the main class?

– EEsparaquia
Mar 8 at 17:48





For that case, I should define two arguments for the code, one for the function I need to choose cp or M and the input file, and catch them using an if statement on the main class?

– EEsparaquia
Mar 8 at 17:48




1




1





Yes, exactly... That should be the choice in your case, I think.

– deHaar
Mar 8 at 17:49





Yes, exactly... That should be the choice in your case, I think.

– deHaar
Mar 8 at 17:49












2 Answers
2






active

oldest

votes


















1














I think you probably need to check the Apache Commons-CLI, it allows you to do the things you describe above, also I give an example how it works, it give the way to specify a message for the usage of the arguments:



https://commons.apache.org/proper/commons-cli/introduction.html



 Options options = new Options();

options.addOption( "M", false,"Merge files request.")
.addOption("CP", false,"Copy files from file.");

CommandLineParser parser = new DefaultParser();
try {

CommandLine cmd = parser.parse(options, args);
if (!cmd.hasOption("M"))
throw new IllegalArgumentException("Must specify an input file.");

// Do something

if (!cmd.hasOption("CP"))
throw new IllegalArgumentException("Must specify an input file.");


// Do something

catch (Exception e)
System.out.println(e.getMEssage());






share|improve this answer






























    0














    take a look at this example.



    basically the args
    in your main method



    public static void main(String[] args) ... 


    args = the arguments you put after java -jar MyJar.jar
    for example -cp someFile.txt
    as a String[]: "-cp", "someFile.txt"






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55068351%2fhow-to-create-a-jar-file-with-execution-options%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      I think you probably need to check the Apache Commons-CLI, it allows you to do the things you describe above, also I give an example how it works, it give the way to specify a message for the usage of the arguments:



      https://commons.apache.org/proper/commons-cli/introduction.html



       Options options = new Options();

      options.addOption( "M", false,"Merge files request.")
      .addOption("CP", false,"Copy files from file.");

      CommandLineParser parser = new DefaultParser();
      try {

      CommandLine cmd = parser.parse(options, args);
      if (!cmd.hasOption("M"))
      throw new IllegalArgumentException("Must specify an input file.");

      // Do something

      if (!cmd.hasOption("CP"))
      throw new IllegalArgumentException("Must specify an input file.");


      // Do something

      catch (Exception e)
      System.out.println(e.getMEssage());






      share|improve this answer



























        1














        I think you probably need to check the Apache Commons-CLI, it allows you to do the things you describe above, also I give an example how it works, it give the way to specify a message for the usage of the arguments:



        https://commons.apache.org/proper/commons-cli/introduction.html



         Options options = new Options();

        options.addOption( "M", false,"Merge files request.")
        .addOption("CP", false,"Copy files from file.");

        CommandLineParser parser = new DefaultParser();
        try {

        CommandLine cmd = parser.parse(options, args);
        if (!cmd.hasOption("M"))
        throw new IllegalArgumentException("Must specify an input file.");

        // Do something

        if (!cmd.hasOption("CP"))
        throw new IllegalArgumentException("Must specify an input file.");


        // Do something

        catch (Exception e)
        System.out.println(e.getMEssage());






        share|improve this answer

























          1












          1








          1







          I think you probably need to check the Apache Commons-CLI, it allows you to do the things you describe above, also I give an example how it works, it give the way to specify a message for the usage of the arguments:



          https://commons.apache.org/proper/commons-cli/introduction.html



           Options options = new Options();

          options.addOption( "M", false,"Merge files request.")
          .addOption("CP", false,"Copy files from file.");

          CommandLineParser parser = new DefaultParser();
          try {

          CommandLine cmd = parser.parse(options, args);
          if (!cmd.hasOption("M"))
          throw new IllegalArgumentException("Must specify an input file.");

          // Do something

          if (!cmd.hasOption("CP"))
          throw new IllegalArgumentException("Must specify an input file.");


          // Do something

          catch (Exception e)
          System.out.println(e.getMEssage());






          share|improve this answer













          I think you probably need to check the Apache Commons-CLI, it allows you to do the things you describe above, also I give an example how it works, it give the way to specify a message for the usage of the arguments:



          https://commons.apache.org/proper/commons-cli/introduction.html



           Options options = new Options();

          options.addOption( "M", false,"Merge files request.")
          .addOption("CP", false,"Copy files from file.");

          CommandLineParser parser = new DefaultParser();
          try {

          CommandLine cmd = parser.parse(options, args);
          if (!cmd.hasOption("M"))
          throw new IllegalArgumentException("Must specify an input file.");

          // Do something

          if (!cmd.hasOption("CP"))
          throw new IllegalArgumentException("Must specify an input file.");


          // Do something

          catch (Exception e)
          System.out.println(e.getMEssage());







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 20 at 8:24









          EEstereoEEstereo

          665




          665























              0














              take a look at this example.



              basically the args
              in your main method



              public static void main(String[] args) ... 


              args = the arguments you put after java -jar MyJar.jar
              for example -cp someFile.txt
              as a String[]: "-cp", "someFile.txt"






              share|improve this answer



























                0














                take a look at this example.



                basically the args
                in your main method



                public static void main(String[] args) ... 


                args = the arguments you put after java -jar MyJar.jar
                for example -cp someFile.txt
                as a String[]: "-cp", "someFile.txt"






                share|improve this answer

























                  0












                  0








                  0







                  take a look at this example.



                  basically the args
                  in your main method



                  public static void main(String[] args) ... 


                  args = the arguments you put after java -jar MyJar.jar
                  for example -cp someFile.txt
                  as a String[]: "-cp", "someFile.txt"






                  share|improve this answer













                  take a look at this example.



                  basically the args
                  in your main method



                  public static void main(String[] args) ... 


                  args = the arguments you put after java -jar MyJar.jar
                  for example -cp someFile.txt
                  as a String[]: "-cp", "someFile.txt"







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 8 at 17:56









                  KeanuQKeanuQ

                  212




                  212



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55068351%2fhow-to-create-a-jar-file-with-execution-options%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

                      Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

                      Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved