You are on page 1of 7

Contents

2 Interacting with the Environment 1


2.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
2.2 Getting Environment Variables . . . . . . . . . . . . . . . . . . . . . . . 1
2.3 Getting Information from System Properties . . . . . . . . . . . . . . . . 2
2.4 Learning about the Current JDK Release . . . . . . . . . . . . . . . . . . 4
2.5 Dealing with Operating SystemDependent Variations . . . . . . . . . . . 4
2.6 NULL Device . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.7 Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

i
Chapter 2

Interacting with the Environment

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.

2.2 Getting Environment Variables


What are Environment Variables?
Environment variables are global system variables accessible by all the processes/users
running under the Operating System (OS), such as Windows, macOS and Linux. Envi-
ronment variables are useful to store system-wide values, for examples,

• PATH: The most frequently-used environment variable, which stores a list of


directories to search for executable programs.
This variable contains a list of directories in which our system looks for files. It
separates directories by a (:) colon.

• USERNAME This variable holds the username.

• HOME This variable holds the default path to the users home directory.

• ENV This variable displays all the environment variable.

• EDITOR This variable contains the path to the specified editor.

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

public class EnvVariables {


public static void main ( String [] args ) {
// Get path
String path = System . getenv ( " PATH " );
System . out . println ( path ) ;
// Get username
String username = System . getenv ( " USERNAME " );
System . out . println ( username ) ;
}
}
Output :
/ usr / local / sbin :/ usr / local / bin :/ usr / sbin :/ usr / bin :
/ sbin :/ bin :/ usr / games :/ usr / local / games :/ snap / bin
nikunja

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.

Problem:WAP to read and display all environment variables.

import java . util .*;


public class AllEnvVar {
public static void main ( String [] args ) {
// Env variable is ( name & value ) ( String String )
// key and value pair in map
Map < String , String > map = System . getenv () ;
// Traverse or iterate map
for ( Map . Entry < String , String > entry :
map . entrySet () ) {
System . out . println ( " Variable
Name =" + entry . getKey () ) ;
System . out . println ( " Value =" + entry . getValue () ) ;
}
}
}

2.3 Getting Information from System Properties


Problem: You need to get information from the system properties.
Solution: Use System.getProperty() or System.getProperties().

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.Properties object controls and describes the Java runtime.

• 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.

Problem:WAP to print OS name, OS version, OS architecture, and Java class


path.

public class SysProperties {


public static void main ( String [] args ) {
// Print the os name
String osname = System . getProperty ( " os . name " );
System . out . println ( osname ) ;
// Get the installed version of OS in your system .
String osversion = System . getProperty ( " os . version " );
System . out . println ( osversion ) ;
// Get the OS architecture of OS in your system .
String osarch = System . getProperty ( " os . arch " );
System . out . println ( osarch ) ;
// Print the java class path
System . out . println ( System . getProperty ( " java . class . path ") );
}
}
Output :
Linux
5.0.0 -38 - generic
amd64
/ home / nikunja / Dropbox / CSW1 / eclipsse - workspace_CSW1_G / Chap00 / bin

Problem:WAP to print all system properties.

import java . util .*;


public class AllSysProp {
public static void main ( String [] args ) {
Properties obj = System . getProperties () ;
obj . list ( System . out ) ;
}
}

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

public class JDKRelease {


public static void main ( String [] args ) {
String jdkver =
System . getProperty ( " java . specification . version " );
System . out . println ( jdkver ) ;
}
}
Output :
11

2.5 Dealing with Operating System Dependent Vari-


ations
Problem
You need to write code that adapts to the underlying operating system.
Solution
You can use System.Properties to find out the operating system, and various features
in the File class to find out some platform-dependent features.
Discussion

• Though Java is designed to be portable, some things arent.

• 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

Name Type Meaning

separator static String The system-dependent filename separator charac-


ter (e.g., / or \).

separatorChar static char The system-dependent filename separator charac-


ter (e.g., / or \).

pathSeparator static String The system-dependent path separator character,


represented as a string for convenience.

pathSeparatorChar static char The system-dependent path separator character.

Problem:WAP to print file separator and path separator

public class FileSeparatorDemo {


public static void main ( String [] args ) {
// Get file separator
String filesep = System . getProperty ( " file . separator " );
String filesep1 = java . io . File . separator ;
System . out . println ( " File Separator " + filesep +"
"+ filesep1 ) ;
// Get path separator
String pathsep = System . getProperty ( " path . separator " );
String pathsep1 = java . io . File . pathSeparator ;
System . out . println ( " Path Separator " + pathsep +"
"+ pathsep1 ) ;
// File separator read as a char
char filesep2 = java . io . File . separatorChar ;
System . out . println ( " File Sep as Char " + filesep2 ) ;
// Path separator read as a char
char pathsep2 = java . io . File . pathSeparatorChar ;
System . out . println ( " Path Sep as Char " + pathsep2 ) ;
}
}
Output :
File Separator / /
Path Separator : :
File Sep as Char /
Path Sep as Char :

2.6 NULL Device


Some OSes, for example, provide a mechanism called the null device that can be used
to discard output (typically used for timing purposes).

5
Problem:WAP to print null device of your system.

import java . io . File ;


public class TestNullDevice
{
final static String UNIX_NULL_DEV = "/ dev / null ";
final static String WINDOWS_NULL_DEV = " NUL : ";
final static String FAKE_NULL_DEV = " jnk ";
public static String getNullDev ()
{
String sys = System . getProperty ( " os . name ") ;
System . out . println ( sys ) ;
if ( new File ( UNIX_NULL_DEV ) . exists () )
return UNIX_NULL_DEV ;
if ( sys == null )
return FAKE_NULL_DEV ;
if ( sys . startsWith ( " Windows ") )
return WINDOWS_NULL_DEV ;
return FAKE_NULL_DEV ;
}
public static void main ( String arhs [])
{
String devNull = getNullDev () ;
System . out . println ( devNull ) ;
}
}
Output :
Linux
/ dev / null

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

You might also like