PDA

View Full Version : Eclipse Problems with java.util.Scanner



tonyr1988
April 26th, 2007, 07:00 PM
Whenever I try anything such as:


import java.util.Scanner;

in Eclipse, it always complains that "The import java.util.Scanner cannot be resolved". I recently upgraded from Edgy to Feisty, and this is the first time this has happened. Files that worked perfectly under my Edgy Eclipse are now broken in Feisty Eclipse.

I'm sure I just need to install an extra package or two, but I'm not sure which ones.

Also, I just did a fresh install of Feisty on my laptop, and I'm having the same problems with Eclipse.

ssodhi
April 26th, 2007, 07:39 PM
Have you updated your JDK?

-Sanjay Sodhi

tonyr1988
April 26th, 2007, 07:41 PM
I haven't touched my JDK. I just updated what the Update Manager told me to when I went from Edgy -> Feisty.

kaamos
April 26th, 2007, 09:34 PM
You need Java 1.5 or newer and make sure code compatibility in your eclipse project is also set to 1.5. You can also select which JRE a project uses.

tonyr1988
April 26th, 2007, 10:15 PM
It looks like I had 1.4.2. Changed to 1.5 and it works perfectly.

I just don't know why it worked before the update, but Feisty broke it? Did it downgrade my JRE? I dunno, but it works, so I'm happy. :)

Todd Bealmear
April 26th, 2007, 11:08 PM
Yeah, it downgrades your JRE - Feisty comes with 1.4 and will replace whatever JRE you were using pre-upgrade.

nick937
March 17th, 2008, 11:16 PM
I am using Hardy heron and I have the same problem




nick@t00r:~$ java -version
java version "1.7.0"
IcedTea Runtime Environment (build 1.7.0-b24)
IcedTea 64-Bit Server VM (build 1.7.0-b24, mixed mode)

nick937
March 17th, 2008, 11:18 PM
and my JRE is


java-1.5.0-gcj-4.2-1.5.0.0


can anyone help?

bruce89
March 18th, 2008, 02:45 AM
The wee error icon on the left of the wrong line might be interesting if you click on it.

nick937
March 18th, 2008, 04:18 AM
Severity and Description Path Resource Location Creation Time Id
Scanner cannot be resolved to a type Lab17b Lab17.java line 8 1205809595681 220

I have also tried
import java.*;
import java.util.*
import java.util.Scanner
...... when I click the icon it says "rename in file"

Also, when I start up Eclipse it give me this:


Unable to create this part due to an internal error. Reason for the failure: An exception was thrown during initialization

