Note that there are some explanatory texts on larger screens.

plurals
  1. PODoes Java directory/package hierarchy enforcement allow for jar files to be platform independent?
    text
    copied!<p>I'm sure that I'm misinterpreting something in the Java Language Specification but I can't figure out what. My question is this: On many systems, java forces the directory structure to parallel package names i.e. if the package name is foo.bar then the package files should reside in the folder foo/bar. However, this isn't part of the Java Language Specification and is considered optional. So, what if you had an implementation of Java on one platform that enforced these rules and another implementation that didn't? Would the implementation that required the file hierarchy be able to find the files in the unstructured package used by the other implementation? Does Java always search for class files in all class paths recursively? If so, then why require the package names to mimic the directory structure to begin with?</p> <p>EDIT: For example, suppose that we have two java files one.java and two.java. At the top of these files we prepend "package foo.bar;" thus labeling these files for the package foo.bar. Now, MOST Java implementations will make us put these files into a file hierarchy so that we have /foo/bar/one.java and /foo/bar/two.java. However, according to the JLS this is not required. Now, suppose that we have a Java implementation which does not force us to put them in a hierarchy. That is, we have a .jar file and it has contents one.java and two.java in the top level directory. Now, if we load that .jar file into a system which relies on file hierarchy to locate class files, will it be able to find those class files?</p> <p>EDIT: So, after a little bit of experimentation, I found out that if you set up packages without the appropriate directory hierarchy by jar cvf foo.jar one.class two.class and you try to import them as foo.bar.two and foo.bar.one it just doesn't work even if the packages are specified - you get a NoClassDefFoundError. This is a pretty interesting result to me considering <a href="http://docs.oracle.com/javase/tutorial/java/package/managingfiles.html" rel="nofollow">http://docs.oracle.com/javase/tutorial/java/package/managingfiles.html</a> says, "Many implementations of the Java platform rely on hierarchical file systems to manage source and class files, although The Java Language Specification does not require this."</p> <p>In short, this means that if you are working on an implementation of Java that has decided to use the hierarchical directory structure to manage packages(which is nearly all of them) then the generated jar files may not be compatible with other systems which did not implement the same way. It seems like the Java community has gotten around this by convention, but I'd be interested to hear more if anything has any additional information.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload