Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
21 views
10 pages
Z 12
Uploaded by
tresbonarchitecte
AI-enhanced title
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Download
Save
Save z12 For Later
Share
0%
0% found this document useful, undefined
0%
, undefined
Print
Embed
Report
0 ratings
0% found this document useful (0 votes)
21 views
10 pages
Z 12
Uploaded by
tresbonarchitecte
AI-enhanced title
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Carousel Previous
Carousel Next
Download
Save
Save z12 For Later
Share
0%
0% found this document useful, undefined
0%
, undefined
Print
Embed
Report
Download
Save z12 For Later
You are on page 1
/ 10
Search
Fullscreen
0412023 20.11 Command-Line Arguments in Java | Baeloung “ Command-Line Arguments in Java Last updated: June 23.2022 |. written by atta Fojér htips/ wanwbaeldung.com/author/atla-folr) Y thitos//adsfreestarcom/? Java (https://wwwbaeldung.com/category/java) + Get started with Spring 5 and Spring Boot 2, through the Learn Spring course: > CHECK OUT THE COURSE (/s-course-start) mrepietty | room woman = Register today: Harness the Power of Your Identity Data Mp ronste (nttps//ads freestarcom/? utm_campaign-branding&ulm_medium-banner&utm_source-baeldung.comutm_content-baeldung_incontent 2 y 1. Introduction Its quite common to run applications from the command-line using arguments. Especially on the server- side. Usually, we don't want the application to do the same thing on every run: we want to configure its behavior some way. In this shor tutorial, wel explore how can we handle command-line arguments in Java - hitps:lvww-baeldung.comjava-command:ine-arguments anooart2029 20.11 Command-Line Azgumenis in Java | Baelsung 2. Accessing Command-Line Arguments in Java ‘Since the main method is the entry point of a Java application, the JVM passes the command-line arguments through its arguments. The traditional way is to use a String array: I} handle arguments 0 ; However, Java § introduced varargs, which are arrays in sheep's clothing, Therefore, we can define our main with a String vararg: (https//ads freestarcom/? public static votd main(String... ares) < 71 mance sraumerts Oo ) Theyre identical, therefore choosing between them is entirely up to personal taste and preference. ‘The method parameter of the main method contains the command-line arguments in the same order we passed at execution, If we want lo access how much arguments did we gel, we only have to check the length of the array. For example. we can print the number of arguments and their value on the standard output: ‘System.out.printla("Argument count: " + args. length); O > Note that in some languages, the first argument willbe the name of the application. On the other hand, in Java, this array contains only the arguments. 3. How to Pass Command-Line Arguments Now that we have an application that handles command-line arguments, were eager to try it Lets see what ‘options we have, ldung.comjava-commanet-jine-arguments 20oart2023 20:11 Command-Line Arguments in Java | Baeloung “ 3.1. Command Line ‘The most obvious way is the command-line, Let's assume we already compiled the class com baeldung.commandlinearguments CliExample with our main method in it ‘Then we can run itwith the following command: It produces the following output: Now. we can pass arguments after the class name: ‘And the output is: Argument count: 2 Argument 0: Hello Argument 1: Wortd! Usually, we publish our application as ajar file, not as a bunch of class files. Let's say, we packaged it in cli-examplejar, and we set com baeldung commandlinearguments CliExample as the main class. Now we can run it without arguments the following way. the java “jar cli-exanple-jar Or with arguments: ¥ htipsifwnew.baeldung,com/ava-commandsiine-arguments anooart2023 20.11 Command-Line Arguments in Java | Baeleung jeva Jar ont wile cur Heltd)Worldt oO Argument count: 2 Argument 02 Hello Argument 1: Kortd! Note, that Java will treat every argument we pass after the class name or the jar file name as the arguments of our application. Therefore. everything we pass bofore thal are arguments for the JVM itself 3.2. Eclipse While we're working on our application, welll want to check iit works the way we want. In Eclipse, we can run applications with the help of run configurations, For example, a run configuration defines which JVM to use, what is the entry point, the classpath, and so on. And of course, we can specify command-line arguments. ‘The easiest way to create an appropriate run configuration is to right-click on our main method, then choose Run As > Java Application from the context menu ns ec x oh OO ROH OS eRe Oia HP Hw BM b+ Feo er oe Sari | oop 7 | oseain =i i: oe ccrereeentine yee B Of ree et tiem tw ne a Syienavto cos a % Open Declaration a tel » Open ype Hlerwchy # = Gmesieny cho cone one onus . tame eter pee, 5 (/wp-content/uploads/2019/09/eclipse-run png) ‘With this, we instantly run our application with settings that honor our project settings. htipsifwnew.baeldung,com/ava-commandsiine-arguments ano0412023 20.11 Command-Line Arguments in Java | Baeloung “ (https//ads freestarcom/? To provide arguments, we should then edit that run configuration. We can do it through the Run > Run Configurations. menu option. Here, we should click the Arguments tab ane fil the Program arguments textbox @ tun Contato o x un lee appicston ‘reste, manage, and run configurations Cr) SB 1S [frame |citsmpe peer (© Waa |o-Argunentt ph RE Ganpatl|y Saure] I Eionnen| El Gemnen) BPemhpe] BB. Apacte Tomcat a © ipeesoataton ranma FF ceneriesener Bene Senerstral Launch ff Gallon BL EE review (vee. deve Api [Fe appicnion Yitorguments (2 chtzample yun FF Nine Pagan Te Bp Lamen raup a Maven Bud @ Notes dppleatin & O5GiFamevere Working decor @ Seng foot 9p Detaie 8 sping toot Denoo cient || © ibe. obeebeeeneel omer Waspces | [ Beaten | [i ne al a ® eo (/wwp-content/uploads/2019/09/eclipse-configure.png) Hitting Run will run the application and pass the arguments we just entered. 3.3. Intellid IntelliJ uses a similar process to run applications. It calls these options simply as configurations, First, we need to right-click on the main method, then choose Run ‘CliExamplemain0 htipsifwnew.baeldung,com/ava-commandsiine-arguments 50oart2023 20:11 Command-Line Arguments in Java | Baeloung B -sejatine eet pedi elepgsparns-acminiconibneingonmaainerginewACianpejeainaal=— OX Ble fi Vow signe Code Anas Beate BUD fun Too’ VCS Whfow Hep CiBanpejoe ———— ” 2 pecags cn dng cman 4 > pbc ane Citi ( i Spe bo plies ji, a ‘ Meant prin) mas > 4 for tine 8 coprtdeence antic Psion ot St ae : ; kent cotiney ' ‘Column Selection Mode Alt Shiteinsert Bene A jake recompile sinieje cost Denis Cteanpe msl) 1G fun caxamptemany wencoueaye 1B eit ciple msin. Show leer 1 opera Temi Lec stony > > compara cipoas Citamele > mend ieuceed Biiemmi = 61000 0 cnet Centos a 508 GF UT. Fai (/wwp-content/uploads/2019/09/intellj-runpng) This will run our program, but it will also add it to the Run list for further configuration, So, then to configure arguments, we should choose Run > Edi Configurations. and edit the Program arguments textbox Bintan x errr = Coe Conon 2s a Pewee [nde (mining sneer 5 I a ee cement nati (RTE z con ager Pe ® Peas a zi ans aa Ca=ape gaa ° (a es (/wp-content/uploads/2019/09/intelli-configure-1024x646.png) After that. we should hit OK and rerun our application for example with the run button inthe toolkar htipsifwnew.baeldung,com/ava-commandsiine-arguments enooart2023 20:11 3.4. NetBrens “ Command-Line Arguments in Java | Baeloung NotBeans also falls into line with its running and configuration processes. \We should run our application first by right-clicking on the main method and choosing Run File: © cocina iach eo =p s AGES 6 poe VOTH )-i-O: HiacG 2 [emote * - — va alr @5 8 Were Feviaaioaina # Bp mone om sens comatersnaens: 7 fp] 3) mets cies canes « Se public static vosd madmeancinat arma ¢ als Sertenrotrpeiord Reve : aj: mcs oe ee || | Cattewely pe rate cms lo oy mom rn Tipjelnebatot Gi eee Pa 5 Bone ae = oa ine htipsifwnew.baeldung,com/ava-commandsiine-arguments (wp-content/uploads/2019/09/netbeans-run png) Like before. this creates a run configuration and runs the program. Next, we have to configure the arguments in that run configuration, We can do that by choosing Run > Set Project Configuration > Customize. Then we should Run on the left anc fil the Arguments text field m0oart2028 20.11 Command-Line Arguments in Java | Baeloung Crenthoe = seni yy x gies Ge cote saasowiaie. ° cmoe Matrathee = a Hos -— es sto ae wap ea (ep ome) Vwp-content/uploads/2019/09/netbeans-configurepng) ‘After that. we should hit OK and start the application, 4. Third-Party Libraries Manual handling of the command-line arguments i straightforward in simple scenarios. However, as ‘our requirements become more and more complex, so does our code. Therefore. if we want to create an application with multiple command-line options, it would be easier to use a third-party library. Fortunately, therere a plethora of those libraries which support most use cases, Two popular examples are Picoeli /java-picocli-create-command-line-program) and Spring Shell (/spring-shell-cli. 5. Conclusion It's always a good idea to make your application's behavior configurable. In this article, we saw how to do that using command-line arguments. Additionally, we covered various ways to pass those arguments, ‘As usual, the examples are available over on GitHub (httos///github.com/eugenp/tutorials/tree/master/core-java-modules/core-java-lang-2). hitps:lvww-baeldung.comjava-command:ine-arguments anooart2023 20:11 Command-Line Arguments in Java | Baeldung ” Get started with Spring 5 and Spring Boot 2, through the Learn Spring course; >> CHECK OUT THE COURSE (/s-course-end) Learning to build your API with Spring? Download the E-book (/rest-api-spring-guide) 2. COMMENT HO Oloest View Comments ‘Comments are closed on this article! htipsifwnew.baeldung,com/ava-commandsiine-arguments ano0412023 20.11 Command-Line Arguments in Java | Baeloung ‘COURSES, [ALL COURSES (aL. COURSES) ALL BULK TEAM COURSES (/ALL-SULK-TEAVACOURSES) SERIES JACKSON JSON TUTORIAL JACKSON ‘SPRING SERSIGTENCE TUTORIAL /PERSISTENCE-WITH-SPRINGHSERIES) ‘ABOUT (OUR PaRINERS {ZPARTNERS) COMPANY INFO {/8AELOUNG-COMPANY=INFO} ipsa baeldung,comijava-commanci son0
You might also like
Mandlineargument
PDF
No ratings yet
Mandlineargument
18 pages
6 Commandarguments
PDF
No ratings yet
6 Commandarguments
18 pages
ITC C106 Lecture - Command Line Arguments
PDF
No ratings yet
ITC C106 Lecture - Command Line Arguments
16 pages
Command Line Arguments
PDF
No ratings yet
Command Line Arguments
5 pages
Command Line Arguments in Java Tutorial
PDF
No ratings yet
Command Line Arguments in Java Tutorial
16 pages
Command Line Argument in Java
PDF
No ratings yet
Command Line Argument in Java
3 pages
Command Line Arguments Java
PDF
No ratings yet
Command Line Arguments Java
12 pages
Simple Example of Command-Line Argument in Java
PDF
0% (1)
Simple Example of Command-Line Argument in Java
2 pages
Simple Example of Command-Line Argument in Java
PDF
No ratings yet
Simple Example of Command-Line Argument in Java
2 pages
Java Naming Conventions
PDF
No ratings yet
Java Naming Conventions
9 pages
Command Line Arguments
PDF
No ratings yet
Command Line Arguments
2 pages
1 - Intro+CommandLevelPrograming - Java
PDF
No ratings yet
1 - Intro+CommandLevelPrograming - Java
32 pages
BSCS 208 - AOOP - Lecture 3 - Simple Class Templates
PDF
No ratings yet
BSCS 208 - AOOP - Lecture 3 - Simple Class Templates
15 pages
Lab 03
PDF
No ratings yet
Lab 03
7 pages
CMD Line Arg
PDF
No ratings yet
CMD Line Arg
6 pages
Unit 1 - Prog, Commandline - PNR
PDF
No ratings yet
Unit 1 - Prog, Commandline - PNR
46 pages
Java p3
PDF
No ratings yet
Java p3
3 pages
Command Line Arguments in JAVA
PDF
No ratings yet
Command Line Arguments in JAVA
2 pages
Java Lec-22 Command Line Arguments.488d7fb
PDF
No ratings yet
Java Lec-22 Command Line Arguments.488d7fb
4 pages
Command Line Arguments
PDF
No ratings yet
Command Line Arguments
5 pages
Command Line Arguments in C - C++
PDF
100% (1)
Command Line Arguments in C - C++
2 pages
Command Line Arguments
PDF
No ratings yet
Command Line Arguments
1 page
3.7 Command-Line Arguments
PDF
No ratings yet
3.7 Command-Line Arguments
18 pages
WINSEM2018-19 STS3004 SS SJTG16 VL2018195000254 Reference Material I Module 1 Session - 4-CLA
PDF
No ratings yet
WINSEM2018-19 STS3004 SS SJTG16 VL2018195000254 Reference Material I Module 1 Session - 4-CLA
22 pages
Command Line ARguments in Eclipse
PDF
No ratings yet
Command Line ARguments in Eclipse
5 pages
Java
PDF
No ratings yet
Java
106 pages
Command Line Arguments in C
PDF
No ratings yet
Command Line Arguments in C
4 pages
OOPM Lab Manual 2014 Updated
PDF
No ratings yet
OOPM Lab Manual 2014 Updated
47 pages
Lab1 6
PDF
No ratings yet
Lab1 6
67 pages
Main Method Lyst3867
PDF
No ratings yet
Main Method Lyst3867
7 pages
Command Line
PDF
No ratings yet
Command Line
16 pages
Manipal University Jaipur First Semester 2017-18 Object-Oriented Programming (CS 1304) LAB-1 (Introduction To Eclipse IDE and Java Basics)
PDF
No ratings yet
Manipal University Jaipur First Semester 2017-18 Object-Oriented Programming (CS 1304) LAB-1 (Introduction To Eclipse IDE and Java Basics)
12 pages
Command Line Arguments
PDF
No ratings yet
Command Line Arguments
2 pages
10 Command Line Arguments 2015
PDF
No ratings yet
10 Command Line Arguments 2015
2 pages
Argc Argv
PDF
No ratings yet
Argc Argv
3 pages
Command Line Arguments
PDF
No ratings yet
Command Line Arguments
5 pages
LAB - 01 (Intro To Eclipse IDE) PDF
PDF
No ratings yet
LAB - 01 (Intro To Eclipse IDE) PDF
12 pages
Ass-10 - CMD Line Arguments
PDF
No ratings yet
Ass-10 - CMD Line Arguments
1 page
Java Tutorial 8
PDF
No ratings yet
Java Tutorial 8
3 pages
JAVA Oracle Notes
PDF
No ratings yet
JAVA Oracle Notes
2 pages
Apache Commons CLI Tutorial
PDF
No ratings yet
Apache Commons CLI Tutorial
12 pages
Processing Command-Line Arguments
PDF
No ratings yet
Processing Command-Line Arguments
4 pages
FALLSEM2022-23 STS3201 SS VL2022230100107 Reference Material I 03-08-2022 L7-Basic IO 1.2 7
PDF
No ratings yet
FALLSEM2022-23 STS3201 SS VL2022230100107 Reference Material I 03-08-2022 L7-Basic IO 1.2 7
27 pages
03 Basic Syntax
PDF
No ratings yet
03 Basic Syntax
23 pages
Java Basic Syntax
PDF
No ratings yet
Java Basic Syntax
23 pages
Exercises Basic Syntax PDF
PDF
No ratings yet
Exercises Basic Syntax PDF
1 page
Hands-On Assignments For Language Basics: Complete The Below Hands-On Assignments Before Proceeding With The Next Topic
PDF
No ratings yet
Hands-On Assignments For Language Basics: Complete The Below Hands-On Assignments Before Proceeding With The Next Topic
2 pages
Java 9
PDF
No ratings yet
Java 9
4 pages
Java Lab Manual
PDF
100% (1)
Java Lab Manual
27 pages
Exercises Basic Syntax PDF
PDF
No ratings yet
Exercises Basic Syntax PDF
1 page
Homework #9 Solution (Directly Helps With Your Final)
PDF
No ratings yet
Homework #9 Solution (Directly Helps With Your Final)
10 pages
LanguageBasics
PDF
No ratings yet
LanguageBasics
1 page
Java Fisrt Program
PDF
No ratings yet
Java Fisrt Program
6 pages
Java Basic
PDF
No ratings yet
Java Basic
189 pages
Java Input and Output CSV File
PDF
No ratings yet
Java Input and Output CSV File
5 pages
Z 31
PDF
No ratings yet
Z 31
7 pages
Z 27
PDF
No ratings yet
Z 27
7 pages
Z 30
PDF
No ratings yet
Z 30
7 pages
Z 32
PDF
No ratings yet
Z 32
9 pages
Z34
PDF
No ratings yet
Z34
7 pages
Z 26
PDF
No ratings yet
Z 26
7 pages
Z 16
PDF
No ratings yet
Z 16
6 pages
Z 24
PDF
No ratings yet
Z 24
7 pages
Z 18
PDF
No ratings yet
Z 18
7 pages
Z 17
PDF
No ratings yet
Z 17
7 pages
X 3
PDF
No ratings yet
X 3
5 pages
Z 19
PDF
No ratings yet
Z 19
6 pages
B 1
PDF
No ratings yet
B 1
9 pages
B 5
PDF
No ratings yet
B 5
6 pages
Z 9
PDF
No ratings yet
Z 9
8 pages
Z 14
PDF
No ratings yet
Z 14
9 pages
B 9
PDF
No ratings yet
B 9
7 pages
B 8
PDF
No ratings yet
B 8
6 pages
B 2
PDF
No ratings yet
B 2
8 pages
S 6
PDF
No ratings yet
S 6
11 pages
S 8
PDF
No ratings yet
S 8
7 pages
Sa
PDF
No ratings yet
Sa
8 pages
S 4
PDF
No ratings yet
S 4
8 pages
P 18
PDF
No ratings yet
P 18
11 pages
B 3
PDF
No ratings yet
B 3
12 pages
S 3
PDF
No ratings yet
S 3
12 pages
B 2
PDF
No ratings yet
B 2
12 pages
B 5
PDF
No ratings yet
B 5
12 pages
P 14
PDF
No ratings yet
P 14
11 pages
P 16
PDF
No ratings yet
P 16
19 pages