In the context of UW's intro course, the instructors and I have been
having a discussion about the "current directory" issue of DrJava's
Interactions pane. As a reminder, here's the facts so far:
1) Opening any file with a relative path name loads it relative to the
directory where the JVM was started. For the Interactions pane, that's
currently the directory where DrJava was started, not the directory of
the open class files.
2) An appropriate way to load files in production code is
getClass().getResource("myfile.jpg"), which loads files from the same
directory as the class file, returning a URL. This is arguably far too
complex for CS1 students.
3) You can't change the current directory of a JVM after it's started.
Given those, our position has been to make people use getResource,
since that has the expected behavior in most cases, and it's definitely
unclear which directory we would put the Interactions pane in if
multiple files from different folders are open in DrJava.
However... in the interest of creating a better heuristic which
might be more convenient, we've discussed what happens when DrJava runs
the main method of a class (either through a menu item or by typing
"java Classname"). We do not currently reset the Interactions pane to
run main(), but it's arguable that we *should* provide a clean slate,
despite the extra time it takes to reset. (It's more in tune with what
the user expects to happen.) If that change were made, then the JVM
could be started in the correct directory to load relative paths in the
case of running a main method.
Note that any normal reset wouldn't attempt to change the directory;
only running the main method of a class would. This could still lead to
some confusing behavior if the user tries to load files in the
Interactions pane after an invocation of main vs. after a clean reset,
but that's arguably much less likely than expecting main to run from the
class's directory.
Please let us know your opinions on this, and whether it's a good
idea to move DrJava in this direction.
Thanks!
Charlie
|