Question

I just installed Ubuntu 8.04 and I'm taking a course in Java so I figured why not install a IDE while I am installing it. So I pick my IDE of choice, Eclipse, and I make a very simple program, Hello World, to make sure everything is running smoothly. When I go to use Scanner for user input I get a very odd error:

My code:

import java.util.Scanner;

class test { public static void main (String [] args) { Scanner sc = new Scanner(System.in); System.out.println("hi"); } }

The output:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    Scanner cannot be resolved to a type
    Scanner cannot be resolved to a type

   at test.main(test.java:5)
Was it helpful?

Solution

The Scanner class is new in Java 5. I do not know what Hardy's default Java environment is, but it is not Sun's and therefore may be outdated.

I recommend installing the package sun-java6-jdk to get the most up-to-date version, then telling Eclipse to use it.

OTHER TIPS

If you are using a version of Java before 1.5, java.util.Scanner doesn't exist.

Which version of the JDK is your Eclipse project set up to use?

Have a look at Project, Properties, Java Build Path -- look for the 'JRE System Library' entry, which should have a version number next to it.

It could also be that although you are have JDK 1.5 or higher, the project has some specific settings set that tell it to compile as 1.4. You can test this via Project >> Properties >> Java Compiler and ensure the "Compiler Compliance Level" is set to 1.5 or higher.

I know, It's quite a while since the question was posted. But the solution may still be of interest to anyone out there. It's actually quite simple...

Under Ubuntu you need to set the java compiler "javac" to use sun's jdk instead of any other alternative. The difference to some of the answers posted so far is that I am talking about javac NOT java. To do so fire up a shell and do the following:

  1. As root or sudo type in at command line:

# update-alternatives --config javac

  1. Locate the number pointing to sun's jdk, type in this number, and hit "ENTER".

  2. You're done! From now on you can enjoy java.util.Scanner under Ubuntu.

System.out.println("Say thank you, Mr."); Scanner scanner = java.util.Scanner(System.in); String thanks = scanner.next(); System.out.println("Your welcome.");

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top