java.lang.ClassNotFoundException: org.eclipse.core.runtime.Plugin
at org.eclipse.osgi.framework.internal.core.BundleLoa der.findClass(BundleLoader.java:402)
at org.eclipse.osgi.framework.internal.core.BundleLoa der.findClass(BundleLoader.java:347)
at org.eclipse.osgi.internal.baseadaptor.DefaultClass Loader.loadClass(DefaultClassLoader.java:83)
at java.lang.ClassLoader.loadClass(libgcj.so.81)
at org.eclipse.core.runtime.Platform.getPlugin(Platfo rm.java:737)
at org.eclipse.core.internal.preferences.legacy.InitL egacyPreferences.init(InitLegacyPreferences.java:4 3)
at org.eclipse.core.internal.preferences.PreferenceSe rviceRegistryHelper.applyRuntimeDefaults(Preferenc eServiceRegistryHelper.java:146)
at org.eclipse.core.internal.preferences.PreferencesS ervice.applyRuntimeDefaults(PreferencesService.jav a:337)
at org.eclipse.core.internal.preferences.DefaultPrefe rences.applyRuntimeDefaults(DefaultPreferences.jav a:162)
at org.eclipse.core.internal.preferences.DefaultPrefe rences.loadDefaults(DefaultPreferences.java:231)
at org.eclipse.core.internal.preferences.DefaultPrefe rences.load(DefaultPreferences.java:227)
at org.eclipse.core.internal.preferences.EclipsePrefe rences.create(EclipsePreferences.java:307)
at org.eclipse.core.internal.preferences.EclipsePrefe rences.internalNode(EclipsePreferences.java:543)
at org.eclipse.core.internal.preferences.EclipsePrefe rences.node(EclipsePreferences.java:662)
at org.eclipse.core.internal.preferences.PreferencesS ervice.getNodes(PreferencesService.java:588)
at org.eclipse.core.internal.preferences.PreferencesS ervice.getString(PreferencesService.java:635)
at org.eclipse.core.internal.filebuffers.TextFileBuff erManager.getLineDelimiterPreference(TextFileBuffe rManager.java:645)
at org.eclipse.core.internal.filebuffers.TextFileBuff erManager.createEmptyDocument(TextFileBufferManage r.java:300)
at org.eclipse.core.internal.filebuffers.ResourceText FileBuffer.initializeFileBufferContent(ResourceTex tFileBuffer.java:268)
at org.eclipse.core.internal.filebuffers.ResourceFile Buffer.create(ResourceFileBuffer.java:242)
at org.eclipse.core.internal.filebuffers.TextFileBuff erManager.connect(TextFileBufferManager.java:108)
at org.eclipse.ui.editors.text.TextFileDocumentProvid er.createFileInfo(TextFileDocumentProvider.java:56 1)
at org.eclipse.jdt.internal.ui.javaeditor.Compilation UnitDocumentProvider.createFileInfo(CompilationUni tDocumentProvider.java:909)
at org.eclipse.ui.editors.text.TextFileDocumentProvid er.connect(TextFileDocumentProvider.java:482)
at org.eclipse.jdt.internal.ui.javaeditor.Compilation UnitDocumentProvider.connect(CompilationUnitDocume ntProvider.java:1069)
at org.eclipse.ui.texteditor.AbstractTextEditor.doSet Input(AbstractTextEditor.java:3063)
at org.eclipse.ui.texteditor.StatusTextEditor.doSetIn put(StatusTextEditor.java:173)
at org.eclipse.ui.texteditor.AbstractDecoratedTextEdi tor.doSetInput(AbstractDecoratedTextEditor.java:15 12)
at org.eclipse.jdt.internal.ui.javaeditor.JavaEditor. internalDoSetInput(JavaEditor.java:2371)
at org.eclipse.jdt.internal.ui.javaeditor.JavaEditor. doSetInput(JavaEditor.java:2344)
at org.eclipse.jdt.internal.ui.javaeditor.Compilation UnitEditor.doSetInput(CompilationUnitEditor.java:1 428)
at org.eclipse.ui.texteditor.AbstractTextEditor$5.run (AbstractTextEditor.java:2396)
at org.eclipse.jface.operation.ModalContext.runInCurr entThread(ModalContext.java:369)
at org.eclipse.jface.operation.ModalContext.run(Modal Context.java:313)
at org.eclipse.jface.window.ApplicationWindow$1.run(A pplicationWindow.java:763)
at org.eclipse.swt.custom.BusyIndicator.showWhile(Bus yIndicator.java:67)
at org.eclipse.jface.window.ApplicationWindow.run(App licationWindow.java:760)
at org.eclipse.ui.internal.WorkbenchWindow.run(Workbe nchWindow.java:2283)
at org.eclipse.ui.texteditor.AbstractTextEditor.inter nalInit(AbstractTextEditor.java:2414)
at org.eclipse.ui.texteditor.AbstractTextEditor.init( AbstractTextEditor.java:2441)
at org.eclipse.ui.internal.EditorManager.createSite(E ditorManager.java:842)
at org.eclipse.ui.internal.EditorReference.createPart Helper(EditorReference.java:583)
at org.eclipse.ui.internal.EditorReference.createPart (EditorReference.java:372)
at org.eclipse.ui.internal.WorkbenchPartReference.get Part(WorkbenchPartReference.java:566)
at org.eclipse.ui.internal.EditorAreaHelper.setVisibl eEditor(EditorAreaHelper.java:263)
at org.eclipse.ui.internal.EditorManager.setVisibleEd itor(EditorManager.java:1474)
at org.eclipse.ui.internal.EditorManager$5.run(Editor Manager.java:1008)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner .java:37)
at org.eclipse.core.runtime.Platform.run(Platform.jav a:858)
at org.eclipse.ui.internal.EditorManager.restoreState (EditorManager.java:1003)
at org.eclipse.ui.internal.WorkbenchPage.restoreState (WorkbenchPage.java:2843)
at org.eclipse.ui.internal.WorkbenchWindow.restoreSta te(WorkbenchWindow.java:1936)
at org.eclipse.ui.internal.Workbench.doRestoreState(W orkbench.java:2873)
at org.eclipse.ui.internal.Workbench.access$14(Workbe nch.java:2821)
at org.eclipse.ui.internal.Workbench$19.run(Workbench .java:1697)
at org.eclipse.ui.internal.Workbench.runStartupWithPr ogress(Workbench.java:1437)
at org.eclipse.ui.internal.Workbench.restoreState(Wor kbench.java:1695)
at org.eclipse.ui.internal.Workbench.access$12(Workbe nch.java:1666)
at org.eclipse.ui.internal.Workbench$17.run(Workbench .java:1545)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner .java:37)
at org.eclipse.ui.internal.Workbench.restoreState(Wor kbench.java:1489)
at org.eclipse.ui.internal.WorkbenchConfigurer.restor eState(WorkbenchConfigurer.java:183)
at org.eclipse.ui.application.WorkbenchAdvisor.openWi ndows(WorkbenchAdvisor.java:702)
at org.eclipse.ui.internal.Workbench.init(Workbench.j ava:1101)
at org.eclipse.ui.internal.Workbench.runUI(Workbench. java:1863)
at org.eclipse.ui.internal.Workbench.createAndRunWork bench(Workbench.java:422)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(Pl atformUI.java:149)
at org.eclipse.ui.internal.ide.IDEApplication.run(IDE Application.java:95)
at org.eclipse.core.internal.runtime.PlatformActivato r$1.run(PlatformActivator.java:78)
at org.eclipse.core.runtime.internal.adaptor.EclipseA ppLauncher.runApplication(EclipseAppLauncher.java: 92)
at org.eclipse.core.runtime.internal.adaptor.EclipseA ppLauncher.start(EclipseAppLauncher.java:68)
at org.eclipse.core.runtime.adaptor.EclipseStarter.ru n(EclipseStarter.java:400)
at org.eclipse.core.runtime.adaptor.EclipseStarter.ru n(EclipseStarter.java:177)
at java.lang.reflect.Method.invoke(libgcj.so.81)
at org.eclipse.core.launcher.Main.invokeFramework(Mai n.java:336)
at org.eclipse.core.launcher.Main.basicRun(Main.java: 280)
at org.eclipse.core.launcher.Main.run(Main.java:977)
at org.eclipse.core.launcher.Main.main(Main.java:952)

