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

Razvan Cirstea
Razvan Cirstea
2,493 Points

Could not understand what does "System.getProperty("java.class.path")" do

It's still unclear to me the whole concept behind System.getProperty("java.class.path"). What is the use of it ? Where would we be using the retrieved information?

Also, I understand that the -cp command in the console tells the compiler where to look for some additional classes , apart from the main folder. Have I got it right ? If so, then why after setting the main folder as tmp (through cd /tmp), by using -cp ~/workspace/ , we will look in /home/treehouse/workspace/ , instead of /tmp/workspace ? I'm sure there's some easy explanation which I'm missing here :)

Anyway, any help on this topic would be greatly appreciated.

1 Answer

About -cp

The class path (or you could call it the source directory or the root directory) is 'workspace'. When you pass in the name of the class with the java or javac command, you type the name of the class file including the path from the class path:

javac  com.teamtreehouse.Systemizer.java

Notice that the 'com' directory' is inside the 'workspace' directory. So if you run the compiler from 'workspace', the compiler finds the class from there and all is well. Now if you type 'cd tmp', you move to the 'tmp' folder, which is not on the class path. This does not set tmp as the class path. The class path is still 'workspace', but you are now in 'tmp'. In this case you type: '-cp ~/workspace/' when you compile, in order to tell the compiler "by the way the class path is 'workspace', not the folder I'm in right now."

About System.getProperty("java.class.path"): well it gives you the class path. That's not something you use in code often, Craig just wanted to show that it was possible to access configuration values from the JVM (which can vary depending on where your program runs).

Razvan Cirstea
Razvan Cirstea
2,493 Points

Thank you very much, I got it now :) ! I suppose the use of the getProperty methods is to adapt the application implementation (for example desktop PC, mobile device, etc) based on certain properties that are outlined by this getter ?