Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: How can I check to see if a file ending with a certain extension exists in the current working directory

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How can I check to see if a file ending with a certain extension exists in the current working directory

    This question is different from "Find files in a folder using Java" that has already been asked because I do not know what the users current working directory will be. I need my program to look in the current working directory that the program is running from, to look for a certain file.

    I need to have my program check to see if a file ending with ".lic" exist in the current working directory before it executes. If the file does not exist, I need a message to appear alerting the user that they need to activate the license. Here is the code that I have.

    When I run it through the debug, "user.dir" give a message - user.dir = >Unknown type "user.dir"<

    String License says "License = >"License" is not a known variable in the current context.<"

    try{
     
        String License = System.getProperty("user.dir") + ("*.lic");
        File license = new File(License);
          if (license.exists()){
        try{
        Runtime.getRuntime().exec(cmd);
     
        } catch (IOException ex) {
      }
          } else {
              JOptionPane.showMessageDialog(null, "Need to activate license, please see README file");
          }
      }catch(HeadlessException e){
          // if any error occurs
     
      }
     
     
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How can I check to see if a file ending with a certain extension exists in the current working directory

    // if any error occurs
    Make sure that all catch blocks have a call to the printStackTrace method so you will get a message if there is an error.

    What happens when the posted code is compiled and executed? What do you want to be different?

    How are you trying to debug the code? What is the value of License? Print it to see.


    String License = System.getProperty("user.dir") + ("*.lic");
    Note: * is not a valid character for a filename
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How can I check to see if a file ending with a certain extension exists in the current working directory

    It is not getting the current working directory. I need to look for a file ending in ".lic".
    When I try to get the value of License, this is what it says, "License = >"License" is not a known variable in the current context.<"

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How can I check to see if a file ending with a certain extension exists in the current working directory

    I need to look for a file ending in ".lic".
    Do you know the name of the file? Why not use that name in filename String passed to the File class's constructor?

    When I try to get the value of License
    What code are you using to do that? Can you post that code?

    it says, "License = >"License" is not a known variable in the current context.<"
    What program is the it you talk about?

    It is not getting the current working directory.
    How do you know that? What value is it getting for the current working directory?
    What is the correct value?

    look for a file ending in ".lic".
    One way to see all the files with that extension is to Use the File class's list method with a filename filter.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How can I check to see if a file ending with a certain extension exists in the current working directory

    My program needs to check for any files ending .lic. If there is a file ending in ".lic" then run the user input. If a ".lic" does not exist then pop up a message.
    in my code the "user.dir" is not getting the current working directory.
    License value is - "License" is not a known variable in the current context.<"

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How can I check to see if a file ending with a certain extension exists in the current working directory

    the "user.dir" is not getting the current working directory.
    Please post the value returned by System.getProperty("user.dir")
    and also post what you think is the current working directory

    License value is - "License" is not a known variable in the current context.<"
    Where does that message come from? Can you post the code that causes that message?

    check for any files ending .lic.
    Please read what I have posted:
    One way to see all the files with that extension is to Use the File class's list method with a filename filter.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. check to see if a file exists using the current directory
    By allisbs in forum Object Oriented Programming
    Replies: 3
    Last Post: January 13th, 2019, 07:01 AM
  2. java.lang.Exception: folder exists, but it is not a directory.
    By tandonkunal in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 7th, 2014, 11:49 PM
  3. check if table exists from a list of files in a directory
    By efoikonom in forum JDBC & Databases
    Replies: 1
    Last Post: June 10th, 2013, 08:42 AM
  4. Replies: 1
    Last Post: May 25th, 2013, 05:13 AM
  5. Wrong current working directory
    By tux008 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 11th, 2011, 11:25 AM

Tags for this Thread