nick937
March 18th, 2008, 02:16 PM
so I'm pretty sure that Icon wont help me

jespdj
March 18th, 2008, 03:52 PM
I am using Hardy heron and I have the same problem




nick@t00r:~$ java -version
java version "1.7.0"
IcedTea Runtime Environment (build 1.7.0-b24)
IcedTea 64-Bit Server VM (build 1.7.0-b24, mixed mode)

Try using Sun Java 6 instead of IcedTea. IcedTea is an alpha / beta / development version of Java 7, so you can expect that not everything works perfectly.

sudo apt-get install sun-java6-jdk
sudo update-alternatives --config java
Select Sun Java 6 in the menu that the update-alternatives command shows.

TreeFinger
March 18th, 2008, 05:49 PM
Have you attempted compiling from the command line and not through your IDE? Maybe that will help you decide if it is your IDE or your java SDK install that needs to be fixed.

twodayslate
August 4th, 2008, 04:14 AM
import java.util.*;
public class twod {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println("How many rows?");
int rows = in.nextInt();
System.out.println("How many columns?");
int columns = in.nextInt();
double[][] table = new double[rows][columns];

double input;

for(int r = 0; r < rows; r++)
{
for(int c = 0; c < columns; c++)
{
System.out.println(r + "," + c + " :");
input = in.nextDouble();
table[r][c] = input;
}
}

for(double[] e : table)
{
System.out.print(e);
}
}//end main
}//end

error:
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 twod.main(twod.java:5)

~$ java -versionjava version "1.6.0_06"
Java(TM) SE Runtime Environment (build 1.6.0_06-b02)
Java HotSpot(TM) Client VM (build 10.0-b22, mixed mode, sharing)

What should I choose?

sudo update-alternatives --config java

There are 4 alternatives which provide `java'.

Selection Alternative
-----------------------------------------------
* 1 /usr/lib/jvm/java-6-sun/jre/bin/java
2 /usr/bin/gij-4.2
3 /usr/lib/jvm/java-gcj/jre/bin/java
+ 4 /usr/lib/jvm/java-6-openjdk/jre/bin/java
I hope I did the 2D arrays right.

edit:// Is it my SDK?

twodayslate
August 10th, 2008, 04:20 AM
no help?

tinny
August 11th, 2008, 01:34 AM
no help?

Your answer lies in this thread

http://ubuntuforums.org/showthread.php?t=874500 (same problem as yours)

In summary:

1. Install Sun Java 6 JDK

2. Make sure Sun Java 6 JDK is setup as your default system Java runtime.

3. Make sure that eclipse is using the correct Java runtime.

twodayslate
August 11th, 2008, 02:03 AM
Thanks for the reply!
3. Make sure that eclipse is using the correct Java runtime.Ok I compiled in command line

javac file.java
It did not do anything so I assume it compiled with no errors.

So how do you do #3?

tinny
August 11th, 2008, 02:19 AM
Thanks for the reply!Ok I compiled in command line

javac file.java
It did not do anything so I assume it compiled with no errors.

So how do you do #3?

For #2



java -version
javac -version


Post back the output.

For #3

http://ubuntuforums.org/showthread.php?t=874500&page=2

Posts: 16, 17 and 18

twodayslate
August 11th, 2008, 02:39 AM
For #2



java -version
javac -version


Post back the output.

For #3

http://ubuntuforums.org/showthread.php?t=874500&page=2

Posts: 16, 17 and 18


~/Programming/8.6$ javac -version
javac 1.6.0_0-internal
~/Programming/8.6$ java -version
java version "1.6.0_0"
OpenJDK Runtime Environment (build 1.6.0_0-b11)
OpenJDK Client VM (build 1.6.0_0-b11, mixed mode, sharing)


Thanks for the link! It fixed everything!

tinny
August 11th, 2008, 02:50 AM
I see that your system is still using the OpenJDK as default (Eclipse is working fine because it overrides this).

To setup the system Java environment correctly



sudo update-alternatives --config java


Choose the "/usr/lib/jvm/java-6-sun/jre/bin/java" option.

wei2912
November 6th, 2010, 07:40 AM
I have this problem too.

I installed Eclipse from the Software Center and i selected Java 1.5

However, i cannot import java.util.Scanner

I get this error when i insert Scanner scan = new Scanner(System.in);
Scanner cannot be resolved to a type.
Scanner cannot be resolved to a type.

When i compile my program, i get:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Scanner cannot be resolved to a type
Scanner cannot be resolved to a type

Edit: Problem solved, i was supposed to use the default java.