CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2004
    Posts
    8

    FileNotFoundException + Exception in thread 'main'

    Hi,

    I am caught up in a bizzare spaghetti of issues, that I cant resolve and need help with.

    1). I started working on netbeans. I have to do programming that requires, filing. Now, when I implemented a small program that just reads a file and displays random words. The program cannot find the file. The text file is in the same folder as the program. I have also tried to put the text file in the build path, but it doesnt work. Though the program works fine, on my friends machine, I get the 'FileNotFoundException'. Similarly I tried, running several sample file reading programs, but ended up with the same exception.

    2). On a friends advice, I started using Eclipse also, but that also gives, 'FileNotFoundException'.

    3). The other big problem is, that when, I try running programs from command prompt, I get the 'exception in thread main () ' error, and therefore no programs run. It was working fine, till 3 days back, but all of a sudden it just started giving this error.

    4). My friend ran the ReadFile.java for me, debugged it and sent it to me back as a jar. If I try to run that jar, it works fine, on command prompt. Not only it doesnt give the 'main thread exception', but it also reads the file, with no issues.

    I have no idea, as to what is wrong, and since I m in the middle of a project, I m trying to avoid, having to uninstall/reinstall, everything from scratch. Please help, if you have any idea, on how to fix it.

  2. #2
    Join Date
    Sep 2006
    Location
    Eastern, NC, USA
    Posts
    907

    Re: FileNotFoundException + Exception in thread 'main'

    What happens if you run this program?:
    Code:
    import java.io.FileInputStream;
    
    public class FileDirectory
    {
        public static void main(String[] args)
        {
            String userDir = System.getProperty("user.dir");
            System.out.println(userDir);
        }
    }
    This will help you know where Java is looking for your file. Now if you are using a Jar, then you're not dealing with Files but instead with resources and Streams, and in this situation you would do well to look into use of this in the Sun tutorials.
    Last edited by petes1234; January 18th, 2009 at 11:38 AM.

  3. #3
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: FileNotFoundException + Exception in thread 'main'

    Ok first of all this is not an IDE issue or a build path issue, it's a runtime path problem. As petes1234 has already pointed out it is to do with where Java is looking for the file as your file has a relative and not an absolute path.

    In Java applications the current directory is the location in the file system from where the java command was invoked and not where the class files are located. It may have worked on your friends machine because they ran it from the directory containing the data file.

    If you want to run it from an IDE, the solution is to set the working directory the IDE will use to invoke the application to the directory containing the data file. Under Eclipse you can do this by opening the 'Run Configurations' dialog and selecting the 'Arguments' tab. At the bottom of the page you can enter the absolute path to the working directory. You can do something similar under Netbeans but I haven't used Netbeans for some time and I can't remember how to do it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured