Java 17 Recipes


Download 3.2 Mb.
Pdf ko'rish
bet31/245
Sana02.06.2024
Hajmi3.2 Mb.
#1839910
1   ...   27   28   29   30   31   32   33   34   ...   245
Bog'liq
Java 17 Recipes

Listing 1-12. Iterating Through a Map of Environment Variables
package org.java17recipes.chapter1.recipe1_13;
import java.util.Map;
public class ReadAllEnvVariables {
public static void main(String[] args){
if(args.length > 0){
String value = System.getenv(args[0]);
if (value != null) {
System.out.println(args[0].toUpperCase() + " = " + value);
} else {
System.out.println("No such environment variable exists");
}
} else {
Map vars = System.getenv();
for(String var : vars.keySet()){
System.out.println(var + " = " + vars.get(var));
}
}
}
}
 How It Works
The System class contains many different utilities that can aid in application 
development. One of those is the getenv() method, which returns a value for a given 
system environment variable.
You can also return the values from all environment variables in which case those 
values are stored in a map. A map is a collection of name/value pairs. Chapter 
7
 provides 
additional information about maps.
ChApteR 1 GettInG StARted wIth JAvA 17


47
Note the method invoked to obtain environment variable values in Listings 
1-11

and 
1-12
. It’s been overloaded to handle both cases shown in the solution. Pass the name 
of a variable as a string if you want to obtain that variable’s value. Pass no argument at all 
to get back the names and values of all variables that are currently set.
1-14. Summary
This chapter includes recipes that allow you to quickly start working with Java. It 
covered the installation of the OpenJDK to the configuration and use of the Eclipse 
IDE. The chapter also covered basics such as declaring variables, compiling code, and 
documentation. The rest of this book dives deeper into each of the different areas of the 
Java language, covering a variety of topics from beginner to expert. Refer to this chapter 
for configuration specifics as you work through the examples in the rest of the book.
ChApteR 1 GettInG StARted wIth JAvA 17


49
© Josh Juneau, Luciano Manelli 2022 
J. Juneau and L. Manelli, Java 17 Recipes
https://doi.org/10.1007/978-1-4842-7963-2_2

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   27   28   29   30   31   32   33   34   ...   245




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling