The System object is a core Java object intended for interacting with the environment surrounding your Java application.
System object interacts with the environment, which may be different depending on the platform you are working on. So, the result of using a System method or property on one platform may not be consistent across all platforms.
Getting Environment Variables
Use System.getenv(String environVariable) method.
Example: String envPath = System.getenv("PATH");
Without an argument,
Example:
Code for Listing all the environment variables.
System object interacts with the environment, which may be different depending on the platform you are working on. So, the result of using a System method or property on one platform may not be consistent across all platforms.
Getting Environment Variables
Use System.getenv(String environVariable) method.
Example: String envPath = System.getenv("PATH");
Without an argument,
getenv
returns a read-only instance of java.util.Map
, where the map keys are the environment variable names, and the map values are the environment variable values.Example:
Code for Listing all the environment variables.
import java.util.Map;
public class EnvMap {
public static void main (String[] args) {
Map<String, String> env = System.getenv();
for (String envName : env.keySet()) {
System.out.format("%s=%s%n", envName, env.get(envName));
}
}
}
Platform Dependency Issues
There are many subtle differences between the way environment variables are implemented on different systems. For example, Windows ignores case in environment variable names, while UNIX does not. The way environment variables are used also varies. For example, Windows provides the user name in an environment variable calledUSERNAME
, while UNIX implementations might provide the user name inUSER
,LOGNAME
, or both.To maximize portability, never refer to an environment variable when the same value is available in a system property. For example, if the operating system provides a user name, it will always be available in the system propertyuser.name
.Command-Line ArgumentsA Java application can accept any number of arguments from the command line.The user enters command-line arguments when invoking the application and specifies them after the name of the class to be run. For example, suppose a Java application calledSort
sorts lines in a file. To sort the data in a file namedfriends.txt
, a user would enter:java Sort friends.txtThe space character separates command-line arguments.Parsing Numeric Command-Line ArgumentsIf an application needs to support a numeric command-line argument, it must convert aString
argument that represents a number, such as "34", to a numeric value.int firstArg; if (args.length > 0) { try { firstArg = Integer.parseInt(args[0]); } catch (NumberFormatException e) { System.err.println("Argument" + " must be an integer"); System.exit(1); } }All of the
Number
classes —Integer
,Float
,Double
, and so on — haveparseXXX
methods that convert aString
representing a number to an object of their type.System Properties
TheSystem
class maintains aProperties
object that describes the configuration of the current working environment.
Key | Meaning |
---|---|
"file.separator" | Character that separates components of a file path. This is "/ " on UNIX and "\ " on Windows. |
"java.class.path" | Path used to find directories and JAR archives containing class files. Elements of the class path are separated by a platform-specific character specified in the path.separator property. |
"java.home" | Installation directory for Java Runtime Environment (JRE) |
"java.vendor" | JRE vendor name |
"java.vendor.url" | JRE vendor URL |
"java.version" | JRE version number |
"line.separator" | Sequence used by operating system to separate lines in text files |
"os.arch" | Operating system architecture |
"os.name" | Operating system name |
"os.version" | Operating system version |
"path.separator" | Path separator character used in java.class.path |
"user.dir" | User working directory |
"user.home" | User home directory |
"user.name" | User account name |
The System
class has two methods used to read system properties : getProperty
and getProperties
.
No comments:
Post a Comment