Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Local Development Environments How it Works The Java Virtual Machine

Matthew Francis
Matthew Francis
6,967 Points

Newbie - Property Object?

From what I've read, properties are similar to a hashMap, they have key and value pairs, but the key and value must be a String. However in this code:

package com.teamtreehouse;
import java.util.Set;
public class Systemizer {

  public static void main(String[] args) {
   System.out.printf("This is the classpath: %s %n",System.getProperty("java.class.path"));
    Set<String> propNames = System.getProperties().stringPropertyNames(); 
    //stringPropertyNaes is a method in the class, Property
  }
}

I'm confused on System.getProperties(), what does it mean? and how can it get the properties when we did not even define the key/value pairs yet?

3 Answers

System.getProperties() is just used to return information about your System Properties (like os name, os type, java version usw) stringPropertyNames() returns "a set of keys in this property list where the key and its corresponding value are strings, including the keys in the default property list"

getProperties() returns "the system properties" https://docs.oracle.com/javase/8/docs/api/java/lang/System.html#getProperties--

https://docs.oracle.com/javase/8/docs/api/java/util/Properties.html#stringPropertyNames--

You dont needt to set properties since they are given by the system.

Matthew Francis
Matthew Francis
6,967 Points

Hmm, so the key and values in the properties are automatically set by the system?

getProperties() is a method of the System class and return the current properties of your system and Java installation yes. This can be used to check, if you system is missing some requirements to run the application (which should not be the case in Java :D). For example to check the system architecture.

You will find this in most programming languages.

Hmm, so the key and values in the properties are automatically set by the system? No the values are getting returned by the getProperties() method which checks the system properties and returns the values and keys.

Matthew Francis
Matthew Francis
6,967 Points

Thanks,

Two questions though, why can't I compile the code above with "javac Systemizer.java"?

And how do you check the keys/values? I tried to do:

    Set<String> propNames = System.getProperties().stringPropertyNames(); 
    System.out.println(propNames);

Unfortunately, it didnt work.

I can't really say why you got compiler errors without some certain information But this code works as you can see: https://repl.it/CpwN/1

Matthew Francis
Matthew Francis
6,967 Points

Thanks! Just for future references if anyone mentions/ask abou tit. what does the key represent and the value represent?

It printed this:

https://repl.it/CpwN/2

The key is just the name of the system Property like: https://docs.oracle.com/javase/8/docs/api/java/lang/System.html#getProperties-- Key | Description of Associated Value

  • java.version | Java Runtime Environment version
  • java.vendor | Java Runtime Environment vendor
  • java.vendor.url | Java vendor URL
  • java.home | Java installation directory
  • java.vm.specification.version | Java Virtual Machine specification version
  • java.vm.specification.vendor | Java Virtual Machine specification vendor
  • java.vm.specification.name | Java Virtual Machine specification name
  • java.vm.version | Java Virtual Machine implementation version
  • java.vm.vendor | Java Virtual Machine implementation vendor
  • java.vm.name | Java Virtual Machine implementation name
  • java.specification.version | Java Runtime Environment specification version
  • java.specification.vendor | Java Runtime Environment specification vendor
  • java.specification.name | Java Runtime Environment specification name
  • java.class.version | Java class format version number
  • java.class.path | Java class path
  • java.library.path | List of paths to search when loading libraries
  • java.io.tmpdir | Default temp file path
  • java.compiler | Name of JIT compiler to use
  • java.ext.dirs | Path of extension directory or directories Deprecated. This property, and the mechanism which implements + it, may be removed in a future release.
  • os.name | Operating system name
  • os.arch | Operating system architecture
  • os.version | Operating system version
  • file.separator | File separator ("/" on UNIX)
  • path.separator | Path separator (":" on UNIX)
  • line.separator | Line separator ("\n" on UNIX)
  • user.name | User's account name
  • user.home | User's home directory
  • user.dir | User's current working directory

Also this might help you https://repl.it/CpwN/4