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;
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
add a comment |
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
1
Have you thought about using theargs
inpublic 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 choosecp
orM
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
add a comment |
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
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
java linux command-line jar executable-jar
asked Mar 8 at 17:42
EEsparaquiaEEsparaquia
386
386
1
Have you thought about using theargs
inpublic 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 choosecp
orM
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
add a comment |
1
Have you thought about using theargs
inpublic 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 choosecp
orM
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
add a comment |
2 Answers
2
active
oldest
votes
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());
add a comment |
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"
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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());
add a comment |
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());
add a comment |
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());
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());
answered Mar 20 at 8:24
EEstereoEEstereo
665
665
add a comment |
add a comment |
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"
add a comment |
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"
add a comment |
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"
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"
answered Mar 8 at 17:56
KeanuQKeanuQ
212
212
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
1
Have you thought about using the
args
inpublic 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
orM
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