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: Resource leakage problem

  1. #1
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Resource leakage problem

    Hi guys

    Getting a strange error when I build this program using the Eclipse IDE
    (Using Java version 8). I do not understand why I am getting an error -
    the book I use does not even explain about this. I might add this is the first
    program I have written using the Scanner utility.

    The error is:

    java Error at line 7: Resource Leak: 'input' is never closed

    I have posed all the code, as I have no idea where the problem is.
    Thanks very much for any help!

    Wishes Ada xx

    import java.util.Scanner;
     
    public class MainClass
    {
    	public static void main(String[] args)
    	{
    		Scanner input = new Scanner(System.in);
     
    		int num1 = 0; // first number
    		int num2 = 0; // second number
    		int num3 = 0; // third number
    		int num4 = 0; // fourth number
    		int num5 = 0; // fifth number
    		int positive = 0; // number of positive inputs
    		int negative = 0; // number of negative inputs
    		int zero = 0; // number of zero inputs
     
    		System.out.print("Enter an integer: ");
    		num1 = input.nextInt();
    		System.out.print("Enter an integer: ");
    		num2 = input.nextInt();
    		System.out.print("Enter an integer: ");
    		num3 = input.nextInt();
    		System.out.print("Enter an integer: ");
    		num4 = input.nextInt();
    		System.out.print("Enter an integer: ");
    		num5 = input.nextInt();
    		input.close();
     
    		// count number of positive numbers
    		if (num1 > 0) positive = positive + 1;
    		if (num2 > 0) positive = positive + 1;
    		if (num3 > 0) positive = positive + 1;
    		if (num4 > 0) positive = positive + 1;
    		if (num5 > 0) positive = positive + 1;
     
    		// count number of negative numbers
    		if (num1 < 0) negative = negative + 1;
    		if (num2 < 0) negative = negative + 1;
    		if (num3 < 0) negative = negative + 1;
    		if (num4 < 0) negative = negative + 1;
    		if (num5 < 0) negative = negative + 1;
     
    		// count number of zero values
    		if (num1 == 0) zero = zero + 1;
    		if (num2 == 0) zero = zero + 1;
    		if (num3 == 0) zero = zero + 1;
    		if (num4 == 0) zero = zero + 1;
    		if (num5 == 0) zero = zero + 1;
     
    		System.out.println("Positives: " + positive
    				+ "\nNegatives: " + negative
    				+ "Zero-based: " + zero);
    	}
    }
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)


  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: Resource leakage problem

    It's a warning. You can ignore it if you'd like, but it's the IDE trying to help.

    I see you've closed input on line 28, so you shouldn't be getting the warning with the code you've posted. If you still are, sometimes (rarely), Eclipse isn't always displaying issues with the latest version of the source code. Try rebuilding, closing/reopening, running, etc. to move Eclipse into the present.

    I'm not sure that it has anything to do with the behavior you're experiencing, but Eclipse Kepler SR 2 has a patch for Java 8. There are specific instructions how to install the patch that you can find by searching.

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

    Ada Lovelace (May 24th, 2014)

  4. #3
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: Resource leakage problem

    Quote Originally Posted by GregBrannon View Post
    It's a warning. You can ignore it if you'd like, but it's the IDE trying to help.

    I see you've closed input on line 28, so you shouldn't be getting the warning with the code you've posted. If you still are, sometimes (rarely), Eclipse isn't always displaying issues with the latest version of the source code. Try rebuilding, closing/reopening, running, etc. to move Eclipse into the present.

    I'm not sure that it has anything to do with the behavior you're experiencing, but Eclipse Kepler SR 2 has a patch for Java 8. There are specific instructions how to install the patch that you can find by searching.
    Thank you Greg! All it working now - the problem was indeed the fact Eclipse did not check that I had added the close() function
    at that line. After I did a re-build and debugged again, the warming had vanished - the code works fine. Coming from C#, I used to
    do:
    int num1 = int.Parse(System.Console.ReadLine());

    but never had to close input after that statement. I know Java is totally different in this aspect,
    but to be honest that code in C# was too long winded for a simple input. Loving Java's shorter style!
    Thanks again!

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  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: Resource leakage problem

    Glad to help. You're well on your way, and I trust you'll find the journey pleasant.

  6. #5
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Resource leakage problem

    By the way, the input from the command line does not leak resources. Its just that the IDE does not know where you are getting the input from. You can also use a scanner to read a file or from a network. In these cases its adviced to close the stream at the end of the programs execution and that is why eclipse is showing you this warning.
    You could also ignore it since you are scanning the command line.

  7. The Following 2 Users Say Thank You to Cornix For This Useful Post:

    Ada Lovelace (May 24th, 2014), GregBrannon (May 24th, 2014)

  8. #6
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: Resource leakage problem

    Thank you for the additional information Cornix - I will keep than in mind for future
    applications.

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

Similar Threads

  1. Help with resource bundles
    By dpjmie in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 22nd, 2013, 03:39 PM
  2. 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
  3. The requested resource () is not available.
    By freakycoder in forum Web Frameworks
    Replies: 0
    Last Post: July 10th, 2012, 12:44 AM
  4. [SOLVED] Resource Injection
    By newbie in forum JDBC & Databases
    Replies: 2
    Last Post: March 22nd, 2011, 04:27 PM
  5. Java Learning Resource for Beginners
    By Freaky Chris in forum The Cafe
    Replies: 1
    Last Post: April 29th, 2009, 04:57 AM