• Post Reply Bookmark Topic Watch Topic
  • New Topic

listing the system properties with a linebreak  RSS feed

 
David James Thompson
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I am quite new to java and trying to print all the system properties with the following code:

This prints all the properties as expected but I am wanting to put each property on a new line and am struggling a bit. Obviously I will need some sort of for loop etc but im just not sure how to do it. Is it something along the lines of:

What my understanding of what the above is doing is checking how many properties there are an looping round for each property that exists, so all I need to do is get the property 1 at a time however I am not sure where to get the property value to input into the System.getProperty(); method. Any help would be appreciated.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.getProperties returns a Properties instance which has a propertyNames method which returns an Enumeration instance. You need to loop through the Enumeration - the Javadoc includes an example of how to do this.
 
Paul Clapham
Sheriff
Posts: 22823
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried the "list" method of Properties?


 
David James Thompson
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

Thanks for the responses. I am still having a little trouble with this though. I have tried:


but this just puts it in an endless loop. Im obviously just not getting it. Can anyone direct me further please?

Oh an sorry Bear Bibeault, I was not aware my name breached the rules and I didnt notice the first message, thanks for bringing this to my attention.

 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.getProperties().propertyNames() creates a new Enumeration, so every time through the loop you create a new Enumeration and get the first element.
Assign the value returned from System.getProperties().propertyNames() to a variable before the loop and then call the hasMoreElements and nextElement methods on that variable.
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can probably try the following:



This code uses the Properties class from the java.util package as a reference to hold the system properties. To create an object with the required information and assign it to this reference we call the static getProperties() method from the System class. Next call the list method on this object and pass in a PrintStream object to print the properties, in this case we use the default PrintStream which will be directed to the System console.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rico Felix wrote:You can probably try the following:

Does that separate the properties with line breaks ?
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:Does that separate the properties with line breaks ?


I suppose...

David James Thompson wrote:I am wanting to put each property on a new line


This was the request... The following shows the output:

Microsoft Windows [Version 6.3.9600]
(c) 2013 Microsoft Corporation. All rights reserved.

C:\Users\Rico Antonio Felix>cd Desktop

C:\Users\Rico Antonio Felix\Desktop>java ListSystemProperties
-- listing properties --
java.runtime.name=Java(TM) SE Runtime Environment
sun.boot.library.path=C:\Program Files\Java\jre8\bin
java.vm.version=25.0-b70
java.vm.vendor=Oracle Corporation
java.vendor.url=http://java.oracle.com/
path.separator=;
java.vm.name=Java HotSpot(TM) 64-Bit Server VM
file.encoding.pkg=sun.io
user.script=
user.country=US
sun.java.launcher=SUN_STANDARD
sun.os.patch.level=
java.vm.specification.name=Java Virtual Machine Specification
user.dir=C:\Users\Rico Antonio Felix\Desktop
java.runtime.version=1.8.0-b132
java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment
java.endorsed.dirs=C:\Program Files\Java\jre8\lib\endorsed
os.arch=amd64
java.io.tmpdir=C:\Users\RICOAN~1\AppData\Local\Temp\
line.separator=

java.vm.specification.vendor=Oracle Corporation
user.variant=
os.name=Windows 8.1
sun.jnu.encoding=Cp1252
java.library.path=C:\Windows\system32;C:\Windows\Sun\Ja...
java.specification.name=Java Platform API Specification
java.class.version=52.0
sun.management.compiler=HotSpot 64-Bit Tiered Compilers
os.version=6.3
user.home=C:\Users\Rico Antonio Felix
user.timezone=America/La_Paz
java.awt.printerjob=sun.awt.windows.WPrinterJob
file.encoding=Cp1252
java.specification.version=1.8
user.name=Rico Antonio Felix
java.class.path=.
java.vm.specification.version=1.8
sun.arch.data.model=64
java.home=C:\Program Files\Java\jre8
sun.java.command=ListSystemProperties
java.specification.vendor=Oracle Corporation
user.language=en
awt.toolkit=sun.awt.windows.WToolkit
java.vm.info=mixed mode
java.version=1.8.0
java.ext.dirs=C:\Program Files\Java\jre8\lib\ext;C:...
sun.boot.class.path=C:\Program Files\Java\jre8\lib\resour...
sun.stderr.encoding=cp437
java.vendor=Oracle Corporation
file.separator=\
java.vendor.url.bug=http://bugreport.sun.com/bugreport/
sun.cpu.endian=little
sun.io.unicode.encoding=UnicodeLittle
sun.stdout.encoding=cp437
sun.desktop=windows
sun.cpu.isalist=amd64

C:\Users\Rico Antonio Felix\Desktop>
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!