• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Enthuware Software Support
Posts: 4803
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
-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");
 
author and cow tipper
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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,
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 :-)

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

What are the ways to set system properties?

Thanks
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic