Results 1 to 9 of 9

Thread: Eclipse and java problem

  1. #1
    Join Date
    Dec 2009
    Beans
    76

    Eclipse and java problem

    I saw similar problems in this forum, but none quite the same. I am running Ubuntu 10.04, have Helios Eclipse installed and java open sdk 6-20. I have several programs in the Eclipse workspace and all of them work except this one. I have looked at the library paths and build configuration and they look fine - java is always visible.

    The bit of code that fails is this:

    Code:
    public static void main(String[] args) throws IOException {
            
        try {
        File inFile = new File("mcdata.txt");
        } catch (FileNotFoundException e) {
            System.out.println("Data file not present");
        }
            FileInputStream inStream   = new FileInputStream(inFile);
            DataInputStream dataStream = new DataInputStream(inStream);
     
            System.out.println(dataStream.readInt());
        }
    The exception handler has no effect - same result if I comment it out. The error message I get is this:

    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    inFile cannot be resolved to a variable
    the file mcdata.txt is present in the same directory as the source, but I get the same error if it is not. I have an import java.io.* statement up at the top.

    Thanks for any help

  2. #2
    Join Date
    Sep 2011
    Location
    UK
    Beans
    76
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Re: Eclipse and java problem

    You have declared inFile in a different scope to where you are trying to use it. Try declaring inFile outside the try block. e.g.

    Code:
    File inFile = null;
    try {
        inFile = new File("mcdata.txt");
    } 
    
    etc.

  3. #3
    Join Date
    Jan 2008
    Location
    Manchester UK
    Beans
    13,573
    Distro
    Ubuntu

    Re: Eclipse and java problem

    Thread moved to Programming Talk.

  4. #4
    Join Date
    Dec 2009
    Beans
    76

    Re: Eclipse and java problem

    Thank you for the reply

    When I take out the exception handler entirely, I get the same error, but also file not found, even though I have the data file in the src and bin directories in the Eclipse workspace.

    I'm going to try a second reinstall of Java.

  5. #5
    Join Date
    Sep 2011
    Location
    UK
    Beans
    76
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Re: Eclipse and java problem

    Quote Originally Posted by grizdog View Post
    Thank you for the reply

    When I take out the exception handler entirely, I get the same error, but also file not found, even though I have the data file in the src and bin directories in the Eclipse workspace.

    I'm going to try a second reinstall of Java.
    If you are running the code from Eclipse, there is an option in the run configurations dialog to specify the current working directory when running the app. You must have the mcdata.txt file in this directory otherwise it won't find it. As a start, I'd recommend putting in the entire path to the .txt file to make sure it is being picked up correctly.

    What does your code look like when you've taken the exceptions out?

  6. #6
    Join Date
    Jun 2007
    Location
    Malvern, UK
    Beans
    992
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Eclipse and java problem

    Seriously, it doesnt look like it's an install problem!

  7. #7
    Join Date
    Sep 2011
    Location
    UK
    Beans
    76
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Re: Eclipse and java problem

    Quote Originally Posted by grizdog View Post
    I'm going to try a second reinstall of Java.
    You've got a compilation problem, so I don't think a fresh install of Java will make any difference.

  8. #8
    Join Date
    Dec 2009
    Beans
    76

    Re: Eclipse and java problem

    Argh. After I did the new install, I moved the data file to the project directory (I feel dumb) and it worked. I have no idea if that was the only problem

    Thanks to all.

  9. #9
    Join Date
    May 2007
    Location
    Leeds, UK
    Beans
    1,675
    Distro
    Ubuntu

    Re: Eclipse and java problem

    This looks like a test project at this stage but you ought to think longer term about using getResourceAsStream to load a resource.

    At the moment, you are relying on the working directory of the JVM to locate that file using a relative path. If you were to open a terminal, change to the project's 'bin' directory and try running the program, you'd probably find it doesn't work. If you package it into a JAR file, it definitely won't work.

    Using getResourceAsStream is the portable and reliable solution (and no more difficult really):

    Code:
    package example;
    
    import java.io.DataInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    
    public class Driver {
    
    	public static void main(String[] args) throws IOException {
    		InputStream in = Driver.class.getResourceAsStream("/data/mcdata.txt");
    		if (in != null) {
    			DataInputStream dataStream = new DataInputStream(in);
    			System.out.println((dataStream.read()));
    		}
    		else {
    			System.err.println("Resource not found");
    		}
    	}
    }
    The directory structure of the project looks like this (bear in mind, the Driver class is in the example package):

    Code:
    src:
    data  example
    
    src/data:
    mcdata.txt
    
    src/example:
    Driver.java

Tags for this Thread

Bookmarks

Posting Permissions

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