File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Associate Certification (SCJA,OCAJ 5/6) and the fly likes What is the java -D command-line option good for? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Associate Certification (SCJA,OCAJ 5/6)
Bookmark "What is the java -D command-line option good for?" Watch "What is the java -D command-line option good for?" New topic
Author

What is the java -D command-line option good for?

Peter Heide
Ranch Hand

Joined: Nov 04, 2006
Posts: 31
One of the SCJA Exam Objectives is:

demonstrate the proper use of the "java" command including the command-line option -D

The java jdk documentation specifies:

-Dproperty=value
Set a system property value. If value is a string that contains spaces, you must enclose the string in double quotes:
java -Dfoo="some string" SomeClass

My question is: How can I access this property value in the code. Does someone have an example?


SCJA
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3285
    
    7
-D is an important switch that allows you to set environment properties.

You can call the following from anywhere in the code to read the properties given on the command line:

String value = System.getProperty("key", "defaultvalue");


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

A JVM runs with a number of system properties. You can configure system properties by using the -D option, pronounced with an upper case 'D'

All you have to do i suse the -D flag, and provide the system prperty name immediately following the D, and equals sign, and then the value to be assigned to the property. For example, to set the file.encoding property of the Java runtime to utf-8, you could set the following property:

java -Dfile.encoding=utf-8

You can then grab the value programatically as follows:

System.getProperty("file.encoding"); /*this method is overloaded, as per previous post*/

System geProperty JavaDoc

Don't confuse the -D switch with the -d switch. Both are tested on the SCJA exam, but both do different things. Casing is important!

Some of the default properties that are supposed to have already been set include: file.encoding, file.separator, java.home and java.version.


Cheers!

-Cameron McKenzie

[ November 05, 2006: Message edited by: Cameron W. McKenzie ]
[ November 05, 2006: Message edited by: Cameron W. McKenzie ]
Peter Heide
Ranch Hand

Joined: Nov 04, 2006
Posts: 31
I used the following class:



Then I tried:

javac hello.java
java -Dhello=world hello

The output was:

hello=world

So the solution of Paul and Cameron worked well.
a haksdjf
Greenhorn

Joined: Nov 22, 2009
Posts: 1
Peter Heide wrote:One of the SCJA Exam Objectives is:

demonstrate the proper use of the "java" command including the command-line option -D

The java jdk documentation specifies:

-Dproperty=value
Set a system property value. If value is a string that contains spaces, you must enclose the string in double quotes:
java -Dfoo="some string" SomeClass

My question is: How can I access this property value in the code. Does someone have an example?


Can someone let me know how I and specify multiple key/value pairs?

Thanks,
Olivier Fresse
Greenhorn

Joined: Dec 10, 2009
Posts: 1
a haksdjf wrote:

Can someone let me know how I and specify multiple key/value pairs?

Thanks,


Use several -D arguments



Basically, it's text. You can also do something like :



then you need to analyze the string key:val,key2:val2,key3:val3
to split it and get the key:val strings.
And you can split these again to get key and val.
String tokenizer is your friend Or regexp :-)

kunu patil
Greenhorn

Joined: Sep 24, 2007
Posts: 20
Hi

What are the ways to set system properties?

Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is the java -D command-line option good for?