Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7821923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:47:18+00:00 2026-06-02T07:47:18+00:00

I have an application using JFileChooser to select a directory. In this app, I

  • 0

I have an application using JFileChooser to select a directory. In this app, I do not want to try to process symbolic links, and I would like to run on multiple platforms. The code therefore attempts to determine whether a chosen file is a symbolic link, and displays an error dialog if it is.

Here is the code that obtains the File from the JFileChooser.

public File getDirectoryChoice(String buttonText, String currentDirectory)
{
  File chosenFile = null;
  if (fileChooser == null) { fileChooser = new JFileChooser(); }
  if (currentDirectory != null) 
    { fileChooser.setCurrentDirectory(new File(currentDirectory)); }
  fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
  fileChooser.setApproveButtonText(buttonText);
  int returnValue = fileChooser.showOpenDialog(mainFrame);
  if (returnValue == JFileChooser.APPROVE_OPTION)
  {
    chosenFile = fileChooser.getSelectedFile();
  }
  return chosenFile;
}

and here is the code I use to determine whether the chosen file is a symbolic link:

public static boolean isSymbolic(File f)
{
  try
  { 
    String absolute = f.getAbsolutePath();
    String canonical = f.getCanonicalPath();
    return !(absolute.equals(canonical));
  }
  catch (IOException ioe)
  {
    return false;
  }
}

On Windows 7: If the user chooses a given directory using the mouse, this works fine. If the user types the same directory name into the filename text box, the second code snippet indicates that the absolute and canonical paths are not the same. It does not matter if the user enters the trailing backslash or not.

When I stop this in the debugger on the ‘return’ statement line and look at the details of the two strings, the hash value of the absolute path string is a big negative number, and the hash value of the canonical string is 0. I don’t know why that would be, and in fact wonder if it’s a quirk of the (eclipse) debugger.

Can anyone tell me why there would be this difference?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T07:47:19+00:00Added an answer on June 2, 2026 at 7:47 am

    Double check that you are typing the string exactly the same as what is returned by JFileChooser.

    I tried this out and without thinking I typed c:\temp. Just like in your tests, isSymbolic() method returned true. However upon closer inspection, I noticed that when I selected this path in the file chooser, it returned C:\temp instead (note the capital C).

    So even though it’s ugly, you could add a special case something like this:

        if (System.getProperty("os.name").toLowerCase().indexOf("win") >= 0)
            return !(absolute.equalsIgnoreCase(canonical));
        else
            return !(absolute.equals(canonical));
    

    You also might want to look at this related question: “Java 1.6 – determine symbolic links“. There are quite a few answers there which you might find helpful.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have an application using the IBM Informix driver. Whenever we try to open
I have a web application using ASP.NET 2.0 and I want to know if
I have a web application using JPA and JTA with Spring. I would like
I have a java-application using JDBC for database interaction. I want to do a
I have an application using NServiceBus to process messages, and while debugging I'm finding
I have this application using CAL. All the modules derive from a special class,
In my Java application I need to select a path using JFileChooser. The code
I have an application using in-app purchase. Apparently, if the user has a jailbroken
I have an application using Java servlets/JSP's. There are multiple clients using my app,
Scenario I have an application using asp.net Master Pages in which I would like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.