In this section you'll create a basic Java console app.
-
Open your command-line interface (CLI) in a directory where you want to create the project. Run the following command to create a new Gradle project.
gradle init --dsl groovy --test-framework junit --type java-application --project-name graphtutorial --package graphtutorial
-
Once the project is created, verify that it works by running the following command to run the app in your CLI.
./gradlew --console plain run
If it works, the app should output
Hello World..
Before moving on, add some additional dependencies that you will use later.
- Microsoft Authentication Library (MSAL) for Java to authenticate the user and acquire access tokens.
- Microsoft Graph SDK for Java to make calls to the Microsoft Graph.
- SLF4J NOP Binding to suppress logging from MSAL.
-
Open ./build.gradle. Update the
dependenciessection to add those dependencies.:::code language="gradle" source="../demo/graphtutorial/build.gradle" id="DependenciesSnippet" highlight="7-9":::
-
Add the following to the end of ./build.gradle.
:::code language="gradle" source="../demo/graphtutorial/build.gradle" id="StandardInputSnippet":::
The next time you build the project, Gradle will download those dependencies.
-
Open the ./src/main/java/graphtutorial/App.java file and replace its contents with the following.
package graphtutorial; import java.util.InputMismatchException; import java.util.Scanner; /** * Graph Tutorial * */ public class App { public static void main(String[] args) { System.out.println("Java Graph Tutorial"); System.out.println(); Scanner input = new Scanner(System.in); int choice = -1; while (choice != 0) { System.out.println("Please choose one of the following options:"); System.out.println("0. Exit"); System.out.println("1. Display access token"); System.out.println("2. List calendar events"); try { choice = input.nextInt(); } catch (InputMismatchException ex) { // Skip over non-integer input input.nextLine(); } // Process user choice switch(choice) { case 0: // Exit the program System.out.println("Goodbye..."); break; case 1: // Display access token break; case 2: // List the calendar break; default: System.out.println("Invalid choice"); } } input.close(); } }
This implements a basic menu and reads the user's choice from the command line.