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 4 of 4

Thread: Eclipse Error: Scanner resource leak

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Eclipse Error: Scanner resource leak

    I am currently working on a program and I am working with Eclipse as my IDE.

    Initially, my code working without any issue. And then, someone used it, and ever since then, I have been getting some issues.

    The problem is related to my Scanner object declaration. Everything that I create the Scanner object, a yellow triangle with an exclamation mark inside appears on the left. The error message is the following:

    "Resource leak: input is never closed."

    And here is my current code.

    public class PatrickTchakounteNjomgangHw2 {
    	public static void main(String[] args) {
    		//Print author's information
    		System.out.println("Patrick Tchakounte Njomgang, Java Homework 2, Tues/Thur 9:30 am");
     
    		//Create a Scanner object
    		Scanner input = new Scanner(System.in);
     
    		//Receive input
    		System.out.println("Enter hourly pay");
    		double hourlyPay = input.nextDouble();
     
    		System.out.println("Enter hours worked");
    		int hoursWorked = input.nextInt();
     
    		System.out.println("Enter tax rate");
    		double taxRate = input.nextDouble()/100;
     
    		//Calculate total income before tax, total income after tax, actual tax
    		double grossIncome = hourlyPay * hoursWorked;
    		double netIncome = grossIncome - (grossIncome * (taxRate));
    		double taxAmount = grossIncome * taxRate;
     
    		//Print outputs
    		System.out.println("The total income before tax is $" + (grossIncome));
    		System.out.println("The total income after tax is $" + (netIncome));
    		System.out.println("The actual tax is $" + ((int)(taxAmount * 100)/100.0));
    	}
    }


    As I stated earlier, I have not changed the code, and there were no errors before.

    I tried the following. This code is only for demonstration purposes.

    Scanner input = new Scanner(System.in);  
    try  
    {  
       //all code which uses the scanner  
    }  
    finally  
    {  
        input.close();  //In finally to make sure it always gets closed  
    }


    I still get the triangle next to the line number, but it is only greyed out. But the yellow error still shows up on my Project Explorer next to my source file name.

    So if someone could help me I would appreciate.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Eclipse Error: Scanner resource leak

    It's not an Eclipse error, it's a warning. You say someone may have used your "program" (not sure which one), but the change you describe could happen if they messed with Eclipse's setup and modified which warnings are displayed. Who knows why'd someone would do that, but people can be weird.

    A warning that has been corrected is grayed out. The folder in the project explorer may show the exclamation mark until the next time the program is compiled and run.

    A try/catch/finally is not appropriate in this case. Instead, at the end of your first posted program (or when done with the Scanner object), simply close the Scanner object:

    input.close();

    Finally, relax a bit. Nothing's going to blow up or call the FBI on you. Get used to your tools, learn how to use them and understand what they can/can't do for you, and learn how to set them up and modify them to suit your needs. They're supposed to be helping you, not making your life more difficult. Don't let your tools be the boss of you. It should be the other way 'round.

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    vicsar (April 6th, 2014)

  4. #3
    Junior Member
    Join Date
    Feb 2014
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Eclipse Error: Scanner resource leak

    Quote Originally Posted by GregBrannon View Post
    It's not an Eclipse error, it's a warning. You say someone may have used your "program" (not sure which one), but the change you describe could happen if they messed with Eclipse's setup and modified which warnings are displayed. Who knows why'd someone would do that, but people can be weird.

    A warning that has been corrected is grayed out. The folder in the project explorer may show the exclamation mark until the next time the program is compiled and run.

    A try/catch/finally is not appropriate in this case. Instead, at the end of your first posted program (or when done with the Scanner object), simply close the Scanner object:

    input.close();

    Finally, relax a bit. Nothing's going to blow up or call the FBI on you. Get used to your tools, learn how to use them and understand what they can/can't do for you, and learn how to set them up and modify them to suit your needs. They're supposed to be helping you, not making your life more difficult. Don't let your tools be the boss of you. It should be the other way 'round.
    I added the closing statement, and it worked. The yellow triangles next to the source files in the Solution Explorer disappeared.

    However, I still have an issue. After I have submitted a zipped version of my .java source code, my instructor wants me to download it again and extract it to a folder other than my current workspace. After I have extracted it, I am to try to run the program in Eclipse as a "Java Application", in order to make sure that it works. However, the link shows up randomly under the "Run" menu, either as a "Run as Server" option, as "(none applicable)", and with my latest try, as "Run as Java Application".

    But it is really random, so it is kind of weird.

    Nevertheless, thank you for the help.

  5. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Eclipse Error: Scanner resource leak

    You lost me. I would suggest that if your instructor wants you to submit your work outside Eclipse, then simply copy the source code from the Eclipse editor, paste it into another text editor (I use Geany a lot for editing on the side, but Notepad++ is highly recommended) and save that in a properly named file with no package specified. If you want to run that new file to verify that it works, then compile and run it from the command line. Getting that to work will not only be a good exercise for you, but it may replicate your instructor's test environment, but I can't be sure.

    Check your class notes and/or the class' web page to see if the instructor has posted specific requirements for turning in source code. If so, and if you'd like help, post those so that we can understand what is being asked for.

Similar Threads

  1. JOptionPane error in eclipse
    By runkerr in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 26th, 2013, 05:09 PM
  2. Resource bundles, Code error
    By dpjmie in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 25th, 2013, 06:39 AM
  3. [SOLVED] Scanner closed error
    By dx2731 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: April 16th, 2013, 04:25 AM
  4. Resource leak: 'in' is never closed..?
    By missm in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 3rd, 2012, 02:01 PM
  5. Error when opening eclipse
    By BlackJavaCoder in forum Java IDEs
    Replies: 4
    Last Post: September 3rd, 2011, 07:57 AM