The way you run your application in Java is by invoking java followed by the class name (without the .class extension).
1 | java MyAwesomeApp |
Here you are not passing any arguments (or parameters) to the application.
The only entry-point of any Java application is through this
1 | public static void main(String[] args) |
The method signature indicates that it accepts arguments.
Our Awesome App Codes
1 2 3 4 5 6 7 8 9 10 11 | package com.karlsangabriel.javarun; public class MyAwesomeApp { public static void main(String[] args) { if (args.length > 0) { System.out.println("Received:" + args[0]); } System.out.println("Awesome!"); } } |
Without passing any argument, our application displays
1 | Awesome! |
Passing Arguments
Let’s pass a one-word string and see what happens
1 | java MyAwesomeApp super |
This displays
1 2 | Received:super Awesome! |
Now let’s pass a string with more than one word
1 | java MyAwesomeApp this is super! |
This outputs
1 2 | Received:this Awesome! |
Clearly not the input we would want.
To display all the words in this is super! enclosed them in quotes. Like this one,
1 | java MyAwesomeApp "this is super!" |
This outputs
1 2 | Received:this is super! Awesome! |