Professional Documents
Culture Documents
i
Chapter 2
2.1 Introduction
• This chapter describes how your Java program can deal with its immediate sur-
roundings, with what we call the runtime environment.
• In one sense, everything you do in a Java program using almost any Java API
involves the environment. Here we focus more narrowly on things that directly
surround your program.
• HOME This variable holds the default path to the users home directory.
1. Problem: You want to get the value of environment variables from within your
Java program.
Solution: Use System.getenv().
1
Problem: WAP to get the value of environment variables
2. Problem: You want to read and display all environment variables from within
your Java program.
Solution:
• The no-argument form of the method System.getenv() returns all the en-
vironment variables, in the form of an immutable String Map.
• You can iterate through this map and access all the users settings or retrieve
multiple environment settings.
2
Discussion
• What is a property anyway? A property is just a name and value pair stored in a
java.util.Properties object.
• The System class has a static Properties member whose content is the merger of
operating system specifics (os.name , for example), system and user tailoring
(java.class.path).
• Note that the use of periods in these names (like os.arch , os.version, java.class.path,
and java.lang.version ) makes it look as though there is a hierarchical relation-
ship similar to that for class names.
3
2.4 Learning about the Current JDK Release
Problem
You need to write code that looks at the current JDK release (e.g., to see what release
of Java you are running under).
Solution
Use System.getProperty() with an argument of java.specification.version.
Problem:WAP to print the current JDK release
• These include such variables as the filename separator. Everybody on Unix knows
that the filename separator is a slash character (/) and that a backward slash, or
backslash (\), is an escape character.
• The file separator (and also the PATH separator), in java.io.File class makes
available some static variables containing this information.
4
Table 2.1: File properties
5
Problem:WAP to print null device of your system.
2.7 Assignment
• Write a program which read a path to a file as a string and modify the string ac-
cording to system specify file seperator and display it.
• Write a program which ask the user to enter a jar file name and check whether
the jar file is install or not.
• Write a program which read a user name from user and check is it the current
user or not