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

Thread: issue with scanner class-resource leak

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

    Default issue with scanner class-resource leak

    Hi everyone, i am new to the forum. I am currently in the middle of an Oracle course and i am having an issue with one of my assignments.
    It runs fine in Eclipse but not in the Cmd line. I have searched for days on how to fix the issue with no results so i am hoping that someone here may be kind enough to help me.

    public class Calc {

    public static void main(String[] args) {

    Scanner items = new Scanner(System.in); // resource error occurs on this line "resource leak:'items' is never closed
    double firstnumber, secondnumber, thirdnumber, answer;
    System.out.println("Enter first number");
    firstnumber = items.nextDouble();
    System.out.println("Enter second number");
    secondnumber = items.nextDouble();
    System.out.println("Enter third number");
    thirdnumber = items.nextDouble();
    answer = firstnumber + secondnumber + thirdnumber;
    System.out.println(answer);


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

    Default Re: issue with scanner class-resource leak

    not in the Cmd line.
    What happens when the code is executed?
    What OS are you are?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    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: issue with scanner class-resource leak

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for newcomers.

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

    Default Re: issue with scanner class-resource leak

    Hi I am running windows 8, no error in eclipse, error from cmdline pasted below

    C:\Users\user1\Desktop\Java\Eclipes projects\Assignment 2\src\Calc>javac Calc.ja
    va

    C:\Users\user1\Desktop\Java\Eclipes projects\Assignment 2\src\Calc>java Calc
    Exception in thread "main" java.lang.NoClassDefFoundError: Calc (wrong name: Cal
    c/Calc)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java :800)
    at java.security.SecureClassLoader.defineClass(Secure ClassLoader.java:14
    2)
    at java.net.URLClassLoader.defineClass(URLClassLoader .java:449)
    at java.net.URLClassLoader.access$100(URLClassLoader. java:71)
    at java.net.URLClassLoader$1.run(URLClassLoader.java: 361)
    at java.net.URLClassLoader$1.run(URLClassLoader.java: 355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.j ava:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:4 25)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:3 58)
    at sun.launcher.LauncherHelper.checkAndLoadMain(Launc herHelper.java:482)


    C:\Users\user1\Desktop\Java\Eclipes projects\Assignment 2\src\Calc>

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,112
    Thanks
    65
    Thanked 2,718 Times in 2,668 Posts

    Default Re: issue with scanner class-resource leak

    java.lang.NoClassDefFoundError: Calc (wrong name: Calc/Calc)
    It looks like the class is in a package. Remove the package statement and redo the javac and java steps and it should work.

    Otherwise when using the java command, set the classpath to the folder containing the Calc folder and include the package name on the commandline: java Calc.Calc
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Eclipse Error: Scanner resource leak
    By NorrinGalan in forum Java IDEs
    Replies: 3
    Last Post: February 7th, 2014, 05:27 AM
  2. Using the Scanner class
    By Brocco767 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 26th, 2012, 07:47 PM
  3. 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
  4. FileScan and Scanner issue! HELP
    By mm4474 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: September 23rd, 2012, 08:10 PM
  5. scanner and int issue
    By saintnicks in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 24th, 2012, 01:01 AM