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

How does the compiler "look for" or find the appropriate file structure?

Hi,

I wonder how the java compiler handles the "package com.teamtreehouse;" line if used in a different directory, for example on C:\example.java (example for windows) or /tmp/example.java (example in linux).

How does the compiler know where those packages are being saved in the file hierarchy? As of my understanding the default libraries are retrieved from java installation directory, but how does this work with custom ones?

Best regards, Michael

1 Answer

Hey Michael,

The question from what I understand is that you want to find out how the Java compiler looks for the directory in which the framework is stored. I'm a bit rusty on this topic, but I know that when you install your IDE you tell it the PATH of your JDK directory. This is the same way custom packages are done, but in a different style. For example, If you provided your project with a JAR file the IDE may ask you if you want to copy it to the project, and if you do copy it then that (the project directory) may be the directory it can be found in.

If you're working in a Unix environment, you can edit the PATH environment variable to the custom package. Using the above example, you can compile your java application with a JAR file (with a package(s)) inside like this:

javac -jar 'jar directory' file.java

Overall, different IDEs handle things differently. At the end of the day, it's just looking for a directory that we introduce.

Good luck! :)

Hi Richard, Thanks for the feedback. However I'm currently trying to figure out how the compiler itself does it (without using a IDE) as this has to work as well. In the Java course exercises we're always working in the workspace provided - I wonder how this workspace looks for those packages.

I've found out that the classpath - environment variable for java might be the key. According to https://docs.oracle.com/javase/8/docs/technotes/tools/windows/classpath.html#BEHBDEEB for the compiler to be able to find the package it has to be defined in the PATH variable.

Now I wonder if a IDE does this automatically when asking for a "workspace" (like eclipse for example does).