Java Applications and the “Current Directory”

A recurring question in the Sun Java Forum groups is, “How do Ichange the current directory of my Java application?” The correct answer is that you don’t! Java applications don’t use the metaphor of ‘current directory’ in the same way native Unix and Windows applications do.

A native Unix or Windows application typically by default points atthe current directory of the command shell where the program isstarted, and if you do an fopen(“x.txt”,”w”) your file will end up inthat directory. You can then call chdir(..) to change the currentdirectory of the application. The metaphor is that the application islike a user at the command line, cd’ing around and accessing files.

On the other hand, when you start a Java application with ‘javaappname’, it is system dependent what the current directory is and youcan’t change the ‘current directory’ from a Java application, and theapplication as user metaphor does not apply.

To understand the Java solution, it is helpful to identify is thereal requirement. The requirement is not to change to a new currentdirectory just because you want to. The real requirement is to allow aprogram to get at files in the file system relative to the currentdirectory. In other words, make it easy to access files in a givenlocation without an absolute path prefix.

In Java, you use File objects to construct a relative view of thefile system. Two of the constructors for the File object take a ‘parent’argument that specifies a parent path that is prefixed to the path ofthe file itself to create the full abstract path to the file. What youdo is, create a File object with the path that represents your currentdirectory and then create all your file objects using that File objectas the parent. Voila, a current directory. If you want to changethe current directory, create a new File object with the desired pathand use it as the parent. See the SDK documentation, File object for thespecifics of the constructors.

The main catch here is that you have to specify the parent somehow,either by knowing it outright, specifying it thru some user input orfiguring it out from the application environment. If you know it outright, you can hardcode it or store it as an application property or something similar. The user input could be a command line argument or some run-timeinput method, such as a JFileChooser object.

On some systems you can read a system property called ‘user.dir’ toget the path that the application was started in. This property issystem dependent as to what it means or whether it exists at all, but inUnix and Windows it returns the directory the command shell was in whenthe java application was started. On Windows and Unix, “user.dir” is aREAD ONLY property, and you can’t change the ‘current directory’ bytrying to set the “user.dir” property. You read system properties withthe System.getProperty(String) method.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

technology leadership

Why the World Needs More Technology Leadership

As a fact, technology has touched every single aspect of our lives. And there are some technology giants in today’s world which have been frequently opined to have a strong influence on recent overall technological influence. Moreover, those tech giants have popular technology leaders leading the companies toward achieving greatness.

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job